Turning a body around a body [Solved]

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Turning a body around a body [Solved]

Post by Jack5500 »

Hey,
I was trying to set up a death trap. I've a bdy type that kill the player, which is no problem, but know I need to have one body with this image overlay to turn around the second static body and I have no idea how to do that

Concept:
Image

Main Problems:
-Ongoing motion of the turning body around the static one
-Moving the image with the body
Last edited by Jack5500 on Thu Dec 29, 2011 1:26 pm, edited 2 times in total.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: Turning a body around a body

Post by Nixola »

Code: Select all

function love.load()
  center_x, center_y = 256, 256
  angle = 0
  distance = 128
end

function love.update(dt)
angle = angle + 360*dt
sinn = math.sin(math.rad(angle))
coss = math.cos(math.rad(angle-180))
end

function love.draw()
love.graphics.circle('line', center_x, center_y, 64, 64)
love.graphics.circle('fill', center_x+(distance*sinn), center_y+(distance*coss), 32, 32)
end
This code will draw an empty circle and a full circle spinning around it, 1 rotation per second. If you change center_x or center_y both images will move, if you raise distance the radius of the rotation will raise.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: Turning a body around a body

Post by Jack5500 »

I tired it with a physic body & shape and that fucked up all the complete game. Now I seem to randomly die and when i start my player makes a random jump to the right, plus the collision are not even working properly. I'm doing it very wrong I gues.. I just wanted to check if the player collides with the shape to kill him, but maybe you have a better solution.

Code: Select all

function Play:enteredState()

center_x = 256
center_y = 256
angle = 0
distance = 128 
sinn = 0
coss = 0
[...]
objects.enemy.two = {}
objects.enemy.two.body = love.physics.newBody(world, center_x+(distance*sinn), center_y+(distance*coss), 0, 0)
objects.enemy.two.shape = love.physics.newCircleShape (objects.enemy.two.body, center_x+(distance*sinn), center_y+(distance*coss),32)
[...]

function Play:update(dt)

world:update(dt)

--Update the Rotation variables
angle = angle + 360*dt
sinn = math.sin(math.rad(angle))
coss = math.cos(math.rad(angle-180))

objects.enemy.two.body:setX( center_x+(distance*sinn) )
objects.enemy.two.body:setY( center_y+(distance*coss) )

[...]

function Play:draw()
[...]
love.graphics.circle('line', center_x, center_y, 64, 64)
love.graphics.circle("line",objects.enemy.two.body:getX(), objects.enemy.two.body:getY(),objects.enemy.two.shape:getRadius(), 20)
[...]

User avatar
Almost
Prole
Posts: 42
Joined: Fri Aug 21, 2009 8:04 pm
Location: Canada
Contact:

Re: Turning a body around a body [Not solved yet]

Post by Almost »

It sounds like the object is spawning on top of the player. Just move centreX or decrease distance if that is the case.
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Turning a body around a body [Not solved yet]

Post by The Burrito »

Personally, if using box2D / love.physics I'd probably either stick two bodies together with a joint ( a distance joint where the fixed center circle has a mass of 0 and the outer one is nonzero) and apply some force to make the outer one spin or I think the easiest way to do it is probably to make one body, and attach the outer shape to it with an offset, then all you have to do is rotate the body, and draw the sprites using body:getWorldPoint().

Code: Select all

function Play:enteredState()

center_x = 256
center_y = 256
angle = 0
distance = 128 
sinn = 0
coss = 0
[...]
objects.enemy.two = {}
objects.enemy.two.body = love.physics.newBody(world, center_x, center_y, 0, 0)
objects.enemy.two.shape = love.physics.newCircleShape (objects.enemy.two.body, distance, 0,32) --the number values are already centered at the body position
[...]

function Play:update(dt)

world:update(dt)
objects.enemy.two.body:setAngularVelocity( 6.28 ) -- setting this each frame should maintain the rotational speed 


[...]

function Play:draw()
[...]
love.graphics.circle('line', center_x, center_y, 64, 64)
local x,y = objects.enemy.two.body:getWorldPoint(distance,0)
love.graphics.circle("line",x,y,objects.enemy.two.shape:getRadius(), 20)
[...]
I don't know what the rest of your code looks like, but that should give you an idea, generally speaking if you're using a physics engine you shouldn't need trigonometry very often, getWorldPoint and getLocalPoint are really handy.
User avatar
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: Turning a body around a body [Not solved yet]

Post by Jack5500 »

But now the enemy.two spins around itself and not around the circle
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Turning a body around a body [Not solved yet]

Post by The Burrito »

It's hard to tell you how to fix it without being able to see everything thats going on, but basically the idea is that the body you want to go around in a circle is actually located at the center of the rotation, that way when you apply rotation the fact that the shape is over to the side should cause it to rotate around the body's location. more than likely you're just accidentally making things more complicated than they need to be.
User avatar
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: Turning a body around a body [Not solved yet]

Post by Jack5500 »

Okay, i see the concept, but not where my error is located. Here is a basic gif of what is happening right now. IT's rotating but just around it's own axis

Image

Tell me if you want me to attach the code. :)
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Turning a body around a body [Not solved yet]

Post by The Burrito »

hmm, yeah I still need to see it, you can pm it to me if you want. Physics is tricky sometimes.

edit: OK, my mistake before, rather than settingAngularVelocity you could try body:setAngle(body:getAngle()+1*dt) obviously varying the 1 depending on speed and direction you want.

This however will create unnatural collisions so it's not ideal.
you could use a revolute joint acting as a motor, and have anchor body that it's hooked to. Something like this:

Code: Select all

anchor = love.physics.newBody(world, 0,0)
[...]
objects.enemy.two = {}
objects.enemy.two.body = love.physics.newBody(world, center_x+distance, center_y, 1, 1) --non zero mass so it can move freely
objects.enemy.two.shape = love.physics.newCircleShape (objects.enemy.two.body, 0, 0,32) 
objects.enemy.two.joint = love.physics.newRevoluteJoint( anchor, objects.enemy.two.body, center_x, center_y )
objects.enemy.two.joint:setMotorSpeed( -10 ) --set the motor speed
objects.enemy.two.joint:setMaxMotorTorque( 10000 ) --something ludicrously high, doesn't really matter
objects.enemy.two.joint:setMotorEnabled( true ) --turn the motor on
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 78 guests