Physics help...

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
evolve
Prole
Posts: 5
Joined: Sat Nov 22, 2008 6:17 pm

Physics help...

Post 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.
Ripe
Prole
Posts: 11
Joined: Wed Nov 19, 2008 3:28 pm
Location: Australia

Re: Physics help...

Post 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.
evolve
Prole
Posts: 5
Joined: Sat Nov 22, 2008 6:17 pm

Re: Physics help...

Post 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.
User avatar
Kuromeku
Party member
Posts: 166
Joined: Sun Jul 20, 2008 5:45 pm

Re: Physics help...

Post by Kuromeku »

You have to use crazy numbers with setVelocity and applyForce/Impulse.

Like 9999999999999999.
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Physics help...

Post by rude »

Kudomiku wrote:You have to use crazy numbers with setVelocity and applyForce/Impulse.

Like 9999999999999999.
Probably because of this.
User avatar
Kuromeku
Party member
Posts: 166
Joined: Sun Jul 20, 2008 5:45 pm

Re: Physics help...

Post by Kuromeku »

Yeah I read that post, can't you convert it somehow? Using 1337 maths?
User avatar
mön
Prole
Posts: 13
Joined: Thu Sep 18, 2008 8:53 am
Location: Zurich
Contact:

Re: Physics help...

Post 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
evolve
Prole
Posts: 5
Joined: Sat Nov 22, 2008 6:17 pm

Re: Physics help...

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot], Google [Bot] and 2 guests