public void Vector2_NormalizedCopy_Test() { var a = new Vector2(5, -10); var b = a.NormalizedCopy(); Assert.IsTrue(new Vector2(0.4472136f, -0.8944272f).EqualsWithTolerance(b)); }
private bool IsTransporterAtNextTile(TilePosition transporterTilePosition, Vector2 motion) { var nextTilePostition = transporterTilePosition.Position + motion.NormalizedCopy(); var(x, y) = ((int)nextTilePostition.X, (int)nextTilePostition.Y); return(_tileOccupationManager.GetItemsInTile(x, y).Any(e => _transporterMapper.Has(e))); }
public static Vector2 Truncate(this Vector2 value, float maxLength) { if (value.LengthSquared() > maxLength * maxLength) { return(value.NormalizedCopy() * maxLength); } return(value); }
internal Bullet(Vector2 position, Vector2 speed) { Position = position; Speed = speed.NormalizedCopy(); Position += Speed * Size / 2; Speed *= 100; Acceleration = Speed; MaxSpeed *= 25; Moveable = true; DestroyOnCollision = true; Collidable = true; CollisionGroup = 0b1000; CollidesWith = 0b0010; Rotation = Speed.ToAngle(); }
public Ray2D(Vector2 position, Vector2 direction) { Position = position; Direction = direction.NormalizedCopy(); }
protected override void Update(GameTime gameTime) { KeyboardState kState = Keyboard.GetState(); // Allow the user to exit by pressing the escape key if (kState.IsKeyDown(Keys.Escape)) { Exit(); } // TODO: Better way to do this? if (!prevKeysDown.Contains(Keys.F1) && kState.IsKeyDown(Keys.F1)) { GenSystem(1); } else if (!prevKeysDown.Contains(Keys.F2) && kState.IsKeyDown(Keys.F2)) { GenSystem(2); } else if (!prevKeysDown.Contains(Keys.F3) && kState.IsKeyDown(Keys.F3)) { GenSystem(3); } else if (!prevKeysDown.Contains(Keys.F4) && kState.IsKeyDown(Keys.F4)) { GenSystem(4); } else if (!prevKeysDown.Contains(Keys.F5) && kState.IsKeyDown(Keys.F5)) { GenSystem(5); } else if (!prevKeysDown.Contains(Keys.F12) && kState.IsKeyDown(Keys.F12)) { GenSystem(0); } // Allow the camera position to be changed float moveSpeed = 4; if (kState.IsKeyDown(Keys.LeftShift)) { moveSpeed *= 3; } if (kState.IsKeyDown(Keys.A)) { cameraPos.X += moveSpeed; } if (kState.IsKeyDown(Keys.D)) { cameraPos.X -= moveSpeed; } if (kState.IsKeyDown(Keys.W)) { cameraPos.Y += moveSpeed; } if (kState.IsKeyDown(Keys.S)) { cameraPos.Y -= moveSpeed; } MouseState mState = Mouse.GetState(); bool zoomedIn = mState.ScrollWheelValue > prevScroll; bool zoomedOut = mState.ScrollWheelValue < prevScroll; if (kState.IsKeyDown(Keys.LeftControl)) { if (zoomedIn) { desiredRadius *= 1.1f; } if (zoomedOut) { desiredRadius /= 1.1f; } } if (kState.IsKeyDown(Keys.LeftShift)) { if (zoomedIn) { desiredMass *= 1.1f; } if (zoomedOut) { desiredMass /= 1.1f; } } if (kState.IsKeyUp(Keys.LeftControl) && kState.IsKeyUp(Keys.LeftShift)) { // Allow the user to zoom in and out if (zoomedIn) { scaleMod *= 1.1f; cameraPos *= 1.1f; } else if (zoomedOut) { scaleMod /= 1.1f; cameraPos /= 1.1f; } } prevScroll = mState.ScrollWheelValue; // Allow the user to add planets with the desired position/velocity mouseCurrentPos = mState.Position.ToVector2(); if (mState.LeftButton == ButtonState.Pressed && movingPlanet is null) { Vector2 mPosInSpace = GetMousePosInSpace(mouseCurrentPos); Planet nearest = GetNearestPlanetToPoint(mPosInSpace); if ((nearest.Displacement - mPosInSpace).LengthSquared() < nearest.Radius * nearest.Radius) { movingPlanet = nearest; } } else if (mState.LeftButton == ButtonState.Pressed && !(movingPlanet is null)) { } else if (mState.LeftButton == ButtonState.Released && !(movingPlanet is null)) { movingPlanet = null; } // If the user has just clicked else if (!preparingToAdd && mState.LeftButton == ButtonState.Pressed) { mouseStartPos = mouseCurrentPos; preparingToAdd = true; } // If the user has just released their click if (preparingToAdd && mState.LeftButton == ButtonState.Released) { preparingToAdd = false; Vector2 mouseDiff = mouseCurrentPos - mouseStartPos; float length = mouseDiff.LengthSquared(); // Velocity is proportional to the distance moved by the mouse squared Vector2 velocity; length /= 100; if (length > 0) { velocity = mouseDiff.NormalizedCopy() * length; } else { velocity = Vector2.Zero; } if (!(focusPlanet is null)) { velocity += focusPlanet.Velocity; } Vector2 position = GetMousePosInSpace(mouseStartPos); planets.Add(new Planet(Color.SkyBlue, desiredRadius, velocity, position, desiredMass)); debugText = position.ToString(); } if (mState.MiddleButton == ButtonState.Pressed && prevMState.MiddleButton == ButtonState.Released) { Vector2 mPosInSpace = GetMousePosInSpace(mouseCurrentPos); Planet nearest = GetNearestPlanetToPoint(mPosInSpace); // If the user middle clicks near enough to the planet, it should still count float leeway = 16; if ((nearest.Displacement - mPosInSpace).LengthSquared() < nearest.Radius * nearest.Radius * leeway) { if (focusPlanet == nearest) { drawLinesRelativeTo = nearest; ResetDrawingFromLines(); } else { focusPlanet = nearest; } } } // Create debris if the right mouse button is held down if (mState.RightButton == ButtonState.Pressed && prevMState.RightButton == ButtonState.Released) { if (!(focusPlanet is null)) { focusPlanet = null; } else { drawLinesRelativeTo = null; ResetDrawingFromLines(); } //Vector2 position = GetMousePosInSpace(mouseCurrentPos); //for (int i = 0; i < 10; i++) //{ // random.NextUnitVector(out Vector2 velocity); // Planet particle = new Planet(Color.SkyBlue, 0.01f, velocity * random.Next(6, 7) + planets[0].Velocity, position, 1000, true); // planets.Add(particle); //} }
protected override void Update(GameTime gameTime) { if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) { Exit(); } _oldKeyboardState = _currentKeyboardState; _currentKeyboardState = Keyboard.GetState(); // Mouse movement if (Mouse.GetState().LeftButton == ButtonState.Pressed) { _boxA.Center = Mouse.GetState().Position.ToVector2(); } // Delta time var deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds; const float moveV = 200.0f; if (IsKeyDown(Keys.A) && !IsKeyDown(Keys.D)) { _boxA.Velocity.X = -moveV; } else if (!IsKeyDown(Keys.A) && IsKeyDown(Keys.D)) { _boxA.Velocity.X = moveV; } else { _boxA.Velocity.X = 0; } if (IsKeyPressed(Keys.W)) { _boxA.Velocity.Y = -300; } // acceleration _boxA.Velocity += _boxA.Acceleration * deltaTime; _boxB.Velocity += _boxB.Acceleration * deltaTime; // move _boxA.Center += _boxA.Velocity * deltaTime; _boxB.Center += _boxB.Velocity * deltaTime; // construct the relative velocity ray var rvRay = (_boxA.Velocity - _boxB.Velocity) * deltaTime; // collision check var md = _boxB.MinkowskiDifference(_boxA); if (md.Min.X <= 0 && md.Max.X >= 0 && md.Min.Y <= 0 && md.Max.Y >= 0) { _isColliding = true; // penetration depth _penetractionVector = md.ClosestPointOnBoundsToPoint(Vector2.Zero); // move the box out of the penetration _boxA.Center += _penetractionVector; if (_penetractionVector != Vector2.Zero) { var tangent = _penetractionVector.NormalizedCopy().Tangent(); _boxA.Velocity = Vector2.Dot(_boxA.Velocity, tangent) * tangent; _boxB.Velocity = Vector2.Dot(_boxB.Velocity, tangent) * tangent; } } else { _isColliding = false; var intersectFraction = md.GetRayIntersectionFraction(Vector2.Zero, rvRay); if (intersectFraction < float.PositiveInfinity) { _isColliding = true; // move the boxes appropriately _boxA.Center += _boxA.Velocity * deltaTime * intersectFraction; _boxB.Center += _boxB.Velocity * deltaTime * intersectFraction; // zero out the normal of the collision var nrvRay = rvRay.NormalizedCopy(); var tangent = new Vector2(-nrvRay.Y, nrvRay.X);//nrvRay.Tangent(); _boxA.Velocity = Vector2.Dot(_boxA.Velocity, tangent) * tangent; _boxB.Velocity = Vector2.Dot(_boxB.Velocity, tangent) * tangent; } } _mdBox = md; base.Update(gameTime); }
public Ray2D(Vector2 position, Vector2 direction) { this.Position = position; this.Direction = direction.NormalizedCopy(); }