/* Sparx * Copyright August Zinsser 2007 * This program is distributed under the terms of the GNU General Public License */ using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; namespace Pina3D.Particles { public class Gravitational : Force { private Emitter mCenter; private Vector3 mCentralDisplacement; public Emitter Center { set { mCenter = value; } get { return mCenter; } } /// /// Displaces the center of gravity from the central object by this amount /// public Vector3 CentralDisplacement { set { mCentralDisplacement = value; } get { return mCentralDisplacement; } } public Gravitational(string name, float lifeSpan, float delay, Emitter center, Vector3 centralDisplacement, float magnitude) : base (name, lifeSpan, delay) { mCenter = center; mMagnitude = magnitude; mCentralDisplacement = centralDisplacement; } public override Object Clone() { Gravitational retFo = new Gravitational(mName + "_clone", mAge, mDelay, mCenter, mCentralDisplacement, mMagnitude); return retFo; } public override void Update(Emission emission) { if (mCenter != null && emission.Age > mDelay && emission.Age < mLifeExpectancy + mDelay) { RecalculateForceVector(emission); emission.Velocity3D += Vector3.Multiply(mForceVector, Pina.ElapsedSeconds); } } /// /// Makes a force vector that points from this emission to the center /// /// private void RecalculateForceVector(Emission newCenter) { // Doing real gravity makes it freak out a bit, and it looks cooler without it... so do it without real gravity! mForceVector = mCenter.Position3D + mCentralDisplacement - newCenter.Position3D; //float distanceSquared = (float)mForceVector.LengthSquared(); mForceVector.Normalize(); //mForceVector = Vector2.Multiply(mForceVector, mMagnitude * (1 / distanceSquared)); mForceVector = Vector3.Multiply(mForceVector, mMagnitude); } } }