using System; using System.Collections.Generic; using System.Linq; using System.Text; using BEPUphysics; using BEPUphysics.DataStructures; using BEPUphysics.Entities; using SpaceSupport; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Input; namespace Derelict { class Ship3d { //PHYSICS public Space interiorSpace; //STRUCTURAL DATA public List tiles3d; public Ship shipContent { set { _shipContent = value; } get { return _shipContent; } } private Ship _shipContent; public CompoundBody shipCore; //INPUT RELATED STUFF private GamePadState oldGPS, newGPS; public Vector3 headOffset; private Vector3 moveRaw; private float speed; public float yaw; public float roll; private float rotSpeed; public Matrix viewMat; float pitch, pitchMin, pitchMax; public Vector3 lookTarget; //private Vector2 heading; public CompoundBody collisionProxy; private Space mySpace; public Dictionary tileLookup; public ShipTile3d spawnPos; public ShipTile3d helmPos; public ShipTile3d camPos; public Vector3 zeroZero; public int ID; //public InteractiveActor helm; public List interactiveActors; public List actors; public Ship3d(Space globalSpace, Ship sc) { interiorSpace = new Space(); interiorSpace.SimulationSettings.MotionUpdate.Gravity = new Vector3(0, -98.1f, 0); if (Environment.ProcessorCount > 1) { for (int i = 0; i < Environment.ProcessorCount; i++) { interiorSpace.ThreadManager.AddThread(); } interiorSpace.UseMultithreadedUpdate = true; } interiorSpace.SimulationSettings.CollisionResponse.Iterations = 5; interiorSpace.SimulationSettings.CollisionDetection.CollisionDetectionType = CollisionDetectionType.DiscreteMPRGJK; shipContent = sc; // tiles3d = ShipTile3d.gen3dTiles(shipContent, globalSpace); //MOVED OUT OF STATIC METHOD //USE CENTER OF MASS FOR SHIP HERE //shipCore = new Sphere(tiles3d[0].tileEntity.WorldTransform.Translation, 1, 1); shipCore = new CompoundBody(true); shipCore.IsAlwaysActive = true; shipCore.AngularDamping = 0f; shipCore.LinearDamping = 0f; gen3dTiles(shipContent, shipCore); // spawnPos = new Vector3(shipContent.spawnPos.Y * 6, 1, shipContent.spawnPos.Y * 6 ); //helmPos = new Vector3(shipContent.helmPos.Y * 6, 0.5f, shipContent.helmPos.Y * 6); //camPos = new Vector3(shipContent.camPos.Y * 6, 0.5f, shipContent.camPos.Y * 6); //GET CENTER OF MASS FOR SHIP interactiveActors = new List(); actors = new List(); globalSpace.Add(shipCore); //ASSIGN TILE ENTITIES AS CHILDREN TO CORE foreach (ShipTile3d st3d in tiles3d) { st3d.tileProxy.BecomeDynamic(10.0f); shipCore.AddBody(st3d.tileProxy); } //INPUT INIT STUFF speed = 1.0f; // 0.01f; rotSpeed = 2f; yaw = 0f; oldGPS = new GamePadState(); newGPS = new GamePadState(); pitchMax = 80.0f; pitchMin = -80.0f; pitch = 0f; headOffset = new Vector3(0f, 15f, 0f); } private void gen3dTiles(Ship shipData, CompoundBody core) { tiles3d = new List(); tileLookup = new Dictionary(); Vector3 tilePos; ShipTile tempTile; ShipTile3d st3d; Entity tempEnt; int x, y; for (int lcv = 0; lcv < shipData.shipTiles[0].Length; lcv++) { if (shipData.shipTiles[0][lcv] != null) { y = lcv % shipData.sizeX; x = lcv / shipData.sizeX; tempTile = shipData.shipTiles[0][lcv]; tilePos = new Vector3(x * 6 - (shipData.sizeX * 3), -3, y * 6 - (shipData.sizeY * 3)); tempEnt = new Box(tilePos + new Vector3(0, 3, 0), 6, 6, 6); // space.Add(tempEnt); st3d = new ShipTile3d(tempEnt, tempTile, new Point(x,y)); if (shipData.spawnPos.Equals(new Point(x, y))) spawnPos = st3d; if (shipData.helmPos.Equals(new Point(x, y))) helmPos = st3d; if (shipData.camPos.Equals(new Point(x, y))) camPos = st3d; tiles3d.Add(st3d); tileLookup.Add(new Point(x, y), st3d); // st3d.tileProxy.BecomeDynamic(10.0f); // shipCore.AddBody(st3d.tileProxy); } } //if ((camPos == null) || (spawnPos == null) || (helmPos == null)) //{ int i = 1; } //return st3dList; } public void updateInput(GamePadState gps, GameTime elapsed) //TODO: incorporate elapsed time { float deltaT = (float)elapsed.ElapsedGameTime.TotalMilliseconds; oldGPS = newGPS; newGPS = gps; pitch = -newGPS.ThumbSticks.Right.Y * deltaT * rotSpeed; yaw = -newGPS.ThumbSticks.Right.X * deltaT * rotSpeed; roll = (newGPS.Triggers.Right - newGPS.Triggers.Left) * -rotSpeed; //pitch = MathHelper.Clamp(pitch, MathHelper.ToRadians(pitchMin), MathHelper.ToRadians(pitchMax)); moveRaw = Vector3.Zero; moveRaw.Z = -newGPS.ThumbSticks.Left.Y * deltaT * speed; moveRaw.X = newGPS.ThumbSticks.Left.X * deltaT * speed; Matrix orientMat = Matrix.CreateFromYawPitchRoll(yaw, pitch, roll); //shipCore.OrientationMatrix* Vector3 tramsformedOrientation = Vector3.Transform(Vector3.Forward, orientMat); Matrix cameraViewRotationMatrix = Matrix.CreateFromQuaternion(shipCore.OrientationQuaternion); //NEED TO ACCOUNT FOR GRAVITY HERE shipCore.ApplyLinearImpulse(Vector3.Transform(moveRaw * 10, cameraViewRotationMatrix)); shipCore.ApplyAngularImpulse(Vector3.Transform(new Vector3(pitch * 100, yaw * 100, roll * 100), shipCore.OrientationMatrix)); } public void teleport(Vector3 destination) { //shipCore.Teleport(destination - shipCore.CenterPosition); shipCore.TeleportTo(destination); } public void updatePosition(Matrix FoRMatrix) { FoRMatrix = shipCore.WorldTransform; Vector3 position; Matrix cameraViewRotationMatrix = Matrix.Identity; Vector3 transformedCameraReference = Vector3.Transform(new Vector3(0.0f, 0.0f, 1.0f), cameraViewRotationMatrix); //position = Vector3.Zero; // (shipCore.WorldTransform).Translation; position = camPos.tileEntity.WorldTransform.Translation; //lookTarget = transformedCameraReference + position; lookTarget = position - transformedCameraReference; //viewMat = Matrix.CreateLookAt(Vector3.Transform(position + headOffset, FoRMatrix), Vector3.Transform(lookTarget + headOffset, FoRMatrix), FoRMatrix.Up); viewMat = Matrix.CreateLookAt(Vector3.Transform(position + headOffset, FoRMatrix), Vector3.Transform(lookTarget + headOffset, FoRMatrix), FoRMatrix.Up); } public void updatePhysics(GameTime gt) { if (interiorSpace != null) interiorSpace.Update(gt); } } }