###

» Sun Nov 20, 2011 4:33 pm

For MoonShield I moved the moon with events and the asteroids with physics :

basically for the moon I increase an angle private variable (I have the distance from the earth in another pv) and then I just position them with

[code]moon.X = earth.X + cos(moon.angle)*moon.distance

moon.Y = earth.Y + sin(moon.angle)*moon.distance[/code]

For the asteroids it's a bit more complicated. I calculate the gravity force of each asteroids toward each moons and the earth

Moons, Earth and Asteroids have all a "mass" private variable (so I can tweak it)

And I just have to :

[code]foreach asteroid

/// Calculating the gravity vector toward the earth following the gravity formula G*m1*m2/d (G is multiplied when I apply force at the end)

asteroid.xVector = (asteroid.mass* earth.mass/distance(asteroid.X,asteroid.Y,earth.X,earth.Y)^2)*cos(angle(asteroid.X,asteroid.Y,earth.X,earth.Y)

asteroid.yVector = (asteroid.mass* earth.mass/distance(asteroid.X,asteroid.Y,earth.X,earth.Y)^2)*sin(angle(asteroid.X,asteroid.Y,earth.X,earth.Y)

/// adding the other gravity vector

foreach moon

ADD (asteroid.mass* moon.mass/distance(asteroid.X,asteroid.Y,moon.X,moon.Y)^2)*cos(angle(asteroid.X,asteroid.Y,moon.X,moon.Y)

ADD (asteroid.mass* moon.mass/distance(asteroid.X,asteroid.Y,moon.X,moon.Y)^2)*sin(angle(asteroid.X,asteroid.Y,moon.X,moon.Y)

/// Applying the result gravity vector

Every Tick asteroid Apply physics force (Clamp(asteroid.xVector*gFactor*dt,minVect,maxVect),Clamp(Self.yVector*gFactor*dt,minVect,maxVect)) at image point 0[/code]

And that's all, the physics behavior use the force I calculated to create the movement.

Oh and I clamped the vector because the asteroids tended to go to fast and the gFactor is a global variable used to tweak the gravity a bit for better gameplay

Yann2011-11-20 16:39:53