public bool CollideAgainstBounce(AxisAlignedRectangle rectangle, float thisMass, float otherMass, float elasticity) { #if DEBUG if (thisMass == 0 && otherMass == 0) { throw new ArgumentException("Both masses cannot be 0. For equal masses pick a non-zero value"); } #endif if (CollideAgainstMove(rectangle, thisMass, otherMass)) { PositionedObject thisTopParent = this.TopParent; PositionedObject otherTopParent = rectangle.TopParent; Vector2 collisionNormal = this.LastMoveCollisionReposition; if (otherMass == 0) { collisionNormal = rectangle.LastMoveCollisionReposition * -1; } ShapeManager.ApplyBounce(thisTopParent, otherTopParent, thisMass, otherMass, elasticity, ref collisionNormal); return(true); } return(false); }
/// <summary> /// Adjusts the calling Circle's position (or its parent if attached) so that the circle is fully-contained /// in the argument AxisAlignedRectangle. /// </summary> /// <param name="otherAxisAlignedRectangle">The rectangle to keep the circle inside of.</param> public void KeepThisInsideOf(AxisAlignedRectangle otherAxisAlignedRectangle) { Vector2 repositionVector; repositionVector.X = 0; repositionVector.Y = 0; this.ForceUpdateDependencies(); if (this.X - this.Radius < otherAxisAlignedRectangle.X - otherAxisAlignedRectangle.ScaleX) { repositionVector.X = (otherAxisAlignedRectangle.X - otherAxisAlignedRectangle.ScaleX + this.Radius) - this.X; } if (this.X + this.Radius > otherAxisAlignedRectangle.X + otherAxisAlignedRectangle.ScaleX) { repositionVector.X = (otherAxisAlignedRectangle.X + otherAxisAlignedRectangle.ScaleX - this.Radius) - this.X; } if (this.Y - this.Radius < otherAxisAlignedRectangle.Y - otherAxisAlignedRectangle.ScaleY) { repositionVector.Y = (otherAxisAlignedRectangle.Y - otherAxisAlignedRectangle.ScaleY + this.Radius) - this.Y; } if (this.Y + this.Radius > otherAxisAlignedRectangle.Y + otherAxisAlignedRectangle.ScaleY) { repositionVector.Y = (otherAxisAlignedRectangle.Y + otherAxisAlignedRectangle.ScaleY - this.Radius) - this.Y; } PositionedObject topParent = this.TopParent; topParent.Position.X += repositionVector.X; topParent.Position.Y += repositionVector.Y; }
private static List <ContactPoint> GetContactPoints(Polygon polygon, AxisAlignedRectangle rectangle) { Segment[] firstSegments = GetSegments(polygon); Segment[] secondSegments = GetSegments(rectangle); List <ContactPoint> contactPoints = GetContactPoints(firstSegments, secondSegments); return(contactPoints); }
/// <summary> /// Returns whether this instance collides with the argument AxisAlignedRectangle. /// </summary> /// <param name="rectangle">The AxisAlignedRectangle to test collision against.</param> /// <returns>Whether a collision has occurred.</returns> #endregion public bool CollideAgainst(AxisAlignedRectangle rectangle) { UpdateDependencies(TimeManager.CurrentTime); rectangle.UpdateDependencies(TimeManager.CurrentTime); // Get world-positioned segment Segment a = AsSegment(); // Check if one of the segment's endpoints is inside the rectangle Vector3 endpoint = new Vector3( (float)a.Point1.X, (float)a.Point1.Y, 0f); if (rectangle.IsPointInside(ref endpoint)) { mLastCollisionPoint = new Point(ref endpoint); return(true); } endpoint = new Vector3( (float)a.Point2.X, (float)a.Point2.Y, 0f); if (rectangle.IsPointInside(ref endpoint)) { mLastCollisionPoint = new Point(ref endpoint); return(true); } // Check if the segment intersects any of the rectangle's edges // Here, prepare rectangle's corner points Point tl = new Point( rectangle.Position.X - rectangle.ScaleX, rectangle.Position.Y + rectangle.ScaleY); Point tr = new Point( rectangle.Position.X + rectangle.ScaleX, rectangle.Position.Y + rectangle.ScaleY); Point bl = new Point( rectangle.Position.X - rectangle.ScaleX, rectangle.Position.Y - rectangle.ScaleY); Point br = new Point( rectangle.Position.X + rectangle.ScaleX, rectangle.Position.Y - rectangle.ScaleY); Point tempPoint; // Test if any of the edges intersect the segment // (this will short-circuit on the first true test) if (a.Intersects(new Segment(tl, tr), out tempPoint) || a.Intersects(new Segment(tl, bl), out tempPoint) || a.Intersects(new Segment(bl, br), out tempPoint) || a.Intersects(new Segment(tr, br), out tempPoint)) { mLastCollisionPoint = tempPoint; return(true); } // No collision return(false); }
private static Segment[] GetSegments(AxisAlignedRectangle rectangle) { Segment[] rectangleSegments = new Segment[4]; rectangleSegments[0] = new Segment(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Top); // top rectangleSegments[1] = new Segment(rectangle.Right, rectangle.Top, rectangle.Right, rectangle.Bottom); // right rectangleSegments[2] = new Segment(rectangle.Right, rectangle.Bottom, rectangle.Left, rectangle.Bottom); // bottom rectangleSegments[3] = new Segment(rectangle.Left, rectangle.Bottom, rectangle.Left, rectangle.Top); // left return(rectangleSegments); }
private static List <Vector3> GetAbsoluteVertices(AxisAlignedRectangle rectangle) { List <Vector3> otherVertices = new List <Vector3>(4); otherVertices.Add(new Vector3(rectangle.Left, rectangle.Top, 0)); otherVertices.Add(new Vector3(rectangle.Right, rectangle.Top, 0)); otherVertices.Add(new Vector3(rectangle.Right, rectangle.Bottom, 0)); otherVertices.Add(new Vector3(rectangle.Left, rectangle.Bottom, 0)); return(otherVertices); }
protected virtual void InitializeEntity(bool addToManagers) { LoadStaticContent(ContentManagerName); mAxisAlignedRectangleInstance = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mAxisAlignedRectangleInstance.Name = "mAxisAlignedRectangleInstance"; PostInitialize(); if (addToManagers) { AddToManagers(null); } }
public PositionedObjectList <FlatRedBall.Math.Geometry.AxisAlignedRectangle> ToAxisAlignedRectangleList() { PositionedObjectList <FlatRedBall.Math.Geometry.AxisAlignedRectangle> listToReturn = new PositionedObjectList <FlatRedBall.Math.Geometry.AxisAlignedRectangle>(); foreach (AxisAlignedRectangleSave rectangleSave in AxisAlignedRectangleSaves) { FlatRedBall.Math.Geometry.AxisAlignedRectangle rectangle = rectangleSave.ToAxisAlignedRectangle(); listToReturn.Add(rectangle); } return(listToReturn); }
protected virtual void InitializeEntity(bool addToManagers) { // Generated Initialize LoadStaticContent(ContentManagerName); mCollision = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mCollision.Name = "mCollision"; PostInitialize(); if (addToManagers) { AddToManagers(null); } }
private static int GetPointToStartAt(Polygon polygon, AxisAlignedRectangle rectangle) { int firstPointToStartAt = -1; for (int i = 0; i < polygon.mVertices.Length - 1; i++) { if (!rectangle.IsPointInside(ref polygon.mVertices[i].Position)) { firstPointToStartAt = i; break; } } return(firstPointToStartAt); }
protected override void InitializeEntity(bool addToManagers) { // Generated Initialize LoadStaticContent(ContentManagerName); SpriteInstance = new FlatRedBall.Sprite(); SpriteInstance.Name = "SpriteInstance"; HandSprite = new FlatRedBall.Sprite(); HandSprite.Name = "HandSprite"; mGunRectangle = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mGunRectangle.Name = "mGunRectangle"; mFedoraSprite = new FlatRedBall.Sprite(); mFedoraSprite.Name = "mFedoraSprite"; base.InitializeEntity(addToManagers); }
protected virtual void InitializeEntity(bool addToManagers) { LoadStaticContent(ContentManagerName); mAxisAlignedRectangleInstance = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mAxisAlignedRectangleInstance.Name = "mAxisAlignedRectangleInstance"; mBallCatchArea = new FlatRedBall.Math.Geometry.Circle(); mBallCatchArea.Name = "mBallCatchArea"; SpriteInstance = new FlatRedBall.Sprite(); SpriteInstance.Name = "SpriteInstance"; // this provides default controls for the platformer using either keyboad or 360. Can be overridden in custom code: this.InitializeInput(); BeforeGroundMovementSet += (newValue) => { if (mGroundMovement != null && mGroundMovement == mValuesJumpedWith) { mValuesJumpedWith = newValue; } }; BeforeAirMovementSet += (newValue) => { if (mAirMovement != null && mAirMovement == mValuesJumpedWith) { mValuesJumpedWith = newValue; } }; BeforeAfterDoubleJumpSet += (newValue) => { if (mAfterDoubleJump != null && mAfterDoubleJump == mValuesJumpedWith) { mValuesJumpedWith = newValue; } }; AfterGroundMovementSet += (not, used) => UpdateCurrentMovement(); AfterAirMovementSet += (not, used) => UpdateCurrentMovement(); AfterAfterDoubleJumpSet += (not, used) => UpdateCurrentMovement(); PostInitialize(); if (addToManagers) { AddToManagers(null); } }
protected override void InitializeEntity(bool addToManagers) { LoadStaticContent(ContentManagerName); SpriteInstance = new FlatRedBall.Sprite(); SpriteInstance.Name = "SpriteInstance"; mAxisAlignedRectangleInstance = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mAxisAlignedRectangleInstance.Name = "mAxisAlignedRectangleInstance"; LightSpriteInstance = new FlatRedBall.Sprite(); LightSpriteInstance.Name = "LightSpriteInstance"; AimSpriteInstance = new FlatRedBall.Sprite(); AimSpriteInstance.Name = "AimSpriteInstance"; PivotPoint = new FlatRedBall.PositionedObject(); PivotPoint.Name = "PivotPoint"; base.InitializeEntity(addToManagers); }
public static void Merge(Polygon polygon, AxisAlignedRectangle rectangle) { List <ContactPoint> contactPoints = GetContactPoints(polygon, rectangle); int firstPointToStartAt = GetPointToStartAt(polygon, rectangle); if (firstPointToStartAt == -1) { throw new NotImplementedException(); // return a polygon that is the same shape as the rectangle } List <Vector3> thisVertices = GetAbsoluteVertices(polygon); List <Vector3> otherVertices = GetAbsoluteVertices(rectangle); SetPointsFromContactPointsAndVertices(polygon, null, contactPoints, firstPointToStartAt, thisVertices, otherVertices); }
protected virtual void InitializeEntity(bool addToManagers) { LoadStaticContent(ContentManagerName); SpriteInstance = new FlatRedBall.Sprite(); SpriteInstance.Name = "SpriteInstance"; TextInstance = new FlatRedBall.Graphics.Text(); TextInstance.Name = "TextInstance"; mHitbox = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mHitbox.Name = "mHitbox"; WeaponInstance = new Pirates.Entities.WeaponEntity(ContentManagerName, false); WeaponInstance.Name = "WeaponInstance"; PostInitialize(); if (addToManagers) { AddToManagers(null); } }
public override void Initialize(bool addToManagers) { LoadStaticContent(ContentManagerName); RectsList = new FlatRedBall.Math.PositionedObjectList <FlatRedBall.Math.Geometry.AxisAlignedRectangle>(); RectsList.Name = "RectsList"; mRectMain = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectMain.Name = "mRectMain"; mRectSide1 = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectSide1.Name = "mRectSide1"; mRectSide2 = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectSide2.Name = "mRectSide2"; mRectBelow = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectBelow.Name = "mRectBelow"; mRectBelow2 = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectBelow2.Name = "mRectBelow2"; mRectTop = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectTop.Name = "mRectTop"; mRectTop2 = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectTop2.Name = "mRectTop2"; RectsListUnused = new FlatRedBall.Math.PositionedObjectList <FlatRedBall.Math.Geometry.AxisAlignedRectangle>(); RectsListUnused.Name = "RectsListUnused"; mRectTopCenter = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectTopCenter.Name = "mRectTopCenter"; mRectBelowCenter = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectBelowCenter.Name = "mRectBelowCenter"; ArrowsInstance = new TestBed.Entities.Arrows(ContentManagerName, false); ArrowsInstance.Name = "ArrowsInstance"; mRectLeftCenter = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectLeftCenter.Name = "mRectLeftCenter"; mRectRightCenter = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectRightCenter.Name = "mRectRightCenter"; PostInitialize(); base.Initialize(addToManagers); if (addToManagers) { AddToManagers(); } }
public override void Initialize(bool addToManagers) { LoadStaticContent(ContentManagerName); mCenterH = new FlatRedBall.Math.Geometry.Polygon(); mCenterH.Name = "mCenterH"; mCenterV = new FlatRedBall.Math.Geometry.Polygon(); mCenterV.Name = "mCenterV"; RectsList = new FlatRedBall.Math.PositionedObjectList <FlatRedBall.Math.Geometry.AxisAlignedRectangle>(); RectsList.Name = "RectsList"; mRect1Main = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRect1Main.Name = "mRect1Main"; mRect2InnerTouching = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRect2InnerTouching.Name = "mRect2InnerTouching"; mRectOuterTouching2 = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRectOuterTouching2.Name = "mRectOuterTouching2"; mRect3InnerTouching = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRect3InnerTouching.Name = "mRect3InnerTouching"; mRect4InnerTouching = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRect4InnerTouching.Name = "mRect4InnerTouching"; mRect5InnerTouching = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRect5InnerTouching.Name = "mRect5InnerTouching"; mRect6OuterTouching = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRect6OuterTouching.Name = "mRect6OuterTouching"; mRect7OuterTouching = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRect7OuterTouching.Name = "mRect7OuterTouching"; mRect8OuterNotTouching = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRect8OuterNotTouching.Name = "mRect8OuterNotTouching"; mRect8OuterNotTouching2 = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRect8OuterNotTouching2.Name = "mRect8OuterNotTouching2"; mRect6OuterTouching2 = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mRect6OuterTouching2.Name = "mRect6OuterTouching2"; PostInitialize(); base.Initialize(addToManagers); if (addToManagers) { AddToManagers(); } }
public float DistanceTo(AxisAlignedRectangle rectangle) { float finalDistance = float.MaxValue; finalDistance = System.Math.Min(finalDistance, DistanceTo(rectangle.Left, rectangle.Top)); finalDistance = System.Math.Min(finalDistance, DistanceTo(rectangle.Left, rectangle.Bottom)); finalDistance = System.Math.Min(finalDistance, DistanceTo(rectangle.Right, rectangle.Top)); finalDistance = System.Math.Min(finalDistance, DistanceTo(rectangle.Right, rectangle.Bottom)); Segment rectSegment; rectSegment = new Segment(rectangle.Left, rectangle.Top, rectangle.Left, rectangle.Bottom); finalDistance = System.Math.Min(finalDistance, DistanceTo(rectSegment)); rectSegment = new Segment(rectangle.Left, rectangle.Top, rectangle.Right, rectangle.Top); finalDistance = System.Math.Min(finalDistance, DistanceTo(rectSegment)); rectSegment = new Segment(rectangle.Right, rectangle.Top, rectangle.Right, rectangle.Bottom); finalDistance = System.Math.Min(finalDistance, DistanceTo(rectSegment)); rectSegment = new Segment(rectangle.Left, rectangle.Bottom, rectangle.Right, rectangle.Bottom); finalDistance = System.Math.Min(finalDistance, DistanceTo(rectSegment)); return(finalDistance); }
public bool CollideAgainstBounce(AxisAlignedRectangle axisAlignedRectangle, float thisMass, float otherMass, float elasticity) { return(axisAlignedRectangle.CollideAgainstBounce(this, otherMass, thisMass, elasticity)); }
public bool CollideAgainstMove(AxisAlignedRectangle axisAlignedRectangle, float thisMass, float otherMass) { return(axisAlignedRectangle.CollideAgainstMove(this, otherMass, thisMass)); }
protected virtual void InitializeEntity(bool addToManagers) { // Generated Initialize LoadStaticContent(ContentManagerName); AnimSprite = AnimationChainListFile; mBody = new FlatRedBall.Math.Geometry.Circle(); EntireScene = EnemySpriteScene.Sprites.FindByName("testsprite32x321").Clone(); mHead = new FlatRedBall.Math.Geometry.Circle(); PathArea = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); PostInitialize(); if (addToManagers) { AddToManagers(null); } }
public bool CollideAgainst(FlatRedBall.TileCollisions.TileShapeCollection shapeCollection, FlatRedBall.Math.Geometry.AxisAlignedRectangle thisCollision, bool isCloudCollision = false) { return(CollideAgainst(() => shapeCollection.CollideAgainstSolid(thisCollision), isCloudCollision)); }
protected override void InitializeEntity(bool addToManagers) { LoadStaticContent(ContentManagerName); mSpriteInstance = new FlatRedBall.Sprite(); mSpriteInstance.Name = "mSpriteInstance"; // Not instantiating for Circle CircleInstance in Entities\Enemies\Sheep1Enemy because properties on the object prevent it LightSprite = new FlatRedBall.Sprite(); LightSprite.Name = "LightSprite"; mAxisAlignedRectangleInstance = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mAxisAlignedRectangleInstance.Name = "mAxisAlignedRectangleInstance"; base.InitializeEntity(addToManagers); if (SpriteInstance.Parent == null) { SpriteInstanceXReset = SpriteInstance.X; } else { SpriteInstanceXReset = SpriteInstance.RelativeX; } if (SpriteInstance.Parent == null) { SpriteInstanceYReset = SpriteInstance.Y; } else { SpriteInstanceYReset = SpriteInstance.RelativeY; } if (SpriteInstance.Parent == null) { SpriteInstanceZReset = SpriteInstance.Z; } else { SpriteInstanceZReset = SpriteInstance.RelativeZ; } if (SpriteInstance.Parent == null) { SpriteInstanceXVelocityReset = SpriteInstance.XVelocity; } else { SpriteInstanceXVelocityReset = SpriteInstance.RelativeXVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceYVelocityReset = SpriteInstance.YVelocity; } else { SpriteInstanceYVelocityReset = SpriteInstance.RelativeYVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceZVelocityReset = SpriteInstance.ZVelocity; } else { SpriteInstanceZVelocityReset = SpriteInstance.RelativeZVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationXReset = SpriteInstance.RotationX; } else { SpriteInstanceRotationXReset = SpriteInstance.RelativeRotationX; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationYReset = SpriteInstance.RotationY; } else { SpriteInstanceRotationYReset = SpriteInstance.RelativeRotationY; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationZReset = SpriteInstance.RotationZ; } else { SpriteInstanceRotationZReset = SpriteInstance.RelativeRotationZ; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationXVelocityReset = SpriteInstance.RotationXVelocity; } else { SpriteInstanceRotationXVelocityReset = SpriteInstance.RelativeRotationXVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationYVelocityReset = SpriteInstance.RotationYVelocity; } else { SpriteInstanceRotationYVelocityReset = SpriteInstance.RelativeRotationYVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationZVelocityReset = SpriteInstance.RotationZVelocity; } else { SpriteInstanceRotationZVelocityReset = SpriteInstance.RelativeRotationZVelocity; } SpriteInstanceAlphaReset = SpriteInstance.Alpha; SpriteInstanceAlphaRateReset = SpriteInstance.AlphaRate; if (LightSprite.Parent == null) { LightSpriteXReset = LightSprite.X; } else { LightSpriteXReset = LightSprite.RelativeX; } if (LightSprite.Parent == null) { LightSpriteYReset = LightSprite.Y; } else { LightSpriteYReset = LightSprite.RelativeY; } if (LightSprite.Parent == null) { LightSpriteZReset = LightSprite.Z; } else { LightSpriteZReset = LightSprite.RelativeZ; } if (LightSprite.Parent == null) { LightSpriteXVelocityReset = LightSprite.XVelocity; } else { LightSpriteXVelocityReset = LightSprite.RelativeXVelocity; } if (LightSprite.Parent == null) { LightSpriteYVelocityReset = LightSprite.YVelocity; } else { LightSpriteYVelocityReset = LightSprite.RelativeYVelocity; } if (LightSprite.Parent == null) { LightSpriteZVelocityReset = LightSprite.ZVelocity; } else { LightSpriteZVelocityReset = LightSprite.RelativeZVelocity; } if (LightSprite.Parent == null) { LightSpriteRotationXReset = LightSprite.RotationX; } else { LightSpriteRotationXReset = LightSprite.RelativeRotationX; } if (LightSprite.Parent == null) { LightSpriteRotationYReset = LightSprite.RotationY; } else { LightSpriteRotationYReset = LightSprite.RelativeRotationY; } if (LightSprite.Parent == null) { LightSpriteRotationZReset = LightSprite.RotationZ; } else { LightSpriteRotationZReset = LightSprite.RelativeRotationZ; } if (LightSprite.Parent == null) { LightSpriteRotationXVelocityReset = LightSprite.RotationXVelocity; } else { LightSpriteRotationXVelocityReset = LightSprite.RelativeRotationXVelocity; } if (LightSprite.Parent == null) { LightSpriteRotationYVelocityReset = LightSprite.RotationYVelocity; } else { LightSpriteRotationYVelocityReset = LightSprite.RelativeRotationYVelocity; } if (LightSprite.Parent == null) { LightSpriteRotationZVelocityReset = LightSprite.RotationZVelocity; } else { LightSpriteRotationZVelocityReset = LightSprite.RelativeRotationZVelocity; } LightSpriteAlphaReset = LightSprite.Alpha; LightSpriteAlphaRateReset = LightSprite.AlphaRate; }
protected virtual void InitializeEntity(bool addToManagers) { // Generated Initialize LoadStaticContent(ContentManagerName); mCollision = ShapeCollectionFile.AxisAlignedRectangles.FindByName("AxisAlignedRectangle1").Clone(); PostInitialize(); if (addToManagers) { AddToManagers(null); } }
protected override void InitializeEntity(bool addToManagers) { LoadStaticContent(ContentManagerName); mAxisAlignedRectangleInstance = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mAxisAlignedRectangleInstance.Name = "mAxisAlignedRectangleInstance"; AnimationChainInstance = new FlatRedBall.Graphics.Animation.AnimationChain(); AnimationChainInstance.Name = "AnimationChainInstance"; base.InitializeEntity(addToManagers); if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteXReset = LightOrShadowSprite.X; } else { LightOrShadowSpriteXReset = LightOrShadowSprite.RelativeX; } if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteYReset = LightOrShadowSprite.Y; } else { LightOrShadowSpriteYReset = LightOrShadowSprite.RelativeY; } if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteZReset = LightOrShadowSprite.Z; } else { LightOrShadowSpriteZReset = LightOrShadowSprite.RelativeZ; } if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteXVelocityReset = LightOrShadowSprite.XVelocity; } else { LightOrShadowSpriteXVelocityReset = LightOrShadowSprite.RelativeXVelocity; } if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteYVelocityReset = LightOrShadowSprite.YVelocity; } else { LightOrShadowSpriteYVelocityReset = LightOrShadowSprite.RelativeYVelocity; } if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteZVelocityReset = LightOrShadowSprite.ZVelocity; } else { LightOrShadowSpriteZVelocityReset = LightOrShadowSprite.RelativeZVelocity; } if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteRotationXReset = LightOrShadowSprite.RotationX; } else { LightOrShadowSpriteRotationXReset = LightOrShadowSprite.RelativeRotationX; } if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteRotationYReset = LightOrShadowSprite.RotationY; } else { LightOrShadowSpriteRotationYReset = LightOrShadowSprite.RelativeRotationY; } if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteRotationZReset = LightOrShadowSprite.RotationZ; } else { LightOrShadowSpriteRotationZReset = LightOrShadowSprite.RelativeRotationZ; } if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteRotationXVelocityReset = LightOrShadowSprite.RotationXVelocity; } else { LightOrShadowSpriteRotationXVelocityReset = LightOrShadowSprite.RelativeRotationXVelocity; } if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteRotationYVelocityReset = LightOrShadowSprite.RotationYVelocity; } else { LightOrShadowSpriteRotationYVelocityReset = LightOrShadowSprite.RelativeRotationYVelocity; } if (LightOrShadowSprite.Parent == null) { LightOrShadowSpriteRotationZVelocityReset = LightOrShadowSprite.RotationZVelocity; } else { LightOrShadowSpriteRotationZVelocityReset = LightOrShadowSprite.RelativeRotationZVelocity; } LightOrShadowSpriteAlphaReset = LightOrShadowSprite.Alpha; LightOrShadowSpriteAlphaRateReset = LightOrShadowSprite.AlphaRate; if (SpriteInstance.Parent == null) { SpriteInstanceXReset = SpriteInstance.X; } else { SpriteInstanceXReset = SpriteInstance.RelativeX; } if (SpriteInstance.Parent == null) { SpriteInstanceYReset = SpriteInstance.Y; } else { SpriteInstanceYReset = SpriteInstance.RelativeY; } if (SpriteInstance.Parent == null) { SpriteInstanceZReset = SpriteInstance.Z; } else { SpriteInstanceZReset = SpriteInstance.RelativeZ; } if (SpriteInstance.Parent == null) { SpriteInstanceXVelocityReset = SpriteInstance.XVelocity; } else { SpriteInstanceXVelocityReset = SpriteInstance.RelativeXVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceYVelocityReset = SpriteInstance.YVelocity; } else { SpriteInstanceYVelocityReset = SpriteInstance.RelativeYVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceZVelocityReset = SpriteInstance.ZVelocity; } else { SpriteInstanceZVelocityReset = SpriteInstance.RelativeZVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationXReset = SpriteInstance.RotationX; } else { SpriteInstanceRotationXReset = SpriteInstance.RelativeRotationX; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationYReset = SpriteInstance.RotationY; } else { SpriteInstanceRotationYReset = SpriteInstance.RelativeRotationY; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationZReset = SpriteInstance.RotationZ; } else { SpriteInstanceRotationZReset = SpriteInstance.RelativeRotationZ; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationXVelocityReset = SpriteInstance.RotationXVelocity; } else { SpriteInstanceRotationXVelocityReset = SpriteInstance.RelativeRotationXVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationYVelocityReset = SpriteInstance.RotationYVelocity; } else { SpriteInstanceRotationYVelocityReset = SpriteInstance.RelativeRotationYVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationZVelocityReset = SpriteInstance.RotationZVelocity; } else { SpriteInstanceRotationZVelocityReset = SpriteInstance.RelativeRotationZVelocity; } SpriteInstanceAlphaReset = SpriteInstance.Alpha; SpriteInstanceAlphaRateReset = SpriteInstance.AlphaRate; }
private bool DoFirstCollisionLineVsShapeCollection(Line line, TileShapeCollection tileShapeCollection) { line.LastCollisionPoint = new Point(double.NaN, double.NaN); Segment a = line.AsSegment(); if (tileShapeCollection.SortAxis == Axis.X) { var leftmost = (float)System.Math.Min(line.AbsolutePoint1.X, line.AbsolutePoint2.X); var rightmost = (float)System.Math.Max(line.AbsolutePoint1.X, line.AbsolutePoint2.X); float clampedPosition = line.Position.X; bool isPositionOnEnd = false; if (clampedPosition <= leftmost) { clampedPosition = leftmost; isPositionOnEnd = true; } else if (clampedPosition >= rightmost) { clampedPosition = rightmost; isPositionOnEnd = true; } // only support rectangles for now (maybe forever) var rectangles = tileShapeCollection.Rectangles; var firstIndex = rectangles.GetFirstAfter(leftmost - tileShapeCollection.GridSize, Axis.X, 0, rectangles.Count); var lastIndex = rectangles.GetFirstAfter(rightmost + tileShapeCollection.GridSize, Axis.X, firstIndex, rectangles.Count); if (isPositionOnEnd) { FlatRedBall.Math.Geometry.AxisAlignedRectangle collidedRectangle = null; Point?intersectionPoint = null; if (clampedPosition < rightmost) { // start at the beginning of the list, go up for (int i = firstIndex; i < lastIndex; i++) { var rectangle = tileShapeCollection.Rectangles[i]; if (collidedRectangle != null) { if (rectangle.X > collidedRectangle.X) { break; } if (rectangle.Y > collidedRectangle.Y && collidedRectangle.Y > line.Position.Y) { break; } if (rectangle.Y < collidedRectangle.Y && collidedRectangle.Y < line.Position.Y) { break; } } Point tl = new Point( rectangle.Position.X - rectangle.ScaleX, rectangle.Position.Y + rectangle.ScaleY); Point tr = new Point( rectangle.Position.X + rectangle.ScaleX, rectangle.Position.Y + rectangle.ScaleY); Point bl = new Point( rectangle.Position.X - rectangle.ScaleX, rectangle.Position.Y - rectangle.ScaleY); Point br = new Point( rectangle.Position.X + rectangle.ScaleX, rectangle.Position.Y - rectangle.ScaleY); Point tempPoint; // left gets priority // left var intersects = a.Intersects(new Segment(tl, bl), out tempPoint); if (rectangle.Y > line.Y) { // bottom gets priority over top if (!intersects) { // bottom intersects = a.Intersects(new Segment(bl, br), out tempPoint); } if (!intersects) { // top intersects = a.Intersects(new Segment(tl, tr), out tempPoint); } } else { // top gets priority over top if (!intersects) { // top intersects = a.Intersects(new Segment(tl, tr), out tempPoint); } if (!intersects) { // bottom intersects = a.Intersects(new Segment(bl, br), out tempPoint); } } if (!intersects) { // right intersects = a.Intersects(new Segment(tr, br), out tempPoint); } if (intersects) { intersectionPoint = tempPoint; collidedRectangle = rectangle; } } } else { // start at the end of the list, go down for (int i = lastIndex - 1; i >= firstIndex; i--) { var rectangle = tileShapeCollection.Rectangles[i]; if (collidedRectangle != null) { if (rectangle.X < collidedRectangle.X) { break; } if (rectangle.Y > collidedRectangle.Y && collidedRectangle.Y > line.Position.Y) { break; } if (rectangle.Y < collidedRectangle.Y && collidedRectangle.Y < line.Position.Y) { break; } } Point tl = new Point( rectangle.Position.X - rectangle.ScaleX, rectangle.Position.Y + rectangle.ScaleY); Point tr = new Point( rectangle.Position.X + rectangle.ScaleX, rectangle.Position.Y + rectangle.ScaleY); Point bl = new Point( rectangle.Position.X - rectangle.ScaleX, rectangle.Position.Y - rectangle.ScaleY); Point br = new Point( rectangle.Position.X + rectangle.ScaleX, rectangle.Position.Y - rectangle.ScaleY); Point tempPoint; // right gets priority // right var intersects = a.Intersects(new Segment(tr, br), out tempPoint); if (rectangle.Y > line.Y) { // bottom gets priority over top if (!intersects) { // bottom intersects = a.Intersects(new Segment(bl, br), out tempPoint); } if (!intersects) { // top intersects = a.Intersects(new Segment(tl, tr), out tempPoint); } } else { // top gets priority over top if (!intersects) { // top intersects = a.Intersects(new Segment(tl, tr), out tempPoint); } if (!intersects) { // bottom intersects = a.Intersects(new Segment(bl, br), out tempPoint); } } if (!intersects) { // left intersects = a.Intersects(new Segment(tl, bl), out tempPoint); } if (intersects) { intersectionPoint = tempPoint; collidedRectangle = rectangle; } } } if (collidedRectangle != null) { line.LastCollisionPoint = intersectionPoint ?? new Point(double.NaN, double.NaN); } return(collidedRectangle != null); } else { throw new NotImplementedException("Complain to Vic about this!"); } } else if (tileShapeCollection.SortAxis == Axis.Y) { throw new NotImplementedException("Bug Vic to do Y. Currently just X is done"); } return(false); }
public static bool CollideAgainst(this ICollidable thisInstance, AxisAlignedRectangle other) { return(thisInstance.Collision.CollideAgainst(other)); }
public bool CollideAgainstMove(AxisAlignedRectangle rectangle, float thisMass, float otherMass) { // just use the rectangle's call, but reverse the otherMass and thisMass return(rectangle.CollideAgainstMove(this, otherMass, thisMass)); }
public bool CollideAgainst(AxisAlignedRectangle rectangle) { if (mLastDependencyUpdate != TimeManager.CurrentTime) { UpdateDependencies(TimeManager.CurrentTime); } if (rectangle.LastDependencyUpdate != TimeManager.CurrentTime) { rectangle.UpdateDependencies(TimeManager.CurrentTime); } // first perform a quick test to see if the Circle is too far // away from the rectangle if ((Position.X + Radius < rectangle.X - rectangle.mScaleX || Position.X - Radius > rectangle.X + rectangle.mScaleX || Position.Y + Radius < rectangle.Y - rectangle.mScaleY || Position.Y - Radius > rectangle.Y + rectangle.mScaleY)) { return(false); } // The simple bounding box test from above will eliminate most // cases. If we get this far, then it is likely that we have a collision // and a more in-depth test can be performed. // quick test to see if the circle is inside of the rectangle if (rectangle.IsPointInside(Position.X, Position.Y)) { mLastCollisionTangent.X = -(Position.Y - rectangle.Position.Y); mLastCollisionTangent.Y = (Position.X - rectangle.Position.X); return(true); } // if we got this far, the bounding box of the circle touches the rectangle // and the center of the circle is not inside of the rectangle. Perform // the most expensive checks now. Point centerPoint = new Point(Position.X, Position.Y); Segment connectingSegment = new Segment(); int numberOfEndpointsClosestTo = 0; // top segment Segment segment = new Segment( new Point(rectangle.Left, rectangle.Top), new Point(rectangle.Right, rectangle.Top)); if (segment.DistanceTo(centerPoint, out connectingSegment) < Radius) { if (segment.IsClosestPointOnEndpoint(ref centerPoint)) { mLastCollisionTangent.X = -(connectingSegment.Point2.Y - Y); mLastCollisionTangent.Y = (connectingSegment.Point2.X - X); numberOfEndpointsClosestTo++; } else { mLastCollisionTangent.X = 1; mLastCollisionTangent.Y = 0; return(true); } } // bottom segment segment = new Segment( new Point(rectangle.Left, rectangle.Bottom), new Point(rectangle.Right, rectangle.Bottom)); if (segment.DistanceTo(centerPoint, out connectingSegment) < Radius) { if (segment.IsClosestPointOnEndpoint(ref centerPoint)) { mLastCollisionTangent.X = -(connectingSegment.Point2.Y - Position.Y); mLastCollisionTangent.Y = (connectingSegment.Point2.X - Position.X); numberOfEndpointsClosestTo++; } else { mLastCollisionTangent.X = 1; mLastCollisionTangent.Y = 0; return(true); } } // left segment segment = new Segment( new Point(rectangle.Left, rectangle.Top), new Point(rectangle.Left, rectangle.Bottom)); if (segment.DistanceTo(centerPoint, out connectingSegment) < Radius) { if (segment.IsClosestPointOnEndpoint(ref centerPoint)) { mLastCollisionTangent.X = -(connectingSegment.Point2.Y - Position.Y); mLastCollisionTangent.Y = (connectingSegment.Point2.X - Position.X); numberOfEndpointsClosestTo++; } else { mLastCollisionTangent.X = 0; mLastCollisionTangent.Y = 1; return(true); } } // right segment segment = new Segment( new Point(rectangle.Right, rectangle.Top), new Point(rectangle.Right, rectangle.Bottom)); if (segment.DistanceTo(centerPoint, out connectingSegment) < Radius) { if (segment.IsClosestPointOnEndpoint(ref centerPoint)) { mLastCollisionTangent.X = -(connectingSegment.Point2.Y - Position.Y); mLastCollisionTangent.Y = (connectingSegment.Point2.X - Position.X); numberOfEndpointsClosestTo++; } else { mLastCollisionTangent.X = 0; mLastCollisionTangent.Y = 1; return(true); } } if (numberOfEndpointsClosestTo > 0) { return(true); } else // well, this is rare, but it can happen. Collision failed! { return(false); } }
protected virtual void InitializeEntity(bool addToManagers) { LoadStaticContent(ContentManagerName); SpriteInstance = new FlatRedBall.Sprite(); SpriteInstance.Name = "SpriteInstance"; mCollisionBox = new FlatRedBall.Math.Geometry.AxisAlignedRectangle(); mCollisionBox.Name = "mCollisionBox"; PostInitialize(); if (SpriteInstance.Parent == null) { SpriteInstanceXReset = SpriteInstance.X; } else { SpriteInstanceXReset = SpriteInstance.RelativeX; } if (SpriteInstance.Parent == null) { SpriteInstanceYReset = SpriteInstance.Y; } else { SpriteInstanceYReset = SpriteInstance.RelativeY; } if (SpriteInstance.Parent == null) { SpriteInstanceZReset = SpriteInstance.Z; } else { SpriteInstanceZReset = SpriteInstance.RelativeZ; } if (SpriteInstance.Parent == null) { SpriteInstanceXVelocityReset = SpriteInstance.XVelocity; } else { SpriteInstanceXVelocityReset = SpriteInstance.RelativeXVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceYVelocityReset = SpriteInstance.YVelocity; } else { SpriteInstanceYVelocityReset = SpriteInstance.RelativeYVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceZVelocityReset = SpriteInstance.ZVelocity; } else { SpriteInstanceZVelocityReset = SpriteInstance.RelativeZVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationXReset = SpriteInstance.RotationX; } else { SpriteInstanceRotationXReset = SpriteInstance.RelativeRotationX; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationYReset = SpriteInstance.RotationY; } else { SpriteInstanceRotationYReset = SpriteInstance.RelativeRotationY; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationZReset = SpriteInstance.RotationZ; } else { SpriteInstanceRotationZReset = SpriteInstance.RelativeRotationZ; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationXVelocityReset = SpriteInstance.RotationXVelocity; } else { SpriteInstanceRotationXVelocityReset = SpriteInstance.RelativeRotationXVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationYVelocityReset = SpriteInstance.RotationYVelocity; } else { SpriteInstanceRotationYVelocityReset = SpriteInstance.RelativeRotationYVelocity; } if (SpriteInstance.Parent == null) { SpriteInstanceRotationZVelocityReset = SpriteInstance.RotationZVelocity; } else { SpriteInstanceRotationZVelocityReset = SpriteInstance.RelativeRotationZVelocity; } SpriteInstanceAlphaReset = SpriteInstance.Alpha; SpriteInstanceAlphaRateReset = SpriteInstance.AlphaRate; if (CollisionBox.Parent == null) { CollisionBoxXReset = CollisionBox.X; } else { CollisionBoxXReset = CollisionBox.RelativeX; } if (CollisionBox.Parent == null) { CollisionBoxYReset = CollisionBox.Y; } else { CollisionBoxYReset = CollisionBox.RelativeY; } if (CollisionBox.Parent == null) { CollisionBoxZReset = CollisionBox.Z; } else { CollisionBoxZReset = CollisionBox.RelativeZ; } if (CollisionBox.Parent == null) { CollisionBoxXVelocityReset = CollisionBox.XVelocity; } else { CollisionBoxXVelocityReset = CollisionBox.RelativeXVelocity; } if (CollisionBox.Parent == null) { CollisionBoxYVelocityReset = CollisionBox.YVelocity; } else { CollisionBoxYVelocityReset = CollisionBox.RelativeYVelocity; } if (CollisionBox.Parent == null) { CollisionBoxZVelocityReset = CollisionBox.ZVelocity; } else { CollisionBoxZVelocityReset = CollisionBox.RelativeZVelocity; } if (CollisionBox.Parent == null) { CollisionBoxRotationXReset = CollisionBox.RotationX; } else { CollisionBoxRotationXReset = CollisionBox.RelativeRotationX; } if (CollisionBox.Parent == null) { CollisionBoxRotationYReset = CollisionBox.RotationY; } else { CollisionBoxRotationYReset = CollisionBox.RelativeRotationY; } if (CollisionBox.Parent == null) { CollisionBoxRotationZReset = CollisionBox.RotationZ; } else { CollisionBoxRotationZReset = CollisionBox.RelativeRotationZ; } if (CollisionBox.Parent == null) { CollisionBoxRotationXVelocityReset = CollisionBox.RotationXVelocity; } else { CollisionBoxRotationXVelocityReset = CollisionBox.RelativeRotationXVelocity; } if (CollisionBox.Parent == null) { CollisionBoxRotationYVelocityReset = CollisionBox.RotationYVelocity; } else { CollisionBoxRotationYVelocityReset = CollisionBox.RelativeRotationYVelocity; } if (CollisionBox.Parent == null) { CollisionBoxRotationZVelocityReset = CollisionBox.RotationZVelocity; } else { CollisionBoxRotationZVelocityReset = CollisionBox.RelativeRotationZVelocity; } if (addToManagers) { AddToManagers(null); } }
public static bool CollideAgainstBounce(this ICollidable thisInstance, AxisAlignedRectangle other, float thisMass, float otherMass, float elasticity) { return(thisInstance.Collision.CollideAgainstBounce(other, thisMass, otherMass, elasticity)); }
public bool CollideAgainst(FlatRedBall.Math.Geometry.AxisAlignedRectangle rectangle, bool isCloudCollision = false) { return(CollideAgainst(() => rectangle.CollideAgainstBounce(this.Collision, 1, 0, 0), isCloudCollision)); }