Jump to content
  • Advertisement
Sign in to follow this  

Physics Question (Frame Independent Integration)

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

If you intended to correct an error in the post then please contact us.

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;	

	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 this post

Link to post
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 this post

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


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!