public void collideSAT(cGameObject obj, float step_time, bool resolve = true, Action on_collision = null) { // check collisions with world List <AABB> wallsPossibleColliding = this.getCollidableBlocks(obj.Bounds); // we must check this, because we need to iterate through the possible // colliding tiles from other direction according to this condition if (obj.Velocity.X > 0.0f) { for (int i = 0; i < wallsPossibleColliding.Count; i++) { cGameObject wallObject = cGameObject.MakeWall(wallsPossibleColliding[i]); if (cSatCollision.checkAndResolve(obj, wallObject, step_time, resolve)) { on_collision?.Invoke(); break; } } return; } // we have to iterate from "end" to the "start" in order to have the last colliding block below us for (int i = wallsPossibleColliding.Count - 1; i >= 0; i--) { cGameObject wallObject = cGameObject.MakeWall(wallsPossibleColliding[i]); if (cSatCollision.checkAndResolve(obj, wallObject, step_time, resolve)) { on_collision?.Invoke(); break; } } }
public cMachineGun(cGameObject owner, int firing_frequency) : base(owner, firing_frequency) { this.maxAmmo = 300; this.ammo = maxAmmo; this.magazineCapacity = 30; this.timeToReload = 1.5f; }
public static bool checkAndResolve(cGameObject objA, cGameObject objB, float dt, bool resolve = false) { float t = dt; // 1.0f / 200.0f Vector2f N = new Vector2f(0.0f, 0.0f); //objA.GetBoundingBox().m_Center - objB.GetBoundingBox().m_Center; Vector2f RelPos = objA.Bounds.center - objB.Bounds.center; //objA.Position - objB.Position; Vector2f RelVel = objA.Velocity - objB.Velocity; if (Collide(objA.HitCollisionRect.getLocalVertices(), 4, objB.HitCollisionRect.getLocalVertices(), 4, RelPos, RelVel, ref N, ref t)) { if (resolve) { handleCollision(objA, objB, N, t); } return(true); } return(false); }
public cWeapon(cGameObject owner, int firing_frequency) { this.owner = owner; this.firingFrequency = firing_frequency; this.regulator = new cRegulator(); this.regulator.resetByFrequency(firing_frequency); this.spread = (float)cAppMath.DegressToRadian(2); }
public void collideParticleSAT(Particle particle, float step_time, bool physics = true) { cGameObject obj = cGameObject.fromParticle(particle); this.collideSAT(obj, step_time); particle.Pos = obj.Position; particle.Vel = obj.Velocity; }
public cWeapon(cGameObject owner, int firing_frequency, string bullet_breed_id) { this.owner = owner; this.firingFrequency = firing_frequency; this.regulator = new cRegulator(); this.regulator.resetByFrequency(firing_frequency); this.spread = (float)AppMath.DegressToRadian(2); this.bulletsPerShot = 1; this.bulletBreedID = bullet_breed_id; }
public cMachineGun(cGameObject owner, int firing_frequency, string bullet_breed_id = "simple-bullet") : base(owner, firing_frequency, bullet_breed_id) { this.maxAmmo = 300; this.currentAmmo = maxAmmo; this.magazineCapacity = 30; this.timeToReload = 1.5f; this.spread = (float)AppMath.DegressToRadian(4); this.bulletsPerShot = 1; }
public static void handleCollision(cGameObject objA, cGameObject objB, Vector2f N, float t) { if (t < 0.0f) { ProcessOverlap(objA, objB, N * (-t)); } else { ProcessCollision(objA, objB, N, t); } }
public static bool checkOnly(cGameObject objA, cGameObject objB, ref float dt, out Vector2f mtd) { mtd = new Vector2f(0.0f, 0.0f); Vector2f RelPos = objA.Bounds.center - objB.Bounds.center; Vector2f RelVel = objA.Velocity - objB.Velocity; return(Collide(objA.HitCollisionRect.getLocalVertices(), 4, objB.HitCollisionRect.getLocalVertices(), 4, RelPos, RelVel, ref mtd, ref dt)); }
// two objects collided at time t. stop them at that time private static void ProcessOverlap(cGameObject objA, cGameObject objB, Vector2f xMTD) { Vector2f mtd = xMTD; float time = (1.0f / 200.0f); // 0.0166f; // 1.0f / 120.0f; //cAppMath.Vec2Truncate(ref mtd, 0.1f); //mtd.X = Math.Clamp<float>(mtd.X, 3.0f, -3.0f); //mtd.Y = Math.Clamp<float>(mtd.Y, 3.0f, -3.0f); if (objA.Unmovable) { objB.MoveBy(mtd * -time); /* * Vector2f p = objB.Position; * p.X -= mtd.X * time; // *0.5f; * p.Y -= mtd.Y * time; // *0.5f; * objB.Position = p; */ } else if (objB.Unmovable) { objA.MoveBy(mtd * time); //objA.Position.X += mtd.X * 0.0166f;// * 0.5f; //objA.Position.Y += mtd.Y * 0.0166f;// * 0.5f; //return; } else { objA.MoveBy(mtd * time); objB.MoveBy(mtd * -time); /* * objA.Position.X += mtd.X * 0.5f; * objA.Position.Y += mtd.Y * 0.5f; * * objB.Position.X -= mtd.X * 0.5f; * objB.Position.Y -= mtd.Y * 0.5f; */ } Vector2f N = mtd; AppMath.Vec2Normalize(ref N); ProcessCollision(objA, objB, N, 0.0f); }
/// <summary> /// /// </summary> /// <param name="A">First gameObject</param> /// <param name="B">Second gameObject</param> /// <param name="onlyFirst">only seperate the first object</param> public static void SeparateEntites(cGameObject A, cGameObject B, bool onlyFirst = false) { Vector2f to = A.GetCenterPos() - B.GetCenterPos(); float dist = (float)AppMath.Vec2Length(to); float amountOfOverlap = (dist - A.BoundingRadius - B.BoundingRadius); // *0.5f Vector2f offset = amountOfOverlap * (to / dist); if (onlyFirst) { A.Position -= offset; return; } offset *= 0.5f; A.Position -= offset; B.Position += offset; }
// two objects overlapped. push them away from each other private static void ProcessCollision(cGameObject objA, cGameObject objB, Vector2f N, float t) { //Vector2f D = m_xDisplacement - xBody.m_xDisplacement; Vector2f D = objA.Velocity - objB.Velocity; float n = AppMath.Vec2Dot(D, N); Vector2f Dn = N * n; Vector2f Dt = D - Dn; if (n > 0.0f) { Dn = new Vector2f(0, 0); } float dt = AppMath.Vec2Dot(Dt, Dt); float CoF = Constants.FRICTION; if (dt < Constants.GLUE * Constants.GLUE) { CoF = 1.01f; } //D = -(1.0f + Constants.RESTITUTION) * Dn - (CoF) * Dt; D.X = -(1.0f + Constants.RESTITUTION) * Dn.X - (CoF) * Dt.X; D.Y = -(1.0f + Constants.RESTITUTION) * Dn.Y - (CoF) * Dt.Y; float m0 = objA.Mass; float m1 = objB.Mass; float m = m0 + m1; float r0 = m0 / m; //m float r1 = m1 / m; //m if (m0 > 0.0f) { objA.AddVelocity(D * r0); /* * Vector2f velA = objA.Velocity + D * r0; * objA.Velocity = velA; */ /* * objA.Velocity.X += D.X * r0; * objA.Velocity.Y += D.Y * r0; */ } if (m1 > 0.0f) { objB.AddVelocity(D * (-r1)); /* * Vector2f velB = objB.Velocity + D * -r1; * objB.Velocity = velB; */ /* * objB.Velocity.X += D.X * -r1; * objB.Velocity.Y += D.Y * -r1; */ } }
public cShotgun(cGameObject owner, int firing_frequency, string bullet_breed_id = "simple-bullet") : base(owner, firing_frequency, bullet_breed_id) { this.spread = (float)AppMath.DegressToRadian(3); this.bulletsPerShot = 4; }
public void AddBullet(cGameObject owner, Vector2f pos, Vector2f direction) { cBullet bullet = new cBullet(owner, pos, direction); bullets.Add(bullet); }
public cShotgun(cGameObject owner, int firing_frequency) : base(owner, firing_frequency) { this.spread = (float)cAppMath.DegressToRadian(4); }