Car simulation (top down) following a path

Official forum for the Chipmunk Physics Library.

Car simulation (top down) following a path

Postby carlessr on Wed Feb 03, 2010 5:12 am

Hi,

I've recently discovered Chipmunk, and wow, I wish I'd found it 6 months ago!

I'm retro fitting Chipmunk to my current game, as it will address various weaknesses in my own collision system.

My my game is a top down tank game, the vehicle is controlled by drawing out a path and the vehicle follows the path. I realise Chipmunk isn't designed for this kind of physics. I have read various posts on this site on how to simulate top down vehicles, however I'm not really sure where to start.

The basic effect I'm looking to achieve is getting a shape (a box) to follow a path (a series of points) (accurately, I could vary ground friction to simulate snow etc ...). My initial thoughts are to implement my own velocity function for the tank, but I want to maintain the angular velocity from impacts and so forth.

I'd be really grateful if someone could point me in the correct direction.


Cheers
Rich
carlessr
 
Posts: 18
Joined: Wed Jan 27, 2010 3:15 pm

Re: Car simulation (top down) following a path

Postby slembcke on Wed Feb 03, 2010 12:52 pm

I've been meaning to try this out for a while, but haven't really had the time or the project for it. Basically what you need is to be able to simulate the friction of the tank sitting on the ground to be able to make your tank move and spin.

Chipmunk currently doesn't have constraints that help you do this, but you can do it well enough using a bit of a hack. It should work well enough for a tank game. Essentially, what you need to do is to create a static body that you joint your tank to, then you control your tank by changing the static body.

So here's what I would try:
  • Create a static body (infinite mass and moment of inertia, don't add it to the space)
  • Add a pivot joint between the tank's body and the static body using cpPivotJointNew2(), use cpvzero for both joint anchors.
  • Add a gear joint between the tanks' body and the static body. Use 1.0 for the ration and 0.0 for the phase.
  • Set the biasCoef of both joints 0.0. Setting the maxForce of the joints will control the amount of friction. The pivot controls the sliding, and the gear joint controls the rotation.

With all this set up, setting the velocity of the static body will control the movement of the tank and setting the rotational velocity will control the tank's rotation. No need to move the static body around, just modify the velocities. It works by using the joints to lock the velocity of the tank to the velocity of the static body. By setting the biasCoef to 0.0, you are disabling the position correction the joint would normally do to keep the joints from stretching. The maxForce is acting like friction by limiting how much force can be used to match their velocities.

It's also possible to control the angle of the tank directly if you don't really care about directly controlling the rotation speed. Instead of setting the gear joint's biasCoef value, set the maxBias value to whatever speed you want the tank to rotate at. This way the angular correction of the gear joint will be active, but limited to the rate defined by maxBias. You can now set the angle of the static body using cpBodySetAngle() and the the tank body will rotate to match it.

Lastly, if you want to define the tank's velocity relative to the tank's rotation (presumably you do), do something like this:
Code: Select all
staticTankBody-> = cpvrotate(tankBody->rot, cpv(speed, 0.0))


Cheers.
I'm the guy that wrote Chipmunk. Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 1485
Joined: Tue Aug 14, 2007 7:13 pm

Re: Car simulation (top down) following a path

Postby carlessr on Wed Feb 03, 2010 2:29 pm

Hi,

First off thank you very much for the reply. I think this is pretty much what I'm after, however there are a few caveats with my implementation ...

I've translated what you said into the following;

Code: Select all
   //* Create a static body (infinite mass and moment of inertia, don't add it to the space)
   m_sbody = cpBodyNew(INFINITY,INFINITY);
   m_sshape = cpCircleShapeNew(m_sbody, 20, cpv(m_pos.x,m_pos.y));
   //* Add a pivot joint between the tank's body and the static body using cpPivotJointNew2(), use cpvzero for both joint anchors.
   m_joint = cpPivotJointNew2(m_body,m_sbody,cpvzero,cpvzero);
   cpSpaceAddConstraint(space,m_joint);
   //* Add a gear joint between the tanks' body and the static body. Use 1.0 for the ration and 0.0 for the phase.
   m_gear = cpGearJointNew(m_body, m_sbody, 0, 1.0);
   cpSpaceAddConstraint(space,m_gear);
   //* Set the biasCoef of both joints 0.0. Setting the maxForce of the joints will control the amount of friction. The pivot controls the sliding, and the gear joint controls the rotation.      
   m_joint->biasCoef = 0;
   //m_gear->biasCoef = 0;
   m_gear->maxBias = 1.0;
   
   cpBodySetAngle(m_body,DEG2RAD(135));
   m_sbody->v = cpv(40,40);


Note I set the angle to 135 degrees and send the tank off at 40,40.

Here's the issues I'm encountering;

- When the tank starts moving it rotates back to 0 degrees (do I need to set the rotation every frame?)
- The tank seems to have an infinite mass and is no longer affected by objects it collides with in the world (a collision occurs but no angular momentum seems to be generated, don't know if this is due to the static body).
- Also the tank seems to jitter as it moves through the world (compared to other objects)

Thanks again for your help and any further help you could would be much appreciated.


Cheers
Rich
carlessr
 
Posts: 18
Joined: Wed Jan 27, 2010 3:15 pm

Re: Car simulation (top down) following a path

Postby slembcke on Wed Feb 03, 2010 3:10 pm

I couldn't help myself. I went ahead and made a demo with a tank that follows the mouse. If you check out the trunk code and build it, it is demo S.

Source here:
http://code.google.com/p/chipmunk-physi ... emo/Tank.c

To answer the questions in your last post:
1) No, you should not have to set the angle of your control body every frame. It doesn't get simulated so it should never change unless you do it yourself.
2) In the code you posted, you attached the circle shape to the infinte mass control body.
3) Not sure about the jitter.

