/// <summary> /// Are ellipses colliding? /// </summary> /// <param name="v1">First ellipse center point</param> /// <param name="size1">First ellipse size</param> /// <param name="angle1">First ellipse angle</param> /// <param name="v2">Second ellipse center point</param> /// <param name="size2">Second ellipse size</param> /// <param name="angle2">Second ellipse angle</param> /// <param name="depth">Penetration depth (out)</param> /// <param name="point1">First ellipse intersection point</param> /// <param name="point2">Second ellipse intersection point</param> /// <returns>True if colliding</returns> public static bool IsEllipseCollision( Vector v1, Vector size1, double angle1, Vector v2, Vector size2, double angle2, out CollisionState state) { // Direction vector from first ellipse center to second ellipse center Vector direction = v2.MinusNew( v1 ); double angle = direction.Angle; // Point of collision on first ellipse state.Point1 = EllipsePoint( v1, size1, angle1, angle ); // Point of collision on second ellipse state.Point2 = EllipsePoint( v2, size2, angle2, angle + Math.PI ); // Penetration depth state.Depth = state.Point2.MinusNew( state.Point1 ); // If first ellipse radius (in direction of ray) + second ellipse radius is greater than the distance // between the two ellipses, we have a collision state.Colliding = ( v1.Distance( state.Point1 ) + v2.Distance( state.Point2 ) > v1.Distance( v2 ) ); return state.Colliding; }
public virtual void ResolveRectangleCollision( RectangleParticle p, Engine engine, ref CollisionState state ) { }
public virtual void ResolveCircleCollision( CircleParticle p, Engine engine, ref CollisionState state ) { }
public virtual void CheckCollision( IParticle particle, Engine engine, ref CollisionState state ) { }
public override void CheckCollision( IParticle particle, Engine engine, ref CollisionState state ) { //particle.ResolveRectangleCollision( this, engine ); }
private void CheckCollisions() { collisions.Clear(); foreach ( ISurface surface in surfaces ) { if ( surface.Active ) { foreach ( Particle particle in particles ) { particle.CheckCollision( surface, this ); } } } Dictionary<string, object> compared = new Dictionary<string, object>(); foreach ( Particle particle1 in particles ) { foreach ( Particle particle2 in particles ) { if ( particle1 != particle2 ) { string key = ( particle1.Index < particle2.Index ) ? particle1.Index.ToString() + "," + particle2.Index.ToString() : particle2.Index.ToString() + "," + particle1.Index.ToString(); if ( !compared.ContainsKey( key ) ) { compared.Add( key, null ); CollisionState state = new CollisionState(); particle2.CheckCollision( particle1, this, ref state ); if ( state.Colliding ) collisions.Add( state ); } } } } }
public override void CheckCollision( IParticle particle, Engine engine, ref CollisionState state ) { // HACK: here i need not to collide wheels with body particles if (this is Wheel && !(particle is Wheel)) return; if (particle is Wheel && !(this is Wheel)) return; particle.ResolveCircleCollision(this, engine, ref state); }
public override void ResolveRectangleCollision( RectangleParticle p, Engine engine, ref CollisionState state ) { throw new Exception( "The method or operation is not implemented." ); }
public override void ResolveCircleCollision( CircleParticle p, Engine engine, ref CollisionState state ) { if ( Collision.IsEllipseCollision( curr, Size, 0, p.curr, p.Size, 0, out state ) ) { Vector normal = state.Depth.Clone().Normalize(); Vector vel1 = curr.MinusNew( prev ); Vector vel2 = p.curr.MinusNew( p.prev ); p.prev = p.curr.PlusNew( state.Depth ); prev = curr.MinusNew( state.Depth ); } }