Quick question: Object moves different amount each frame despite constant velocity.

GeanGean
Posts: 7
Joined: Sat Feb 04, 2023 11:04 pm

Quick question: Object moves different amount each frame despite constant velocity.

Post by GeanGean »

I have a vehicle (but I think the issue exists with normal rigid bodies) in a scene with a constant timestep that I get the position of each frame, to simplfy:

Code: Select all

//world->stepSimulation(deltaMS / 1000.0);
world->stepSimulation(0.05,1,0.05);

if(car->getCurrentSpeedKmHour() > someValue)
//if(car->getRigidBody()->getLinearVelocity().length() > someValue)
{
     car->getWheel(0).applyEngineForce(someOtherValue);
     car->getWheel(1).applyEngineForce(someOtherValue);
}

btVector3 pos = car->getRigidBody()->getWorldTransform().getOrigin();
float changeInPos = pos.x() - lastPos;
lastPos = pos.x();

std::cout<<"Change in pos: "<<changeInPos<<" Velocity: "<<car->getRigidBody()->getLinearVelocity().x()<<"\n";
I could end up with output like this:

Code: Select all

Change in pos: 4.2 Velocity 250.0
Change in pos: 4.1 Velocity 250.0
Change in pos: 3.9 Velocity 250.0
Change in pos: 4.2 Velocity 250.0
Change in pos: 3.7 Velocity 250.0
Change in pos: 4.5 Velocity 250.0
Change in pos: 4.3 Velocity 250.0
If the timestep is constant, and the velocity is constant, what is the issue? Why is change in pos not the same every time?
User avatar
drleviathan
Posts: 849
Joined: Tue Sep 30, 2014 6:03 pm
Location: San Francisco

Re: Quick question: Object moves different amount each frame despite constant velocity.

Post by drleviathan »

Also curious is: the distance traveled along the x-axis does not agree with your speed and timestep:

Code: Select all

time = 0.05 sec
rate = 250 m/sec
distance = rate * time =  250 m/sec * 0.05 sec = 12.5m
I wonder if you're bumping up against a maximum speed limit, especially since your total speed is so steady. Maybe try measuring things at slower speeds?