## Physics help...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
evolve
Prole
Posts: 5
Joined: Sat Nov 22, 2008 6:17 pm

### Physics help...

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

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

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

### Re: Physics help...

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

Like 9999999999999999.
rude
Posts: 1051
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

### Re: Physics help...

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

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

### Re: Physics help...

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

### Re: Physics help...

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

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.

### Who is online

Users browsing this forum: slime and 31 guests