Dragging car on ground

Official forum for the Chipmunk2D Physics Library.
Decept
Posts: 14
Joined: Tue Oct 13, 2009 2:38 am
Contact:

Dragging car on ground

Post by Decept »

In my iPhone game, I have created a car very similar to the one in the moonbuggy example that rolls on a flat ground (static line shape). I want to be able to drag the car forward/backward on the ground using touches/mouse and the wheels should roll correctly due to friction. I have tried several approaches but none worked perfectly.
These are the two best approaches so far:

1. Not adding the car body to the space. Keep track of how much the mouse point has moved on the x-axis and then drag the car itself by updating the horizontal position and velocity of the car body. This works except that for some reason the wheels are lagging behind in movement. They catch up as soon as I stop dragging.

2. Create a mouse body like in the examples but updating only the x-value and with the same y-position as the car body. Constrain the mouse body to the car body (which is in space). The problem here is that the car can bounce around, probably because of the wheels acting on car body's rotation.

I like the first approach more because I can control the sitiuation better. But just can't get the wheels to move along correctly.

Any ideas on this problem? I've been at it for many hours now and I'm out of ideas.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Dragging car on ground

Post by slembcke »

Search for cpMouse on the forums if you want to continue using 4.x. The trunk code contains a more robust version of mouse interaction based on some new joint stuff if you want to move to the latest code.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Decept
Posts: 14
Joined: Tue Oct 13, 2009 2:38 am
Contact:

Re: Dragging car on ground

Post by Decept »

sorry, I should have mentioned that I'm using the trunk already. One of the few changes I made to the car from the moonbuggy example is that I swapped the joints for constraints.
Decept
Posts: 14
Joined: Tue Oct 13, 2009 2:38 am
Contact:

Re: Dragging car on ground

Post by Decept »

Any ideas why the wheels are lagging behind the car in my first approach? Each wheel is constrained by a pin joint from the side and a damped spring from above. Shouldn't the pin joint stop the wheel from going straight sideways?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Dragging car on ground

Post by slembcke »

My guess is that you are calculating the velocity incorrectly. If the velocity you are calculating is too small (did you remember to divide by the timestep?) it will be mostly up to the position correction to pull the wheels along. The position correction is really only supposed to be correcting small errors and looks mushy or damped when correcting large errors.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Decept
Posts: 14
Joined: Tue Oct 13, 2009 2:38 am
Contact:

Re: Dragging car on ground

Post by Decept »

I do this in my step: function

Code: Select all

const float timeStep = 1.0f/60.0f;
mTotalTime += dt;
int steps = mTotalTime / timeStep;
mTotalTime -= steps * timeStep;

cpVect carVel = cpv( ( newPos.x - carBody->p.x ) / (steps*timeStep), 0.0f );
carBody->p = newPos;
carBody->v = carVel;

for( int i = 0; i < steps; i++ )
    cpSpaceStep( mSpace, timeStep );
timeStep is fixed. I calculate how many steps to do in this frame.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Dragging car on ground

Post by slembcke »

Not sure exactly, but part of the problem is most likely due to the coarseness of your position and velocity updates. You are stepping several times based on a single position and velocity change. You should smooth out the position change and apply it and the velocity each step. Otherwise you are going from moving for a couple of steps to completely stopped the next time your loop happens. User input coming from the OS is very coarse, it gathers them up and sends them only a handful of times per second and not every step. The physics does not like jumpiness that.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Decept
Posts: 14
Joined: Tue Oct 13, 2009 2:38 am
Contact:

Re: Dragging car on ground

Post by Decept »

Thanks for helping me with this.
I already filter the input like in one of the examples:

Code: Select all

cpVect newPoint = cpvlerp( mLastMousePoint, mMousePoint, 0.25f );
mLastMousePoint = newPoint;
mMousePoint is updated in the touch functions.

I also tried to split the velocity and apply it on every step, but I got the same result:

Code: Select all

const float timeStep = 1.0f/60.0f;
mTotalTime += dt;
int steps = mTotalTime / timeStep;
mTotalTime -= steps * timeStep;

cpVect movement = cpv( newPos.x - carBody->p.x, 0.0f );
cpVect stepCarVel = cpvmult( movement, (1.0f/steps)/timeStep );

for( int i = 0; i < steps; i++ )
{
    carBody->p = cpv( carBody->p.x + stepCarVel.x * timeStep, carBody->p.y );
    carBody->v = stepCarVel;
    cpSpaceStep( mSpace, timeStep );
}

User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Dragging car on ground

Post by slembcke »

Two more potential problems with the new code you posted. The important part is that the velocity is smoothed and recalculated per step, the position follows the velocity naturally. Secondly, if the body of the car is still added to the space, that means that it's position is getting updated twice, once by you and once by the space shortly after. Calculating the velocity alone and letting the space handle the position is a much better idea.

Honestly, having tried this before myself, the solution really is to use a constraint to move things around.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Decept
Posts: 14
Joined: Tue Oct 13, 2009 2:38 am
Contact:

Re: Dragging car on ground

Post by Decept »

I did as you suggested and only set the velocity and let the space update the position. But the result is exactly the same, with the wheels lagging behind.
I have not had much success with using a constraint to move the car as the car is now totally under the control of the space. The result is that you can make the car bounce around because of the forces from the springs. The wheels are updated correctly though.
Post Reply

Who is online

Users browsing this forum: No registered users and 34 guests