Trouble with Forces/Movement

Official forum for the Chipmunk2D Physics Library.
Post Reply
Paragon
Posts: 3
Joined: Wed Oct 09, 2013 12:05 am
Contact:

Trouble with Forces/Movement

Post 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?
LegoCylon
Posts: 29
Joined: Wed May 09, 2012 12:06 am
Contact:

Re: Trouble with Forces/Movement

Post 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.
Paragon
Posts: 3
Joined: Wed Oct 09, 2013 12:05 am
Contact:

Re: Trouble with Forces/Movement

Post by Paragon »

Thanks, that was exactly what I was looking for.
adrianjuaneli
Posts: 1
Joined: Fri Dec 20, 2013 7:11 am
Contact:

Re: Trouble with Forces/Movement

Post 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
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Trouble with Forces/Movement

Post by slembcke »

Ok. For spam that is amusingly apropos.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests