Page 1 of 1

Physics help...

Posted: Mon Nov 24, 2008 3:09 am
by evolve
Ok, for a little test game that I am writing I'm using the physics module mainly for easier collision detection. The game is overhead 2d and thus has no gravity. Originally I just was using the body:setX/setY function in order to move the player, however I then realized that you can't check collision moving bodies that way. After a little searching around the forums I think you have to use setVelocity in order to move bodies, however I'm not having much luck.

Basically nothing is currently happening, here is a little code snippet of what I think is causing the problem...

Code: Select all

if love.keyboard.isDown(love.key_down) then
	local player_x = player_body:getX() + math.cos(math.rad(player_body:getAngle() + 90)) * (player_speed * dt)
	local player_y = player_body:getY() + math.sin(math.rad(player_body:getAngle() + 90)) * (player_speed * dt)
		
	player_body:setVelocity(player_x, player_y)
end
I believe I am doing everything properly, however absolutely no movement happens.

Can anyone point me in the right direction?

Thanks in advance, and thanks for LÖVE.

Re: Physics help...

Posted: Mon Nov 24, 2008 4:56 am
by Ripe
I'm using body:setX() and body:setY() to move my player around and collision works fine, take a look at http://love2d.org/docs/Contact.html to check collision.

Re: Physics help...

Posted: Mon Nov 24, 2008 5:32 am
by evolve
Thank you for the reply, and yes indeed collision does *sometimes* work when you set the position directly using setPosition() or setX/Y, however it only works if you have some gravity. As soon as you setGravity(0,0) all bodies will go straight through each other. Furthermore, from the documentation:
Note that setting the position directly is not a great way to move Bodies. This function should only be used for initial placement of Bodies, or when stuff needs to "warp" from one place to another. When you want Bodies to move, and not warp, use Body:applyImpulse or Body:setVelocity.
I've been having some luck getting applyImpulse to make bodies move, however when I use my trig forumla to fill in the x/y parameters the body only moves on a slanted path. I have still yet to get setVelocity to work at all, I am fairly certain I am doing something wrong.

Oh, and before someone suggests it, I would rather not use the ËNVY framework, or any libraries right now. I'm attempting to understand the basics of the engine.

Re: Physics help...

Posted: Mon Nov 24, 2008 9:00 am
by Kuromeku
You have to use crazy numbers with setVelocity and applyForce/Impulse.

Like 9999999999999999.

Re: Physics help...

Posted: Mon Nov 24, 2008 9:27 am
by rude
Kudomiku wrote:You have to use crazy numbers with setVelocity and applyForce/Impulse.

Like 9999999999999999.
Probably because of this.

Re: Physics help...

Posted: Mon Nov 24, 2008 9:29 am
by Kuromeku
Yeah I read that post, can't you convert it somehow? Using 1337 maths?

Re: Physics help...

Posted: Mon Nov 24, 2008 12:32 pm
by mön
i use set impuls to set the velocity

Code: Select all

function mySetVelocity(body, vx, vy)
  local x, y = body:getVelocity()
  local mass  body:getMass()
  body:applyImpulse(mass * (vx - x), mass * (vy - y))
end
its also important to make the bodies have some mass, otherwise they become static and don't collide with each other

Re: Physics help...

Posted: Mon Nov 24, 2008 5:35 pm
by evolve
I've ended up using applyImpulse to move the body and it is working fine now.

For future reference, when you applyImpulse you do not have to take into consideration the bodies current x/y coords. This makes sense if you think about it, but was causing me a lot of trouble.