Public Group

# Physics Question (Frame Independent Integration)

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've been trying to convert my game's physics system to be frame-independent, but ran into a problem with resistive forces (friction, drag, etc.). Acceleration due to resistive forces always ends up being several thousand times what it should be, and ends up overcoming both current velocity and acceleration (ex: if velocity = 10 and standard acceleration = 1, then acceleration due to friction could end up being about -7000). I've included a copy of my "vehicle" class (written in Java) below, simplified operate in only one dimension. I think I made some sort of mistake either in the way I calculate the vehicle's drag coefficient in the constructor, or the way I integrate resistive force in the "updatePhysics" method, but I'm having trouble working it out. I'd appreciate any suggestions anyone can offer.


class Vehicle
{
float  maxSpeed = 0;
double engineAcceleration = 0;
float  xDragCoefficient = 0;

private float xPosition = 0;
private float xSpeed = 0;
private float xAcceleration = 0;
private float xResistance = 0;

public Vehicle(int newMaxSpeed, int newEngineAcceleration)
{
//This is the maximum speed this vehicle should attain under its own power (per millisecond)
maxSpeed = new Double(newMaxSpeed*0.001).floatValue();

//This is the acceleration caused by the force of the vehicle's engine (per millisecond)
engineAcceleration = new Double(newEngineAcceleration*0.001).floatValue();

//This represents the drag coefficient of the vehicle's "front" cross-section.
//It should keep the vehicle's velocity (xSpeed) from exceeding maxSpeed under
//engine power alone.
xDragCoefficient = new Double(2*engineAcceleration/(2*maxSpeed + engineAcceleration)).floatValue();
}

public void updateVehicle()
{
//The engine is accelerating the vehicle forward, possibly in addition to external forces
xAcceleration += engineAcceleration;

//The motion of the vehicle is resisted by its cross-sectional drag, possibly in addition to other resistive forces
xResistance += xDragCoefficient;

updatePhysics();
}

private void updatePhysics()
{
//Get the number of milliseconds that have passed since the last update cycle
int time = Test.getElapsedTime();

//Temporarily store the initial velocity
float xSpeedInitial = xSpeed;

//Integrate the acceleration due to standard forces (acceleration is constant over a single cycle)
xSpeed += xAcceleration*time;

//Determine the average velocity over this cycle in absence of resistive forces
float averageXSpeed = (xSpeedInitial + xSpeed)/2;

//Determine the average resistive acceleration over this cycle
float resistanceXAcceleration = (-xResistance*(averageXSpeed)*time);

//Integrate the acceleration due to resistive forces
xSpeed += resistanceXAcceleration*time;

//Update the possition by integrating the average speed, now that resistive forces are accounted for
xPosition += ((xSpeedInitial + xSpeed)/2)*time;

//Set Acceleration and Resistance to zero in preparation for the next cycle
xAcceleration = 0;
xResistance = 0;
}
}


[Edited by - jsn_segal on July 28, 2005 10:38:40 PM]

##### Share on other sites
No answer, but you might see if you can get this moved to math and physics - you'll probably get more answers there. Also, if you can put your code in a [ source ] [ /source ] block to preserve formatting, people will be more likely to read through it.

##### Share on other sites
Thanks, I'll re-post it in the Math/Physics forum.

##### Share on other sites
i don't know if i understood it correctly but it seems you are looking for canonical game looping

http://www.mindcontrol.org/~hplus/graphics/game_loop.html

1. 1
2. 2
Rutin
25
3. 3
4. 4
5. 5

• 10
• 10
• 13
• 20
• 14
• ### Forum Statistics

• Total Topics
632950
• Total Posts
3009374
• ### Who's Online (See full list)

There are no registered users currently online

×