Take a look at the code I posted above.
I'm the guy that wrote Chipmunk. Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 1485
Joined: Tue Aug 14, 2007 7:13 pm

Re: Car simulation (top down) following a path

Postby carlessr on Wed Feb 03, 2010 4:18 pm

Thanks again, I've lifted your code (verbatim) and it's pretty cool.

However I still get some weirdness.

- If I click around the screen the tanks seems to move fine for a while, however after a collision with a dynamic object (also a box) the controls seem to go a bit odd and the tank seems to drive in circles whilst continuously rotating.
- If I ram another object I don't seem to incur any (visible) effect from the collision, no rotation and no movement (again dynamic simulated objects). However as I mentioned in the previous point I think a collision effects control system.

Do you get these effects on your demo?

Cheers
Rich
carlessr
 
Posts: 18
Joined: Wed Jan 27, 2010 3:15 pm

Re: Car simulation (top down) following a path

Postby slembcke on Wed Feb 03, 2010 5:23 pm

Not sure about the first issue. As for the second, you will have to tweak mass ratios and friction amounts. It should do what you want, the trick is finding values that make it look nice.
I'm the guy that wrote Chipmunk. Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 1485
Joined: Tue Aug 14, 2007 7:13 pm

Re: Car simulation (top down) following a path

Postby carlessr on Wed Feb 03, 2010 5:26 pm

ok brilliant, I'll give it a go.

Thanks again for your help.
carlessr
 
Posts: 18
Joined: Wed Jan 27, 2010 3:15 pm

Re: Car simulation (top down) following a path

Postby carlessr on Thu Feb 04, 2010 5:56 pm

Hi,

I created a little test app to identify some of the problems I mentioned, please forgive the awful draw code it was knocked together in haste.

the zip is a little windows app, I've left the debug exe in the zip file for convince.

Note how the tank behaves a little odd from time to time.

If you had time to have a look and maybe steer me in the right direction (forgive the pun) I'd be more than grateful.

http://www.sundaystudios.co.uk/chiptest.zip


Thanks
Rich
carlessr
 
Posts: 18
Joined: Wed Jan 27, 2010 3:15 pm

Re: Car simulation (top down) following a path

Postby slembcke on Fri Feb 05, 2010 9:46 am

Why I try to run it in my VM, it tells me that "This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem."
I'm the guy that wrote Chipmunk. Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
User avatar
slembcke
Site Admin
 
Posts: 1485
Joined: Tue Aug 14, 2007 7:13 pm

Re: Car simulation (top down) following a path

Postby carlessr on Fri Feb 05, 2010 1:40 pm

Strange ... works for me.

I've build a clean version here : http://www.sundaystudios.co.uk/chiptest.zip

It's built under 64-bit windows 7 using visula studio 2008 (however it is not a 64-bit exe, it is 32-bit).

If that version doesn't work I'll have to build a mac version (I'm no so familiar with mac apps, which is why I didn't build a mac version to start with).


Cheers
Rich
carlessr
 
Posts: 18
Joined: Wed Jan 27, 2010 3:15 pm

Next

Return to Chipmunk Physics

Who is online

Users browsing this forum: No registered users and 2 guests

cron