public GameObject(Game game, Vector3 pos, BasicModel mdl, PhysicsObject pobj) : base(game) { basicmodel = mdl; physobj = pobj; //Default values setPosition(pos); rotation = Quaternion.CreateFromYawPitchRoll(0, 0, 0); scale = 1f; world = Matrix.Identity; }
private bool checkCollision(PhysicsObject p1, PhysicsObject p2) { if (p1.bsphere.Intersects(p2.bsphere)) return true; return false; }
public void addPhysicsObject(PhysicsObject pobj) { physobjs.Add(pobj); }
public void addCollisionEvent(PhysicsObject p1, PhysicsObject p2) { int[] pindex = new int[2] {physobjs.IndexOf(p1), physobjs.IndexOf(p2)}; collisionEvents.Add(pindex); //Add list of physics objects to collision events }
//There is an issue if p1 is moving perpendicularly to p. private void resolveCollision(PhysicsObject p1, PhysicsObject p2) { Vector3 p = p1.position - p2.position; float r1 = p1.bsphere.Radius; float r2 = p2.bsphere.Radius; float R = p.Length(); p.Normalize(); float vd = -Vector3.Dot(p, p1.velocity) + Vector3.Dot(p, p2.velocity); float pd = 0; //Distance for each object to be moved //To stop it from recolliding each frame, give objects a distance to move = penetration distance. //Value is increased slightly to stop objects getting stuck to each other if (p1.isStatic || p2.isStatic) //Move the non-static object the full distance pd = 1.00f * Math.Abs(r1 + r2 - R); else pd = 0.50f * Math.Abs(r1 + r2 - R); //Move both objects half the distance p1.velocity += 0.8f * vd * p; p2.velocity += 0.8f * vd * -p; if (!p1.isStatic) { p1.position += p * pd; p2.velocity += 0.8f * vd * p; } if (!p2.isStatic) { p2.position += -p * pd; p1.velocity += 0.8f * vd * -p; } }
//This method of handling the collisionEventType will be changed in the future. A collisionEvent will be an object of its own. //The response to a registeredCollisionEvent will be handled manually as above. private int collisionEventType(PhysicsObject[] pindex) { if (pindex.Contains(enemy.body.physobj) && pindex.Contains(player.fist.physobj)) return 0; if (pindex.Contains(player.body.physobj) && pindex.Contains(enemy.fist.physobj)) return 1; return -1; }
/// <summary> /// Create a PhysicsObject based on position and bounding sphere. This will exist in the GameWorld and does not need a BasicModel to be simulated. /// </summary> /// <param name="pos"></param> /// <param name="bsphere"></param> /// <returns></returns> public PhysicsObject createPhysicsObject(Vector3 pos, BoundingSphere bsphere) { PhysicsObject pobj = new PhysicsObject(pos, bsphere); physworld.addPhysicsObject(pobj); return pobj; }