public Player(Game1 game) { this.game = game; timer = new TimeSpan(0); frame = 0; bounds = new PhysicsRectangle(new Vector2(game.GraphicsDevice.Viewport.Bounds.X + FRAME_WIDTH * scale, game.GraphicsDevice.Viewport.Bounds.Height - FRAME_HEIGHT / 2 * scale), FRAME_WIDTH * scale, FRAME_HEIGHT * scale); }
public static Contact RectangleVSRectangle(PhysicsRectangle r, PhysicsRectangle r1, float Delta) { if ((r.VelX == 0) && (r.VelY == 0)) { return new Contact() { Collided = false } } ; PhysicsRectangle Expanded = new PhysicsRectangle() { SizeX = r1.SizeX + r.SizeX, SizeY = r1.SizeY + r.SizeY, X = r1.X - (r.SizeX / 2), Y = r1.Y - (r.SizeY / 2) }; Ray ray = new Ray() { x = r.X + (r.SizeX / 2), y = r.Y + (r.SizeY / 2), dx = r.VelX * Delta, dy = r.VelY * Delta }; //Console.WriteLine($"X:{Expanded.X} Y:{Expanded.Y}\nSizeX:{Expanded.SizeX} SizeY:{Expanded.SizeY}"); return(RectangleVSRay(Expanded, ray)); } }
public static Vector2 pointOnRectangle(Vector2 point, PhysicsRectangle rect) { var qx = point.X - rect.origin.X; var qy = point.Y - rect.origin.Y; if (qx > rect.halfWidth) { qx = rect.halfWidth; } else if (qx < -rect.halfWidth) { qx = -rect.halfWidth; } if (qy > rect.halfHeight) { qy = rect.halfHeight; } else if (qy < -rect.halfHeight) { qy = -rect.halfHeight; } return(new Vector2(qx + rect.origin.X, qy + rect.origin.Y)); }
public TankPlayer(Game1 game, PhysicsRectangle tankRectangle) { this.game = game; this.tankRectangle = tankRectangle; turretRectangle = new PhysicsRectangle(new Vector2(tankRectangle.origin.X, tankRectangle.origin.Y - tankRectangle.halfHeight), 15, 25); game.particleEngine.exhaustSystem.Emitter.Add(tankRectangle.origin); }
public void Reset() { int randWidth = rand.Next(50, 500); int randHeight = rand.Next(100, 200); this.bounds = new PhysicsRectangle(new Vector2(game.GraphicsDevice.Viewport.Bounds.Width + randWidth, game.GraphicsDevice.Viewport.Bounds.Height - randHeight / 2), randWidth, randHeight); velocity = new Vector2(0); leftGravity = rand.Next(2, 7) * 0.1f; }
public static bool Collides(PhysicsRectangle rect1, PhysicsRectangle rect2) { if (Math.Abs(rect1.origin.X - rect2.origin.X) > rect1.halfWidth + rect2.halfWidth) { return(false); } if (Math.Abs(rect1.origin.Y - rect2.origin.Y) > rect1.halfHeight + rect2.halfHeight) { return(false); } return(true); }
/// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. particleEngine = new ParticleEngine(); floor = new PhysicsRectangle(new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height), GraphicsDevice.Viewport.Width, 50); spriteBatch = new SpriteBatch(GraphicsDevice); particleEngine.LoadContent(GraphicsDevice, Content); debugTexture = Content.Load <Texture2D>("Pixel"); testTank = new TankPlayer(this, new PhysicsRectangle(new Vector2(GraphicsDevice.Viewport.Width / 2, GraphicsDevice.Viewport.Height - 50 / 2), 50, 50)); testTank.texture = debugTexture; // TODO: use this.Content to load your game content here }
public static bool Collides(PhysicsCircle circle, PhysicsRectangle rect) { var point = Nearest.pointOnRectangle(circle.origin, rect); return(Within.pointInCircle(point, circle)); }
public static Contact RectangleVSRay(PhysicsRectangle r, Ray l) { float inverseX = 1.0f / l.dx; float inverseY = 1.0f / l.dy; float NearX = (r.X - l.x) * inverseX; float NearY = (r.Y - l.y) * inverseY; float FarX = ((r.X + r.SizeX) - l.x) * inverseX; float FarY = ((r.Y + r.SizeY) - l.y) * inverseY; //Console.WriteLine("X:" + inverseX + " Y:" + inverseY); //Console.WriteLine("X:"+(r.X - l.x) + "::"+ ((r.X + r.SizeX) - l.x)); //Console.WriteLine("Y:"+(r.Y - l.y) + "::" + ((r.Y + r.SizeY) - l.y)); //Console.WriteLine("X:" + NearX + "::" + FarX); //Console.WriteLine("Y:" + NearY + "::" + FarY); if (float.IsNaN(NearX) || float.IsNaN(NearY)) { return new Contact { Collided = false } } ; if (float.IsNaN(FarX) || float.IsNaN(FarY)) { return new Contact { Collided = false } } ; //Console.WriteLine("C1"); if (NearX > FarX) { Swap(ref NearX, ref FarX); } if (NearY > FarY) { Swap(ref NearY, ref FarY); } if ((NearX > FarY) || (NearY > FarX)) { return new Contact { Collided = false } } ; float TimeHitNear = Math.Max(NearX, NearY); float TimeHitFar = Math.Min(FarX, FarY); if (TimeHitFar < 0) { return new Contact { Collided = false } } ; float ContactX = l.x + (l.dx * TimeHitNear); float ContactY = l.y + (l.dy * TimeHitNear); float NormalX = 0; float NormalY = 0; if (NearX > NearY) { if (l.dx < 0) { NormalX = 1; } else { NormalX = -1; } } else if (NearX < NearY) { if (l.dy < 0) { NormalY = 1; } else { NormalY = -1; } } //Console.WriteLine(TimeHitNear + ":" + TimeHitFar); if ((TimeHitNear >= 0.0f) && (TimeHitNear < 1.0f)) { return new Contact() { Collided = true, NX = NormalX, NY = NormalY, TimeOfContact = TimeHitNear, X = ContactX, Y = ContactY } } ; else { return new Contact() { Collided = false, NX = NormalX, NY = NormalY, TimeOfContact = TimeHitNear, X = ContactX, Y = ContactY } }; }
public static bool RectangleIntersect(PhysicsRectangle r, PhysicsRectangle r1) { return((r.X < r1.X + r1.SizeX) && (r.X + r.SizeX > r1.X) && (r.Y < r1.Y + r1.SizeY) && (r.Y + r.SizeY > r1.Y)); }
static void PrintRectangle(int index, PhysicsRectangle a) { Console.WriteLine($"===============\nIndex:{index}\nX:{a.X} Y:{a.Y}\nSizeX:{a.SizeX} SizeY:{a.SizeY}\nDX:{a.VelX} DY:{a.VelY} Parent:{a.Parent} Hit: Up: {a.Hit[(int)Direction.Up]} Down: {a.Hit[(int)Direction.Down]} Left: {a.Hit[(int)Direction.Left]} Up: {a.Hit[(int)Direction.Right]}\n==============="); }