Velocity Verlet Integration

Jan 3, 2013 at 3:26pm
closed account (2NywAqkS)
I'm new to the idea of Velocity Verlet Integration, whereas before I was apparently using Euler's method. After finding out about this I returned to an asteroid gravity simulation(whereby every asteroid is gravitationally attracted to every other on) I'd made a while ago and tried to implement the integration. However it doesn't quite work and I get get some odd results. Not insane, but not what I'd expect in real life.
I'm following this tutorial;
http://buildnewgames.com/gamephysics/

The code (if it doesn't make sense please ask questions):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
lastAcceleration = avgAcceleration;
position.x += velocity.x * dT + ( 0.5 * lastAcceleration.x * dT * dT);
position.y += velocity.y * dT + ( 0.5 * lastAcceleration.y * dT * dT);

// Calculate New Acceleration
for(unsigned int i = 0; i < asteroidVector.size(); i++)
{
	if (i != id) // Stops asteroid from caclulating it self
	{
		float dx = asteroidVector[i].position.x - position.x;
		float dy = asteroidVector[i].position.y - position.y;
		float direction = fast_atan2f(dy, dx);
		float distanceSquared = (dx*dx + dy*dy);
			
		if (distanceSquared < 200.0f)
		{
			continue; // This stop the asteroids fly off when they collide
		}

		float force = 100.0f * ((mass * asteroidVector[i].mass)/distanceSquared);
		newAcceleration.x = (force / mass) * cos(direction);
		newAcceleration.y = (force / mass) * sin(direction);
	}
}

avgAcceleration.x = (lastAcceleration.x + newAcceleration.x) / 2.0;
avgAcceleration.y = (lastAcceleration.y + newAcceleration.y) / 2.0;

velocity.x += avgAcceleration.x * dT;
velocity.y += avgAcceleration.y * dT;
Jan 3, 2013 at 3:37pm
You are overwriting the value of `newAcceleration', so in the end just considering the last asteroid.
Topic archived. No new replies allowed.