Page 1 of 1

Trouble with Forces/Movement

Posted: Mon Oct 28, 2013 11:54 pm
by Paragon
I am having difficulty figuring out how to properly achieve the effect I am looking for. I have a top down view, so gravity would be along the z axis, into the screen... so I don't set a gravity value.

The issue is that I can't get a body to move in response to controls the way I'd like.
Each iteration of the game loop I poll the keyboard, if the right arrow is down I set the force to (10,0), if no keys are down I set the force to (0,0). I am running into two issues with this, 1) I am applying a constant force so obviously the body continues to accelerate, and two removing the force does not affect the velocity of the body even though it stops accelerating.

I guess I am trying to achieve some sort of resistance force, applying a constant force should accelerate the body until a terminal velocity is reached, releasing the key should *quickly* slow then stop the body. I realize the resistance force alone wouldn't stop the object quickly enough for my liking.

The effect should really be similar to the controls you would expect from asteroid or geometry wars, but I don't particularly want to set a hard limit on velocity using cpBodySetVelLimit b/c I want to be able to upgrade the players ships engines to produce more force, thereby increasing their maximum velocity...

Does anyone have any tips on how I could get this to work?

Re: Trouble with Forces/Movement

Posted: Tue Oct 29, 2013 1:48 am
by LegoCylon
Setting the force to 0 just means that it won't cause the object to accelerate in any direction; the existing velocity is retained until the object collides with something or you apply another force. This is expected behavior for a physics system if there is no gravity or damping factor configured.

Have you tried setting a damping factor on the space? It will automatically reduce object velocities each update and it's pretty simple to configure.

Another option is to apply a per-body custom damping factor by overriding the velocity callback for a body and then multiplying the damping parameter (which has the value for the space) by your custom amount (which you can access via the user data from the body).

You can also apply a force opposite to the direction of the velocity, but you'll need to constantly adjust the force to make sure you don't push it in the opposite direction and ping-pong.

Finally, the typical advice for "direct control" is to check out the TankDemo - it demonstrates configuring a rogue "control body" which is connected to the "primary body" by a constraint. Since the control body is not in the space, it won't simulate collisions, but it will apply appropriate forces to the primary body to maintain the constraint. You have to maintain more objects, but you can make the physics system do all the hard work for you. This option works quite well if you don't want any forces to act upon your primary body other than those applied by the control body.

Re: Trouble with Forces/Movement

Posted: Wed Oct 30, 2013 10:04 am
by Paragon
Thanks, that was exactly what I was looking for.

Re: Trouble with Forces/Movement

Posted: Fri Dec 20, 2013 7:19 am
by adrianjuaneli
I too faced this kind of situation about the properly achieve the effect. The information which is shared in this thread is remarkable and worthy.

reflector design
reverse ray tracing

Re: Trouble with Forces/Movement

Posted: Fri Dec 20, 2013 11:54 am
by slembcke
Ok. For spam that is amusingly apropos.