public override void OnUpdate() { base.OnUpdate (); float mult = (float)Forgottenvoxels.GetInstance ().TargetUpdateFrequency; float grav = gravity / mult, drag = airDrag / mult; if (!flying) { velocity.X *= drag; velocity.Y -= grav; velocity.Z *= drag; } else { velocity *= drag; } if (velocity.X != 0 || velocity.Y != 0 || velocity.Z != 0) { //Collision stuff collisionObject.prePosition = entity.location; collisionObject.preVelocity = velocity; collisionObject = CollisionManager.CollisionCheckWorld (entity, entity.world, collisionObject); entity.location = collisionObject.postPosition; velocity = collisionObject.postVelocity; //End collision stuff } }
public PhysicsComponent() { velocity = Vector3.Zero; flying = false; collisionObject = new CollisionObject (); collisionObject.collisionBox = collisionBox; }
public static CollisionObject TrsyCollide(CollisionObject objectA, CollisionBox[] objects) { bool moveX = true, moveY = true, moveZ = true; //Get some collision boxes CollisionBox nextFrameBox = objectA.velocityBoxOffset, currentbox = objectA.collisionBox; //Get the individual velocity vectors Vector3 stepX = new Vector3(objectA.preVelocity.X, 0, 0), stepY = new Vector3(0, objectA.preVelocity.Y, 0), stepZ = new Vector3(0, 0, objectA.preVelocity.Z); Vector3 prelocation = objectA.prePosition + objectA.collisionBox.offset; nextFrameBox.origin = prelocation; currentbox.origin = prelocation; //Set post stuff to pre stuff objectA.postPosition = objectA.prePosition; objectA.postVelocity = objectA.preVelocity; for (int i = 0; i < objects.Length; i++) { CollisionBox objectB = objects [i]; nextFrameBox.origin = prelocation + stepX; if(Intersects (nextFrameBox, objectB)){ moveX = false; } nextFrameBox.origin = prelocation + stepY; if(Intersects (nextFrameBox, objectB)){ moveY = false; } nextFrameBox.origin = prelocation + stepZ; if(Intersects (nextFrameBox, objectB)){ moveZ = false; } } const float mult = 0.1f; if (moveX) { objectA.postPosition.X += objectA.preVelocity.X; } else { objectA.postVelocity.X *= mult; } if (moveY) { objectA.postPosition.Y += objectA.preVelocity.Y; } else { objectA.postVelocity.Y *= mult; } if (moveZ) { objectA.postPosition.Z += objectA.preVelocity.Z; } else { objectA.postVelocity.Z *= mult; } return objectA; }
public static CollisionObject MoveObject(bool[] moveArray, CollisionObject obj) { if (moveArray.Length != 3) { Console.WriteLine("Array length was above limits! {0}", moveArray.Length); return obj; } const float mult = 0.1f; //Set post values to pre values obj.postPosition = obj.prePosition; obj.postVelocity = obj.preVelocity; //X Checking if (moveArray[0]) obj.postPosition.X += obj.preVelocity.X; else obj.postVelocity.X *= mult; //Y Checking if (moveArray[1]) obj.postPosition.Y += obj.preVelocity.Y; else obj.postVelocity.Y *= mult; //Z Checking if (moveArray[2]) obj.postPosition.Z += obj.preVelocity.Z; else obj.postVelocity.Z *= mult; return obj; }
public static CollisionObject CollisionCheckWorld(Entity ent, World world, CollisionObject collisionObject) { List<CollisionObject> objects = new List<CollisionObject>(); List<CollisionBox> boxes = new List<CollisionBox>(); //The size of our search Vector3 addvec = (Vector3.One * 2); for(int x = (int)-addvec.X; x < (int)addvec.X; x++) { for(int y = (int)-addvec.Y; y < (int)addvec.Y; y++) { for(int z = (int)-addvec.X; z < (int)addvec.Z; z++) { Block block = world.GetBlock(collisionObject.prePosition + new Vector3(x,y,z)); if(block.collide) boxes.Add(world.GetCollisionBox(collisionObject.prePosition + new Vector3(x,y,z))); } } } foreach(Entity e in EntityManager.instance.GetEntitiesInRadius(ent, collisionObject.prePosition, 2)) { if(e.HasComponentOfType(typeof(PhysicsComponent))) { PhysicsComponent physComponent = (PhysicsComponent)e.GetComponentOfType(typeof(PhysicsComponent)); CollisionObject collObj = physComponent.collisionObject; objects.Add( collObj ); } } return CollisionManager.Collision(collisionObject, objects.ToArray(), boxes.ToArray()); }
public static CollisionObject Collision(CollisionObject ourObject, CollisionObject[] objects, CollisionBox[] boxes) { Stopwatch sw = new Stopwatch(); sw.Start(); bool[] move = new bool[3]{true, true, true}; foreach(CollisionBox box in boxes) { bool[] movement; CollideStatic(ourObject, box, out ourObject, out movement); move = CompareMovement(movement, move); } for(int i = 0; i < objects.Length; i++) { CollisionObject obj = objects[i]; bool[] movement; CollideMoving(ourObject, obj, out ourObject, out obj, out movement); move = CompareMovement(movement, move); } CollisionObject returnObject = MoveObject(move, ourObject); sw.Stop(); Total += sw.ElapsedMilliseconds; Runs += 1; return returnObject; }
public static void CollideStatic(CollisionObject movingObject, CollisionBox staticBox, out CollisionObject outObject, out bool[] move) { bool[] movement = new bool[]{true, true, true}; CollisionBox nextFrameBox = movingObject.velocityBoxOffset; Vector3 stepX = new Vector3(movingObject.preVelocity.X, 0, 0), stepY = new Vector3(0, movingObject.preVelocity.Y, 0), stepZ = new Vector3(0, 0, movingObject.preVelocity.Z); Vector3 prelocation = movingObject.prePosition + movingObject.collisionBox.offset; nextFrameBox.origin = prelocation + stepX; if(Intersects (nextFrameBox, staticBox)){ movement[0] = false; } nextFrameBox.origin = prelocation + stepY; if(Intersects (nextFrameBox, staticBox)){ movement[1] = false; } nextFrameBox.origin = prelocation + stepZ; if(Intersects (nextFrameBox, staticBox)){ movement[2] = false; } outObject = movingObject; move = movement; }
public static void CollideMoving(CollisionObject objectA, CollisionObject objectB, out CollisionObject outObjectA, out CollisionObject outObjectB, out bool[] move) { CollideStatic(objectA, objectB.velocityBoxOffset, out outObjectA, out move); outObjectB = objectB; }