Page 1 of 1

physics vector to move 1px per update?

Posted: Wed Aug 26, 2009 5:05 am
by sillydino
Hello all,

I'm trying to right my vector so that it moves my body at 1 pixel per update cycle. If I set my vector to 1, nothing happens, if I set my vector to 50, the body moves, but skips pixels. I've read the box2d documentation and the love documentation but I can't figure out a way to compute this. Can someone please help?

Thanks!

Re: physics vector to move 1px per update?

Posted: Wed Aug 26, 2009 11:45 am
by napco
Hi! I'm not very good with physics, but i think that the value you've changed from 1 to 50 is the intensity of the movement (you can push a glass with a finger either slowly and quickly). Physics module takes advance of the dt (delta time) value, so it's difficult (but not impossible, i think) to move your body by 1 pixel per update. I'll give a look and then i'll reply if i find the solution. Another solution is to build your own physics module (in fact to move a sprite in my game i don't use physics).

Re: physics vector to move 1px per update?

Posted: Wed Aug 26, 2009 4:41 pm
by TechnoCat
If you want to move it one pixel per update you could write this in update(dt)

Code: Select all

body:setX(body:getX()+1)
This moves 1 pixel/update

But you are more likely wanting to do it one pixel per second. This is because the update cycle frequency highly depends on the speed of the computer it is being run on.
You would do this in update(dt)

Code: Select all

body:setX(body:getX()+1*dt)
This moves 1 pixel/second

Re: physics vector to move 1px per update?

Posted: Thu Aug 27, 2009 2:13 am
by sillydino
TechnoCat wrote:If you want to move it one pixel per update you could write this in update(dt)

Code: Select all

body:setX(body:getX()+1)
This moves 1 pixel/update

But you are more likely wanting to do it one pixel per second. This is because the update cycle frequency highly depends on the speed of the computer it is being run on.
You would do this in update(dt)

Code: Select all

body:setX(body:getX()+1*dt)
This moves 1 pixel/second
I tried moving via the setX/Y functions on Body, but the collision detection doesn't work. Does anyone know how to get both pixel by pixel movement with collision detection?

Re: physics vector to move 1px per update?

Posted: Thu Aug 27, 2009 3:04 am
by TechnoCat
Every update also check if it is colliding, if it is colliding move it back before update(dt) is finished.

Re: physics vector to move 1px per update?

Posted: Thu Aug 27, 2009 3:15 am
by sillydino
TechnoCat wrote:Every update also check if it is colliding, if it is colliding move it back before update(dt) is finished.
That is what I was doing before integrating with the physics module. My goal in using the physics module is to inherit the collision detection. At the same time, I need to be able to track at a pixel level so when the unit hits the center of the screen I can transition motion back to the map scroller(for mimicking a camera). I gotta believe there's a way to back compute from 'one pixel per update' to 'some vector', I'm just too dumb to figure it out.