weight dependend velocity

Official forum for the Chipmunk2D Physics Library.
Post Reply
verty
Posts: 1
Joined: Wed Jul 13, 2011 10:52 am
Contact:

weight dependend velocity

Post by verty »

Hi,
I started creating a 2D pop-down racing game with chipmunk as physics engine.
The gravity is set to (0,0) and damping is used to create the friction between the cars and the ground .The cars are accelerated by adding velocity to their bodies.
I would like to accelerate heavy vehicles slower than light ones, but the damping is not influenced by the weight.
Another problem is that the process of accelerating to the max-speed is very short. The maximum speed is reached after less than a second.
I am trying to solve this problem for days now and i have no solution.

My code for the cars:

Code: Select all

for (int i = 0;i<=ncars;i++){   // 0-ncars
                //NSLog(@"car nr.: %i", i);
                //steering:
                float angvel;
                angvel=cpBodyGetAngVel(car[i].shape->body);
                angvel*=0.1;
                angvel-=car[i].lenkung*dt*30;
                cpBodySetAngVel(car[i].shape->body, angvel);
               
                car[i].speed=pow(((car[i].node.position.x-car[i].lastx)*(car[i].node.position.x-car[i].lastx))
                                          +((car[i].node.position.y-car[i].lasty)*(car[i].node.position.y-car[i].lasty)) , 0.5 );
               
                //cpBodyApplyImpulse(car[i].shape->body,
                //                               cpv(30000000*sin(CC_DEGREES_TO_RADIANS(car[i].node.rotation)), 30000000*cos(CC_DEGREES_TO_RADIANS(car[i].node.rotation))),
                //                               cpv(0,0));
               
                cpVect carvel = cpBodyGetVel(car[i].shape->body);
                //drifting:
                carvel.x=0.9*(carvel.x*abs(sin(CC_DEGREES_TO_RADIANS(car[i].node.rotation))))+0.1*(carvel.x);
                carvel.y=0.9*(carvel.y*abs(cos(CC_DEGREES_TO_RADIANS(car[i].node.rotation))))+0.1*(carvel.y);
               
                //acceleration:
                carvel.x=carvel.x+ car[i].gas *20*sin(CC_DEGREES_TO_RADIANS(car[i].node.rotation));
                carvel.y=carvel.y+ car[i].gas *20*cos(CC_DEGREES_TO_RADIANS(car[i].node.rotation));
               
               
                //carvel.x*=dt;
                //carvel.y*=dt;
                cpBodySetVel(car[i].shape->body , carvel);
                //NSLog(@"car[i].node.rotation: %f",car[i].node.rotation);
               
                car[i].pic.position = ccp(car[i].node.position.x,car[i].node.position.y);
                car[i].pic.rotation = car[i].node.rotation;
                car[i].lastx=car[i].node.position.x;
                car[i].lasty=car[i].node.position.y;
        }
http://pastebin.com/E6SGRyU9
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: weight dependend velocity

Post by slembcke »

Damping provided by Chipmunk is just simple damping and not drag. It slows down all objects at the same rate regardless of their shape or mass. If you want real drag, that is much much more difficult and expensive to calculate. Also, drag/damping isn't a good substitute for friction.

You should look at the Tank demo that comes with Chipmunk. It will show you how you can use specially configured constraints to calculate the friction forces with the ground. Because it works be setting specific forces, you can also control how things accelerate and decelerate.

If you want to implement proper wheel physics, that will be require you write a new constraint type, but it would also be just as hard hard to implement just using forces too.

Lastly, you can simplify a lot of your code using the vector operator functions such as cpvmult, cpvsub, etc. Picking one such example:

Code: Select all

carvel.x=carvel.x+ car[i].gas *20*sin(CC_DEGREES_TO_RADIANS(car[i].node.rotation));
carvel.y=carvel.y+ car[i].gas *20*cos(CC_DEGREES_TO_RADIANS(car[i].node.rotation));
Can become:

Code: Select all

cpVect rotation = cpvforangle(CC_DEGREES_TO_RADIANS(car[i].node.rotation));
carvel = cpvadd(carvel, cpvmult(rotation, car[i].gas*20));
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: Heise IT-Markt [Crawler] and 7 guests