public static void NewLocation(IBlock block, IMario mario, CollisionSide side) { switch (side) { case CollisionSide.Top: mario.MarioPhysics.Position = new Vector2(mario.MarioPhysics.Position.X, block.BlockPhysics.Position.Y - mario.MarioBox.Height); mario.MarioPhysics.Velocity = new Vector2(0, 0); break; case CollisionSide.Left: mario.MarioPhysics.Position = new Vector2(block.BlockPhysics.Position.X - mario.MarioBox.Width, mario.MarioPhysics.Position.Y); mario.MarioPhysics.Velocity = new Vector2(0, mario.MarioPhysics.Velocity.Y); break; case CollisionSide.Right: mario.MarioPhysics.Position = new Vector2(block.BlockPhysics.Position.X + block.BlockBox.Width, mario.MarioPhysics.Position.Y); mario.MarioPhysics.Velocity = new Vector2(0, mario.MarioPhysics.Velocity.Y); break; case CollisionSide.Bottom: mario.MarioPhysics.Position = new Vector2(mario.MarioPhysics.Position.X, block.BlockPhysics.Position.Y + block.BlockBox.Height); mario.MarioPhysics.Velocity = new Vector2(mario.MarioPhysics.Velocity.X, 0); break; default: break; } }
public EnemyEnemyCollisionResponse(IEnemy e1, IEnemy e2, CollisionSide type) { Rectangle intersect = Rectangle.Intersect(e1.Position, e2.Position); if (type == CollisionSide.Left) { if ((!e1.IsDead) && (!e2.IsDead)) { e1.BounceX(intersect.Width); e2.BounceX(-intersect.Width); e1.Turn(); e2.Turn(); } } else if (type == CollisionSide.Right) { if ((!e1.IsDead) && (!e2.IsDead)) { e1.BounceX(-intersect.Width); e2.BounceX(intersect.Width); e1.Turn(); e2.Turn(); } } }
public NightmareEnemyItemCollisionResponse(INightmareEnemy n, IItems i, CollisionSide c, Game1 g) { Rectangle intersection = Rectangle.Intersect(n.Position, i.Position); switch (c) { case CollisionSide.Top: n.BounceY(-intersection.Height); if (!n.IsKilled) { n.Land(); } break; case CollisionSide.Left: n.TurnRight(); n.BounceX(intersection.Width); break; case CollisionSide.Right: n.BounceX(-intersection.Width); n.TurnLeft(); break; case CollisionSide.None: n.Fall(); break; } }
public static Rectangle NewOwnCollisionRectangle(Character character, CollisionSide collisionSide, int newCoordinate) => new Rectangle( x: (collisionSide == CollisionSide.LEFT || collisionSide == CollisionSide.RIGHT) ? newCoordinate : (int)character.GetMovement().Position.X, y: (collisionSide == CollisionSide.BOTTOM || collisionSide == CollisionSide.TOP) ? newCoordinate : (int)character.GetMovement().Position.Y, width: character.CollisionRectangle.Width, height: character.CollisionRectangle.Height );
private void BounceOffSide(CollisionSide side) { if (!bounceOnCooldown) { OnBounce(); if (side == CollisionSide.left) { SetVelocity(new Vector2(Mathf.Abs(GetVelocity().x), GetVelocity().y)); //velocity.x = Mathf.Abs(GetVelocity().x); } else if (side == CollisionSide.right) { SetVelocity(new Vector2(-Mathf.Abs(GetVelocity().x), GetVelocity().y)); //velocity.x = -Mathf.Abs(velocity.x); } else if (side == CollisionSide.above) { SetVelocity(new Vector2(GetVelocity().x, -Mathf.Abs(GetVelocity().y))); //velocity.y = -Mathf.Abs(velocity.y); } else { SetVelocity(new Vector2(GetVelocity().x, Mathf.Abs(GetVelocity().y))); //velocity.y = Mathf.Abs(velocity.y); } } }
public ProjectileMarioCollisionResponse(IMario m, IProjectile p, CollisionSide type) { Rectangle intersection = Rectangle.Intersect(m.Position, p.Position); Console.WriteLine("In the response"); if (p is ShellProjectile) { if (p.ShellCount < 1) { if (type == CollisionSide.Left || type == CollisionSide.Top) { m.BounceX(-intersection.Width); m.BounceY(-intersection.Height); p.Move(); p.ShellCount++; Console.WriteLine("In the response block"); } else if (type == CollisionSide.Right) { m.BounceX(intersection.Width); p.MoveLeft(); p.ShellCount++; } } else { m.Die(); } } }
/// <summary>Gets which side of a stationary object was collided with by a moving object /// by comparing the slope of the moving object's velocity and the slope of the velocity /// that would have caused the moving object to be touching corners with the stationary /// object.</summary> /// <param name="potentialSides">The potential two sides that the moving object might have /// collided with.</param> /// <param name="velocitySlope">The slope of the moving object's velocity.</param> /// <param name="nearestCornerSlope">The slope of the path from the closest corner of the /// moving object's previous hitbox to the closest corner of the stationary object's /// hitbox.</param> /// <returns>The side of the stationary object with which the moving object collided. /// </returns> static CollisionSide GetCollisionSideFromSlopeComparison( CollisionSide potentialSides, double velocitySlope, double nearestCornerSlope) { if ((potentialSides & CollisionSide.Top) == CollisionSide.Top) { if ((potentialSides & CollisionSide.Left) == CollisionSide.Left) { return(velocitySlope < nearestCornerSlope ? CollisionSide.Top : CollisionSide.Left); } else if ((potentialSides & CollisionSide.Right) == CollisionSide.Right) { return(velocitySlope > nearestCornerSlope ? CollisionSide.Top : CollisionSide.Right); } } else if ((potentialSides & CollisionSide.Bottom) == CollisionSide.Bottom) { if ((potentialSides & CollisionSide.Left) == CollisionSide.Left) { return(velocitySlope > nearestCornerSlope ? CollisionSide.Bottom : CollisionSide.Left); } else if ((potentialSides & CollisionSide.Right) == CollisionSide.Right) { return(velocitySlope < nearestCornerSlope ? CollisionSide.Bottom : CollisionSide.Right); } } return(CollisionSide.None); }
public static void HandleCollision(IDavis davis, IEnemy enemy, CollisionSide side, IWorld world) { if (side != CollisionSide.None) { if (!(enemy.PhysicsState is JulianKnockBackState)) { if (davis.PhysicsState is FlyingKneeState || davis.PhysicsState is ShoryukenState || davis.PhysicsState is ShunpoState) { enemy.TakeDamage(); world.HUD.score += 100; } } //if collision is not on bottom if (side == CollisionSide.Top) { if (!enemy.Dead && !davis.DeadFlag && !(enemy.PhysicsState is JulianKnockBackState)) { enemy.TakeDamage(); world.HUD.score += 100; } } else { if (!enemy.Dead && davis.DavisStatus != DavisStatus.Invincible && !davis.DeadFlag && !(davis.PhysicsState is FlyingKneeState) && !(davis.PhysicsState is ShoryukenState && !(davis.PhysicsState is ShunpoState))) { davis.DavisDeath(); world.HUD.lives--; } } } }
public Vector2 SetVelocity(CollisionSide colliderSide, float endSpeed, Vector2 actualVelocity) { float xVel = actualVelocity.x; float yVel = actualVelocity.y; float xVelAbs = Mathf.Abs(xVel); float yVelAbs = Mathf.Abs(yVel); Vector2 endVel = new Vector2(); switch (colliderSide) { case CollisionSide.Bottom: endVel = new Vector2(xVel, yVelAbs).normalized *endSpeed; break; case CollisionSide.Top: endVel = new Vector2(xVel, -yVelAbs).normalized *endSpeed; break; case CollisionSide.Left: endVel = new Vector2(xVelAbs, yVel).normalized *endSpeed; break; case CollisionSide.Right: endVel = new Vector2(-xVelAbs, yVel).normalized *endSpeed; break; } Rigidbody.velocity = endVel; return(endVel); }
protected override void OnCollisionResponse(IPlayer Mario, CollisionSide side) { if (this.currentState.Equals(BlockState.Idle) && side.Equals(CollisionSide.Bottom)) { CurrentSprite = used; currentState = BlockState.Bumped; this.beingCollided = true; preBumpPos = PosY; //If the first item is grow mushroom, then the second item must be flower. if (items != null && items.Length > ZERO) { if (items[ZERO] is GrowMushroomItem) { if (((Mario)Mario).CurrentState == MarioStateMachine.MarioState.Small) { items[ZERO].Spawn(); } else { items[ONE].Spawn(); } } else { items[ZERO].Spawn(); } } } }
public static void HandleCollision(IEnemy enemy, IBlock block, CollisionSide side) { if (!(enemy.PhysicsState is EnemyDeadState)) { switch (side) { case CollisionSide.Left: case CollisionSide.Right: enemy.ChangeDirection(); break; case CollisionSide.Bottom: if (!(block is EmptyBlock) && !(block.PhysicsState is JulianKnockBackState)) { enemy.Location = new Vector2(enemy.Location.X, block.Location.Y + block.HitBox.Height); enemy.PhysicsState = new FallState(enemy); } break; case CollisionSide.Top: if (!(block is EmptyBlock) && !(block.PhysicsState is JulianKnockBackState) && !(enemy.PhysicsState is JulianPowerPunchState) && !(enemy.PhysicsState is JulianMetaAttackState)) { enemy.Location = new Vector2(enemy.Location.X, block.Location.Y - enemy.HitBox.Height); enemy.PhysicsState = new StandingState(enemy); } break; default: break; } } }
public void OnCollisionResponse(ICollider otherCollider, CollisionSide side) { if (otherCollider is IPlayer) { OnCollisionResponse((IPlayer)otherCollider, side); } }
public List <IRayCollider> HandleCollision(List <IRayCollider> collidersToSkip) { List <IRayCollider> collidedWith = new List <IRayCollider>(); Vector2 direction = currentPosition - LastFrameCenterPoint; float distanceForRay = direction.magnitude; RaycastHit2D rayHit = rayHits[0]; CollisionSide colSide = CollisionSide.Bottom; if (rayHit.collider.CompareTag(GameTags.Ball)) { //BallBase ballBase = rayHit.collider.GetComponent<BallBase>(); //colSide = CollisionSideDetect.GetCollisionSide(rayHit.centroid, rayHit.point); //OnCollisionWithBall(ballBase, rayHit, colSide, direction, out distanceForRay); } else if (rayHit.collider.CompareTag(GameTags.Ship)) { ShipCollider ship = rayHit.collider.GetComponent <ShipCollider>(); Debug.LogWarningFormat("ray for another ship hit"); //colSide = ship.GetCollisionSideWithBall(this, LastFrameCenterPoint); // OnCollision(this, rayHit, colSide, CollisionType.Ship, PhysicsConstants.BallSpeedAfterShipHit, safe, out distanceForRay); } Debug.LogWarningFormat("Ship collision with {0} on side {1}", rayHit.collider.gameObject.name, colSide); return(collidedWith); }
/// <summary> /// Determine the prominent collision side of the two tiles /// </summary> /// <param name="tile">The tile to check against</param> /// <returns>The prominent side for the collision of the two tiles</returns> public CollisionSide GetCollisionSide(BaseTile tile) { CollisionSide side = CollisionSide.None; if (IsCollided(tile)) { // Calculate offsets double topOffset = Bottom - tile.Top; double botOffset = tile.Bottom - Top; double leftOffset = Right - tile.Left; double rightOffset = tile.Right - Left; // Start with the top side double newOffset = topOffset; side = CollisionSide.Top; // Check the bottom side side = botOffset > newOffset ? CollisionSide.Bottom : side; newOffset = Math.Max(newOffset, botOffset); // Check the left side side = leftOffset > newOffset ? CollisionSide.Left : side; newOffset = Math.Max(newOffset, leftOffset); // Check the right side side = rightOffset > newOffset ? CollisionSide.Right : side; newOffset = Math.Max(newOffset, rightOffset); // Check if no side side = newOffset > MagicNumbers.TILESIZE * 2 ? CollisionSide.None : side; } return(side); }
private Tuple <ConstructorInfo, ConstructorInfo> GetCommandsToExecuteSideSpecific(Collision c) { IGameObject obj1 = c.ObjectColliding; IGameObject obj2 = c.ObjectCollidedWith; CollisionSide side = c.Side; Tuple <ConstructorInfo, ConstructorInfo> commandsToExecute = new Tuple <ConstructorInfo, ConstructorInfo>(null, null); Tuple <String, String, CollisionSide> doubleSpecificKey = new Tuple <string, string, CollisionSide>(obj1.SpecificCollisionType, obj2.SpecificCollisionType, side); Tuple <String, String, CollisionSide> obj1SpecificKey = new Tuple <string, string, CollisionSide>(obj1.SpecificCollisionType, obj2.CollisionType, side); Tuple <String, String, CollisionSide> obj2SpecificKey = new Tuple <string, string, CollisionSide>(obj1.CollisionType, obj2.SpecificCollisionType, side); Tuple <String, String, CollisionSide> noSpecificKey = new Tuple <string, string, CollisionSide>(obj1.CollisionType, obj2.CollisionType, side); if (SideSpecificCollisionMap.ContainsKey(doubleSpecificKey)) { commandsToExecute = SideSpecificCollisionMap[doubleSpecificKey]; } else if (SideSpecificCollisionMap.ContainsKey(obj1SpecificKey)) { commandsToExecute = SideSpecificCollisionMap[obj1SpecificKey]; } else if (SideSpecificCollisionMap.ContainsKey(obj2SpecificKey)) { commandsToExecute = SideSpecificCollisionMap[obj2SpecificKey]; } else if (SideSpecificCollisionMap.ContainsKey(noSpecificKey)) { commandsToExecute = SideSpecificCollisionMap[noSpecificKey]; } return(commandsToExecute); }
public Collision(PhysicsEntity entity, GameObject gameObject, CollisionSide side) { this.entity = entity; this.gameObject = gameObject; collideArray = new bool[4]; collideArray[(int)side] = true; }
// Find the points that two hitboxes collide // <h> is the other hitbox // <thisPos> is the current hitbox's position // <hPos> is the other hitbox's position // Returns a CollisionSide object with the results public CollisionSide IntersectsSide(Hitbox h, Vector2 thisPos, Vector2 hPos) { Vector2 topRight = thisPos + (Size / 2); Vector2 bottomLeft = thisPos - (Size / 2); Vector2 topLeft = new Vector2(bottomLeft.X, topRight.Y); Vector2 bottomRight = new Vector2(topRight.X, bottomLeft.Y); Vector2 middleTop = (topLeft + topRight) / 2; Vector2 middleBottom = (bottomLeft + bottomRight) / 2; Vector2 middleLeft = (topLeft + bottomLeft) / 2; Vector2 middleRight = (topRight + bottomRight) / 2; Vector2 wallLeft = (bottomLeft) + new Vector2(0f, 0.1f); Vector2 wallRight = (bottomRight) + new Vector2(0f, 0.1f); CollisionSide collisionSide = new CollisionSide(); collisionSide.TopLeft = Inside(h, hPos, topLeft); collisionSide.TopRight = Inside(h, hPos, topRight); collisionSide.BottomLeft = Inside(h, hPos, bottomLeft); collisionSide.BottomRight = Inside(h, hPos, bottomRight); collisionSide.TopMiddle = Inside(h, hPos, middleTop); collisionSide.BottomMiddle = Inside(h, hPos, middleBottom); collisionSide.LeftMiddle = Inside(h, hPos, middleLeft); collisionSide.RightMiddle = Inside(h, hPos, middleRight); collisionSide.WallLeft = Inside(h, hPos, wallLeft); collisionSide.WallRight = Inside(h, hPos, wallRight); return(collisionSide); }
public CollisionSide GetCollisionSideWithBall(Vector2 centroidPoint) { CollisionSide colSide = CollisionSideDetect.GetCollisionSideBasedOnTriangleAndBottomPoint(LeftPoint.position, RightPoint.position, BottomPoint.position, centroidPoint); Debug.Log("Striker col: " + colSide); return(colSide); }
private void CollisionWith(CollisionSide side) // для границ карты { if (side == CollisionSide.vertical) { RectangleF newPos = new RectangleF((float)(Area.X - Speed * Direction.X), (float)(Area.Y - Speed * Direction.Y), Area.Width, Area.Height); Direction = new Vector2(Direction.X, Direction.Y * -1); SetPosition(newPos.Left, newPos.Top); } else if (side == CollisionSide.horizontal) { RectangleF newPos = new RectangleF((float)(Area.X - Speed * Direction.X), (float)(Area.Y - Speed * Direction.Y), Area.Width, Area.Height); Direction = new Vector2(Direction.X * -1, Direction.Y); SetPosition(newPos.Left, newPos.Top); } if (Direction.X == 0 && Area.X < Map.WindowSize.Left) { Direction = new Vector2(1, Direction.Y); } else if (Direction.X == 0 && Area.Right > Map.WindowSize.Right) { Direction = new Vector2(-1, Direction.Y); } if (Direction.Y == 0 && Area.Top < Map.WindowSize.Top) { Direction = new Vector2(Direction.X, 1); } else if (Direction.Y == 0 && Area.Bottom > Map.WindowSize.Bottom) { Direction = new Vector2(Direction.X, -1); } Direction = Vector2.Normalize(Direction); }
private Vector2 GetForceOnLeftOrRight(BallBase ball, CollisionSide colSide) { Vector2 forceVector = StrikerCollisionForceManager.GetCollisionEndVelocity(CollisionType, colSide, isMovingOrMovedUp, isForceModeOn); float endSpeed = PhysicsConstants.BallSpeedAfterStrikerIdleHit; return((ball.LastFrameVelocity.normalized + forceVector).normalized * endSpeed); }
/// <summary>Returns a Vector2 storing the correct location of a moving object /// after collision with a stationary object has been resolved.</summary> /// <param name="movingObjectHitbox">The hitbox of the moving object colliding with a /// stationary object.</param> /// <param name="stationaryObjectHitbox">The hitbox of the stationary object.</param> /// <param name="collisionSide">The side of the stationary object with which the moving /// object collided.</param> /// <returns>A Vector2 storing the corrected location of the moving object /// after resolving its collision with the stationary object.</returns> public static Vector2 GetCorrectedLocation(Rectangle movingObjectHitbox, Rectangle stationaryObjectHitbox, CollisionSide collisionSide) { Vector2 correctedLocation = movingObjectHitbox.Location.ToVector2(); switch (collisionSide) { case CollisionSide.Left: correctedLocation.X = stationaryObjectHitbox.X - movingObjectHitbox.Width; break; case CollisionSide.Right: correctedLocation.X = stationaryObjectHitbox.X + stationaryObjectHitbox.Width; break; case CollisionSide.Top: correctedLocation.Y = stationaryObjectHitbox.Y - movingObjectHitbox.Height; break; case CollisionSide.Bottom: correctedLocation.Y = stationaryObjectHitbox.Y + stationaryObjectHitbox.Height; break; } return(correctedLocation); }
public static Vector2 GetCollisionDirectionVector(this CollisionSide colSide, Vector2 vector) { float xVel = vector.x; float yVel = vector.y; float xVelAbs = Mathf.Abs(xVel); float yVelAbs = Mathf.Abs(yVel); Vector2 endVector = new Vector2(); switch (colSide) { case CollisionSide.Bottom: endVector = new Vector2(xVel, yVelAbs); break; case CollisionSide.Top: endVector = new Vector2(xVel, -yVelAbs); break; case CollisionSide.Left: endVector = new Vector2(xVelAbs, yVel); break; case CollisionSide.Right: endVector = new Vector2(-xVelAbs, yVel); break; } return(endVector.normalized); }
/// <summary> /// Get the collision side when moving object has a non-straight movement /// </summary> /// <param name="sides">Side flags to enrich</param> /// <param name="velocityRatio">The velocity ratio between previous frame moving object corner point and static object corner point</param> /// <param name="nearestCornerRatio">The nearest corner ratio between corrected position corner point and static object corner point</param> /// <returns></returns> private static CollisionSide GetCollisionSideFromVectorComparison(CollisionSide sides, double velocityRatio, double nearestCornerRatio) { if ((sides & CollisionSide.Top) == CollisionSide.Top) { if ((sides & CollisionSide.Left) == CollisionSide.Left) { return(velocityRatio < nearestCornerRatio ? CollisionSide.Top : CollisionSide.Left); } if ((sides & CollisionSide.Right) == CollisionSide.Right) { return(velocityRatio > nearestCornerRatio ? CollisionSide.Top : CollisionSide.Right); } } else if ((sides & CollisionSide.Bottom) == CollisionSide.Bottom) { if ((sides & CollisionSide.Left) == CollisionSide.Left) { return(velocityRatio > nearestCornerRatio ? CollisionSide.Bottom : CollisionSide.Left); } if ((sides & CollisionSide.Right) == CollisionSide.Right) { return(velocityRatio < nearestCornerRatio ? CollisionSide.Bottom : CollisionSide.Right); } } return(CollisionSide.None); }
protected override void OnCollisionResponse(IPlayer Mario, CollisionSide side) { //If the Mario hit the invisible block from top, left and right sides, toggle the collision status to true. if (this.currentState.Equals(BlockState.Invisible) && (side.Equals(CollisionSide.Top) || side.Equals(CollisionSide.Left) || side.Equals(CollisionSide.Right))) { this.CollidedWithThreeSides = true; } //If the Mario hit the invisible block from the bottom, then change the state of the block. if (this.currentState.Equals(BlockState.Invisible) && side.Equals(CollisionSide.Bottom) && this.CollidedWithThreeSides == false) { this.ChangeState(); //If the first item is grow mushroom, then the second item must be flower. if (items != null && items.Length > ZERO) { if (items[ZERO] is GrowMushroomItem) { if (((Mario)Mario).CurrentState == MarioStateMachine.MarioState.Small) { items[ZERO].Spawn(); } else { items[ONE].Spawn(); } } else { items[ZERO].Spawn(); } } } }
/// <summary> /// Perform the logic when the given tile collides with this tile /// </summary> /// <param name="tile">The tile that collided with this tile</param> /// <param name="side">The prominent side of the collision</param> /// <returns>The event arguments for the collision</returns> public override CollisionEventArgs CollideWith(Player tile, CollisionSide side) { CollisionEventArgs args = null; if (side == CollisionSide.Top) { args = new CollisionEventArgs { Command = "ground", Argument = Top + 1, Sender = this }; } else if (side == CollisionSide.Bottom) { args = new CollisionEventArgs { Command = "unground", Sender = this }; } else if (side == CollisionSide.Left) { args = new CollisionEventArgs { Command = "halt", Argument = tile.Right - Left, Sender = this }; } return(args); }
public static CollisionSide GetOppositeSide(CollisionSide side) { CollisionSide oppositeSide; switch (side) { case CollisionSide.Top: oppositeSide = CollisionSide.Bottom; break; case CollisionSide.Bottom: oppositeSide = CollisionSide.Top; break; case CollisionSide.Left: oppositeSide = CollisionSide.Right; break; case CollisionSide.Right: oppositeSide = CollisionSide.Left; break; default: throw new InvalidOperationException("Invalid collision side provided"); } return(oppositeSide); }
public CollideEventArgs(GameObjectRigidBody object1, GameObjectRigidBody object2, CollisionSide side, float depth) { this.object1 = object1; this.object2 = object2; this.side = side; this.depth = depth; }
public void RegisterSideSpecificCommand(String objType1, String objType2, CollisionSide side, ConstructorInfo commandOnObj1, ConstructorInfo commandOnObj2) { Tuple <String, String, CollisionSide> collisionTuple = new Tuple <string, string, CollisionSide>(objType1, objType2, side); Tuple <ConstructorInfo, ConstructorInfo> commandTuple = new Tuple <ConstructorInfo, ConstructorInfo>(commandOnObj1, commandOnObj2); SideSpecificCollisionMap.Add(collisionTuple, commandTuple); }
protected override void OnCollisionResponse(IProjectile projectile, CollisionSide side) { if (projectile is KoopaShellProjectile) { currentState = KoopaShellStates.Idle; } }
public static void HandleCollision(IPlayer player, IBlock block, CollisionSide side) { if (side == CollisionSide.Top && player.IsJumping) { player.Idle(); } if (block is HiddenBlock) // Special case handling for hidden blocks (only bottom collision allowed) { HandleHiddenBlockCollision(player, block as HiddenBlock, side); } else { if (!block.Collidable) { return; } switch (block) { case BrickBlock brickBlock: HandleBrickBlockCollision(player, brickBlock, side); break; case QuestionBlock questionBlock: HandleQuestionBlockCollision(player, questionBlock, side); break; default: PlayerBlockRepel(block, player, side); break; } } }
public Scenery(Vector2 position, bool isVisible = true, ICutlassTexture texture = null, bool animated = false, CollisionSide side = CollisionSide.All) { _Position = position; _IsVisible = isVisible; _Active = true; if (isVisible && texture != null) { _SceneryObject_Id = TextureManager.AddTexture(texture); _Animated = animated; _Side = side; } else { _Side = CollisionSide.All; } }
public CollisionInfo(CollisionSide side, Script targetScript, Script collisionScript) { CollisionSide = side; TargetScript = targetScript; CollisionScript = collisionScript; }
/// <summary> /// Bounces the two objects off each other. This method makes sure the speed /// of each object after the bounce is set to the given speed values /// </summary> /// <param name="firstVelocity">the velocity of the first object</param> /// <param name="firstDrawRectangle">the draw rectangle of the first object</param> /// <param name="secondVelocity">the velocity of the second object</param> /// <param name="secondDrawRectangle">the draw rectangle of the second object</param> /// <param name="preCollisionDuration">the duration before the collision</param> /// <param name="postCollisionDuration">the duration after the collision</param> /// <param name="collisionSide">the collision side</param> /// <returns>the collision resolution info object</returns> private static CollisionResolutionInfo BounceObjects(Vector2 firstVelocity, Rectangle firstDrawRectangle, Vector2 secondVelocity, Rectangle secondDrawRectangle, int preCollisionDuration, int postCollisionDuration, CollisionSide collisionSide) { // save speeds for later float firstSpeed = firstVelocity.Length(); float secondSpeed = secondVelocity.Length(); // move forward up to collision Rectangle newFirstDrawRectangle = MoveForward(firstVelocity, firstDrawRectangle, preCollisionDuration); Rectangle newSecondDrawRectangle = MoveForward(secondVelocity, secondDrawRectangle, preCollisionDuration); // change velocities as appropriate Vector2 newFirstVelocity; Vector2 newSecondVelocity; GetNewVelocities(firstVelocity, secondVelocity, collisionSide, out newFirstVelocity, out newSecondVelocity); // move objects forward after collision MoveForward(newFirstVelocity, newFirstDrawRectangle, postCollisionDuration); MoveForward(newSecondVelocity, newSecondDrawRectangle, postCollisionDuration); // may still need to move objects apart if they're still colliding MoveCollidingObjectsApart(newFirstVelocity, newFirstDrawRectangle, newSecondVelocity, newSecondDrawRectangle, out newFirstDrawRectangle, out newSecondDrawRectangle); return new CollisionResolutionInfo(newFirstVelocity, newFirstDrawRectangle, false, newSecondVelocity, newSecondDrawRectangle, false); }
/// <summary> /// Gets the new velocity vectors for the collider (first object) and /// collidee (second object) in a collision. Need to be careful if /// the collider caught up to the collidee when both were going in /// the same direction /// </summary> /// <param name="firstVelocity">velocity of the first object</param> /// <param name="secondVelocity">velocity of the second object</param> /// <param name="collisionSide">the collision side</param> /// <param name="newFirstVelocity">the new first object velocity</param> /// <param name="newSecondVelocity">the new second object velocity</param> private static void GetNewVelocities(Vector2 firstVelocity, Vector2 secondVelocity, CollisionSide collisionSide, out Vector2 newFirstVelocity, out Vector2 newSecondVelocity) { switch (collisionSide) { case CollisionSide.Top: if (firstVelocity.Y > 0 && secondVelocity.Y > 0) { // first object caught up to second object, only change first y velocity newFirstVelocity = new Vector2(firstVelocity.X, -1 * firstVelocity.Y); newSecondVelocity = secondVelocity; } else if (firstVelocity.Y < 0 && secondVelocity.Y < 0) { // second object caught up to first object, only change second y velocity newFirstVelocity = firstVelocity; newSecondVelocity = new Vector2(secondVelocity.X, -1 * secondVelocity.Y); } else { // normal top collision, change both y velocities newFirstVelocity = new Vector2(firstVelocity.X, -1 * firstVelocity.Y); newSecondVelocity = new Vector2(secondVelocity.X, -1 * secondVelocity.Y); } break; case CollisionSide.Bottom: if (firstVelocity.Y > 0 && secondVelocity.Y > 0) { // second object caught up to first object, only change second y velocity newFirstVelocity = firstVelocity; newSecondVelocity = new Vector2(secondVelocity.X, -1 * secondVelocity.Y); } else if (firstVelocity.Y < 0 && secondVelocity.Y < 0) { // first object caught up to second object, only change first y velocity newFirstVelocity = new Vector2(firstVelocity.X, -1 * firstVelocity.Y); newSecondVelocity = secondVelocity; } else { // normal bottom collision, change both y velocities newFirstVelocity = new Vector2(firstVelocity.X, -1 * firstVelocity.Y); newSecondVelocity = new Vector2(secondVelocity.X, -1 * secondVelocity.Y); } break; case CollisionSide.Left: if (firstVelocity.X > 0 && secondVelocity.X > 0) { // first object caught up to second object, only change first x velocity newFirstVelocity = new Vector2(-1 * firstVelocity.X, firstVelocity.Y); newSecondVelocity = secondVelocity; } else if (firstVelocity.X < 0 && secondVelocity.X < 0) { // second object caught up to first object, only change second x velocity newFirstVelocity = firstVelocity; newSecondVelocity = new Vector2(-1 * secondVelocity.X, secondVelocity.Y); } else { // normal left collision, change both x velocities newFirstVelocity = new Vector2(-1 * firstVelocity.X, firstVelocity.Y); newSecondVelocity = new Vector2(-1 * secondVelocity.X, secondVelocity.Y); } break; case CollisionSide.Right: if (firstVelocity.X > 0 && secondVelocity.X > 0) { // second object caught up to first object, only change second x velocity newFirstVelocity = firstVelocity; newSecondVelocity = new Vector2(-1 * secondVelocity.X, secondVelocity.Y); } else if (firstVelocity.X < 0 && secondVelocity.X < 0) { // first object caught up to second object, only change first x velocity newFirstVelocity = new Vector2(-1 * firstVelocity.X, firstVelocity.Y); newSecondVelocity = secondVelocity; } else { // normal right collision, change both x velocities newFirstVelocity = new Vector2(-1 * firstVelocity.X, firstVelocity.Y); newSecondVelocity = new Vector2(-1 * secondVelocity.X, secondVelocity.Y); } break; default: // should never get here newFirstVelocity = firstVelocity; newSecondVelocity = secondVelocity; break; } }
/// <summary> /// Gets the collision normal for the given collision side of an object /// </summary> /// <param name="side">the collision side</param> /// <returns>the collision normal</returns> private static Vector2 GetSideCollisionNormal(CollisionSide side) { switch (side) { case CollisionSide.Top: return new Vector2(0, -1); case CollisionSide.Bottom: return Vector2.UnitY; case CollisionSide.Left: return Vector2.UnitX; case CollisionSide.Right: return new Vector2(-1, 0); default: return Vector2.Zero; // should never get here } }