public override void Tick(float dt) { base.Tick(dt); GamePadData data = GamePad.GetData(0); float analogX = 0.0f; float analogY = 0.0f; //d-pad movement,emulating analog movement of the sticks if (Input2.GamePad0.Right.Down) { analogX = 1.0f; } else if (Input2.GamePad0.Left.Down) { analogX = -1.0f; } if (Input2.GamePad0.Up.Down) { analogY = -1.0f; } else if (Input2.GamePad0.Down.Down) { analogY = 1.0f; } //if the left stick is moving,then use values read from the stick if (data.AnalogLeftX > 0.2f || data.AnalogLeftX < -0.2f || data.AnalogLeftY > 0.2f || data.AnalogLeftY < -0.2f) { analogX = data.AnalogLeftX; analogY = data.AnalogLeftY; } //calculate the position Position = new Vector2(Position.X + analogX / 10f, Position.Y - analogY / 10f); //rotate according to the right analog stick, or if it's not moving, then according the the left stick // so basically if you are not pointing the player in any direction with the right stick he is going to point in the walking direction //or if both sticks are not moving,then use the analogX and analogY values(d-pad movement) if (data.AnalogRightX > 0.2f || data.AnalogRightX < -0.2f || data.AnalogRightY > 0.2f || data.AnalogRightY < -0.2f) { var angleInRadians = FMath.Atan2(-data.AnalogRightX, -data.AnalogRightY); Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } else if (data.AnalogLeftX > 0.2f || data.AnalogLeftX < -0.2f || data.AnalogLeftY > 0.2f || data.AnalogLeftY < -0.2f) { var angleInRadians = FMath.Atan2(-data.AnalogLeftX, -data.AnalogLeftY); Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } else if (analogX != 0.0f || analogY != 0.0f) { var angleInRadians = FMath.Atan2(-analogX, -analogY); Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } }
public void Move(Vector2 moveFactor) { float angle = FMath.Atan2(moveFactor.X, moveFactor.Y); float rotateFactor = ((0 - angle - this.Angle - (FMath.PI / 2.0f))); while (rotateFactor < -2 * FMath.PI) { rotateFactor += 2 * FMath.PI; } while (rotateFactor > 2 * FMath.PI) { rotateFactor -= 2 * FMath.PI; } if (rotateFactor > FMath.PI) { rotateFactor -= 2 * FMath.PI; } else if (rotateFactor < -FMath.PI) { rotateFactor += 2 * FMath.PI; } rotateFactor /= 20.0f; //System.Console.WriteLine("rotateFactor = "+(rotateFactor*20.0f*FMath.RadToDeg)+", angle = "+(angle*FMath.RadToDeg)+", this.Angle = "+(this.Angle*FMath.RadToDeg)); Rotate(rotateFactor); }
public Vec3f ToEuler() { float phi = FMath.Atan2(2.0f * (x * y + z * w), 1.0f - 2.0f * (y * y + z * z)); float theta = FMath.Asin(2.0f * (x * z - w * y)); float psi = FMath.Atan2(2.0f * (x * w + y * z), 1.0f - 2.0f * (z * z + w * w)); return(new Vec3f(phi, theta, psi)); }
private static float AngleBetweenEdges(int prev, int current, int next, V[] vertices) { var vec1 = vertices[prev].position - vertices[current].position; var vec2 = vertices[next].position - vertices[current].position; var result = FMath.Atan2(vec2.Y, vec2.X) - FMath.Atan2(vec1.Y, vec1.X); return(result < 0 ? FMath.TwoPi + result : result); }
public override void Move() { powerStnPos = powerstation.Position; if (point1 != true) { powerStnPos.Y = shipHeight / 2; } // Step 2 - move towards the player by calculating the vector between the Boid and the player Vector3 diff = Vector3.Subtract(powerStnPos, position); if (diff.Length() > 1) { velocity += Vector3.Normalize(diff) / 10; } // Calculate the rotation angle of this Boid (for drawing) using Atan2 if ((velocity.X != 0) || (velocity.Y != 0)) { rotation = FMath.Atan2(velocity.Y, velocity.X); } // Slow this Boid down if it's going too fast float velLength = velocity.Length(); if (velLength > speed) { velocity = velocity.Normalize(); velocity *= speed; } //stop the motion of the ship. if (point1) { stpDist = 15000; } else { stpDist = 100; } if (Vector3.DistanceSquared(position, powerStnPos) < stpDist) { velocity *= 0; if (point1 == false) { point1 = true; } } // Update the position of the Boid based on its velocity position += velocity; sprite.Position = position; sprite.Rotation = rotation; }
public override void Move() { //I want this to follow the MotherShip, but I am not sure of any good way to do this. vectorPos = playership.Position; // Step 1 - avoid your neighbors avoidNeighbors(enemyList); // Step 2 - move towards the player by calculating the vector between the Boid and the player Vector3 diff = Vector3.Subtract(vectorPos, Pos); if (diff.Length() > 1) { velocity += Vector3.Normalize(diff) / 10; } // Calculate the rotation angle of this Boid (for drawing) using Atan2 if ((velocity.X != 0) || (velocity.Y != 0)) { rotation = FMath.Atan2(velocity.Y, velocity.X); } // Slow this Boid down if it's going too fast float velLength = velocity.Length(); if (velLength > speed) { velocity = velocity.Normalize(); velocity *= speed; } //stop the motion of the ship. if (Vector3.DistanceSquared(Pos, vectorPos) < 5000) { velocity *= 0; } // Update the position of the Boid based on its velocity position += velocity; sprite.Position = position; sprite.Rotation = rotation; }
public void TestAtan2() { var y = Fixed.FromInt(0); var x = Fixed.FromInt(1); var r = Fixed.FromFloat(0); AssertApproximately(r, FMath.Atan2(y, x)); y = Fixed.FromInt(0); x = Fixed.FromInt(-1); r = Fixed.FromFloat((float)Math.PI); AssertApproximately(r, FMath.Atan2(y, x)); y = Fixed.FromInt(1); x = Fixed.FromInt(0); r = Fixed.FromFloat((float)Math.PI / 2); AssertApproximately(r, FMath.Atan2(y, x)); y = Fixed.FromFloat(1.54513f); x = Fixed.FromFloat(-1.65673f); r = Fixed.FromFloat(2.390926f); // 2.3910351266f is more accurate value AssertApproximately(r, FMath.Atan2(y, x)); }
public void Update(Vector2 centrePos, Vector2 rotation) { sprite.Position = centrePos; sprite.Angle = -(float)FMath.Atan2(rotation.X, rotation.Y); }
public override void Tick(float dt) { base.Tick(dt); GamePadData data = GamePad.GetData(0); float analogX = 0.0f; float analogY = 0.0f; //d-pad movement,emulating analog movement of the sticks if (Input2.GamePad0.Right.Down) { analogX = 1.0f; } else if (Input2.GamePad0.Left.Down) { analogX = -1.0f; } if (Input2.GamePad0.Up.Down) { analogY = -1.0f; } else if (Input2.GamePad0.Down.Down) { analogY = 1.0f; } //if the left stick is moving,then use values read from the stick if (data.AnalogLeftX > 0.2f || data.AnalogLeftX < -0.2f || data.AnalogLeftY > 0.2f || data.AnalogLeftY < -0.2f) { analogX = data.AnalogLeftX; analogY = data.AnalogLeftY; } //calculate the position /*if(analogX>0) * { * Position = new Vector2 (Position.X + 0.1f, Position.Y); * }else if(analogX<0) * { * Position = new Vector2 (Position.X - 0.1f, Position.Y); * } * * if(analogY>0) * { * Position = new Vector2 (Position.X + Position.Y- 0.1f); * }else if(analogY<0) * { * Position = new Vector2 (Position.X, Position.Y + 0.1f); * }*/ Vector2 proposedChange; if (analogX != 0.0f) { proposedChange = new Vector2(analogX / 10f, 0.0f); if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange)) { Position += proposedChange; } } if (analogY != 0.0f) { proposedChange = new Vector2(0.0f, -analogY / 10f); if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange)) { Position += proposedChange; } } //rotate according to the right analog stick, or if it's not moving, then according the the left stick // so basically if you are not pointing the player in any direction with the right stick he is going to point in the walking direction //or if both sticks are not moving,then use the analogX and analogY values(d-pad movement) // OR do autoaim if enabled if (data.AnalogRightX > 0.2f || data.AnalogRightX < -0.2f || data.AnalogRightY > 0.2f || data.AnalogRightY < -0.2f) { var angleInRadians = FMath.Atan2(-data.AnalogRightX, -data.AnalogRightY); playerBodySprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } else if (!Game.autoAim && (data.AnalogLeftX > 0.2f || data.AnalogLeftX < -0.2f || data.AnalogLeftY > 0.2f || data.AnalogLeftY < -0.2f)) { var angleInRadians = FMath.Atan2(-data.AnalogLeftX, -data.AnalogLeftY); playerBodySprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } else if (!Game.autoAim && (analogX != 0.0f || analogY != 0.0f)) { var angleInRadians = FMath.Atan2(-analogX, -analogY); playerBodySprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } else if (Game.autoAim) { GameEntity e; if (Collisions.findNearestEnemy(Player.Instance, 8.0f, out e)) { Vector2 distance = (Player.Instance.Position - e.Position).Normalize(); var angleInRadians = FMath.Atan2(distance.X, -distance.Y); playerBodySprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } else { var angleInRadians = FMath.Atan2(-analogX, -analogY); playerBodySprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); } } }
public BasicEnemy(Vector2 pos, TextureInfo tex) { Position = pos; sprite = new SpriteTile(); sprite.TextureInfo = tex; sprite.Position = pos; sprite.TileIndex2D = new Vector2i(0, 0); sprite.CenterSprite(new Vector2(0.4f, 0.5f)); sprite.Scale = new Vector2(2.0f, 2.0f); //set up the random vector randomMovement = Support.rand.NextVector2(-0.01f, 0.01f); //update the animation frames sprite.Schedule((dt) => { if (FrameCount % 2 == 0) { //if attacking,use all animation frames if (attacking) { //deal damage to the player if (FrameCount % damageDelay == 0) { Player.Instance.Health -= damage; } animationFrame = (animationFrame + 1) % 25; } else { //if not,then use first three animation frames animationFrame = (animationFrame + 1) % 4; } //assign the correct tileindex sprite.TileIndex1D = animationFrame; //if close to the player,then follow,otherwise move randomly if (Player.Instance.Position.Distance(sprite.Position) < 6.0f && Collisions.checkLineOfSight(this, Player.Instance)) { isMovingRandomly = false; step = (Player.Instance.Position - sprite.Position).Normalize() / 15.0f; //check if should be attacking if (Collisions.checkCollisionBetweenEntities(this, Player.Instance)) { //check if is not attacking already if (attacking == false) { attacking = true; //manualy skip the frames animationFrame = 4; } } else { //make sure that attacking is set to false attacking = false; } } else { //player is not within range, move randomly isMovingRandomly = true; step = randomMovement; } } //advance the position by the given Vector2 step sprite.Position += step; //only move when not attacking if (!attacking) { //collision detection on the X axis //create a vector 2 containing a proposed change to the position Vector2 proposedChange = new Vector2(step.X, 0.0f) * speedModifier; //temporary game entity to contain the entity the enemy might have collided with GameEntity tempEntity; //check wall collisions and then collisions with other enemies if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange) && !Collisions.efficientCollisionsCheck(this, proposedChange, out tempEntity)) { //no collision, so we can change the position Position += proposedChange / speedModifier; } else if (isMovingRandomly) { //collided with something,but if the enemy should be moving randomly, then let it move randomMovement.X = -randomMovement.X; } //collision detection on the Y axis proposedChange = new Vector2(0.0f, step.Y) * speedModifier; if (!Collisions.checkWallsCollisions(this, MapManager.Instance.currentMap, ref proposedChange) && !Collisions.efficientCollisionsCheck(this, proposedChange, out tempEntity)) { Position += proposedChange / speedModifier; } else if (isMovingRandomly) { randomMovement.Y = -randomMovement.Y; } //position changed, so we need to remove the entity from the QuadTree and add again //this is because the entity might be in the wrong place in the QuadTree and removing and re-adding is the only way to fix that Game.Instance.quadTree.removeEntity(this); Game.Instance.quadTree.insert(this); } //rotate the sprite to face the direction of walking var angleInRadians = -FMath.Atan2(step.X, step.Y); sprite.Rotation = new Vector2(FMath.Cos(angleInRadians), FMath.Sin(angleInRadians)); //correct for the fact that the sprite is rotated in the texture file sprite.Rotation = sprite.Rotation.Rotate(90.0f); }, -1); //create a shadow texture - temporarily disabled //SpriteUV shadow = new SpriteUV(new TextureInfo(Bullet.fireTexture)); //shadow.CenterSprite(new Vector2(0.5f,0.5f)); //shadow.Color = Sce.PlayStation.HighLevel.GameEngine2D.Base.Math.SetAlpha(Colors.Black,0.5f); //calculate the bounds for the entire sprite bounds = new Bounds2(); sprite.GetlContentLocalBounds(ref bounds); bounds = new Bounds2(bounds.Min * 0.5f, bounds.Max * 0.5f); }
//Update player V2.1 @AS public void Update(Vector2 gravity, Vector2 rotate, Vector2 movement, bool invert, bool falling, Vector2 additionalForces) { duckRotation = -(float)FMath.Atan2(rotate.X, rotate.Y); sprite.Angle = duckRotation; //Rotate the duck so it's always facing upright Vector2 tempDir; //Initalise the interchangable temp vector if (!falling) { if (!invert) { if (additionalForces != new Vector2(0.0f, 0.0f)) { tempDir = movement * 5 * additionalForces; //Normal movement caused by tilting the device } else { tempDir = movement * 5; } } else { if (additionalForces != new Vector2(0.0f, 0.0f)) { tempDir = movement * 5 * additionalForces; //Normal movement caused by tilting the device } else { tempDir = movement * 5; } } } else { if (gravVelocity > -1.0f) //If he isn't moving backwards from a collision { tempDir = new Vector2(gravity.X, gravity.Y); //Normal gravity } else { if (invert) //If inverted { tempDir = new Vector2(gravity.X, -gravity.Y); //Rebound in the Y axis (SIDES) } else { tempDir = new Vector2(-gravity.X, gravity.Y); //Else rebound in the X axis (FLOOR) } } if (gravVelocity < maxGrav) //Increase the gravity velocity { if (gravVelocity > -0.5f && gravVelocity < 0.5f) { gravVelocity = 1f; } else if (gravVelocity > 1f) { gravVelocity += gravSpeed / 3; } else { gravVelocity += gravSpeed; } } } Vector2 velocityChange = new Vector2((tempDir.X * gravVelocity) + additionalForces.X, (tempDir.Y * gravVelocity) + additionalForces.Y); //Move the player sprite.Position = new Vector2(sprite.Position.X + velocityChange.X, sprite.Position.Y + velocityChange.Y); momentum = velocityChange.Length() * mass; }