Skip to content

Commit

Permalink
Added basic implementation of Player model following mouse. But the u…
Browse files Browse the repository at this point in the history
…nit doesnt turn around.
  • Loading branch information
stanpalatnik committed Feb 4, 2012
1 parent d63b37c commit 10af5af
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Osmium Wars/Osmium Wars/Classes/Entity.cs
Expand Up @@ -20,6 +20,7 @@ abstract class Entity : Microsoft.Xna.Framework.DrawableGameComponent
protected Model model;

protected Vector3 position = Vector3.Zero;
protected Vector3 faceposition = Vector3.Forward;
protected float rotation = 0.0f;

/// <summary>
Expand Down Expand Up @@ -54,7 +55,7 @@ public override void Draw(GameTime gameTime)
effect.EnableDefaultLighting();
effect.AmbientLightColor = new Vector3(255, 255, 255);
effect.World = transforms[mesh.ParentBone.Index] * Matrix.CreateScale(0.2f, 0.2f, 0.2f) * Matrix.CreateRotationY(this.rotation) * Matrix.CreateTranslation(this.position);
effect.View = Matrix.CreateLookAt(this.game.cameraPosition, Vector3.Zero, Vector3.Forward);
effect.View = Matrix.CreateLookAt(this.game.cameraPosition, Vector3.Zero, faceposition);
effect.Projection = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), this.game.aspectRatio, 1.0f, 10000.0f);
}
// Draw the mesh, using the effects set above.
Expand Down
12 changes: 10 additions & 2 deletions Osmium Wars/Osmium Wars/Classes/Player.cs
Expand Up @@ -15,6 +15,7 @@ namespace OW
class Player : Infantry
{
private static int SPEED = 10;
private double angle;
public Player(Game game) : base(game)
{
this.game = game;
Expand All @@ -29,6 +30,8 @@ protected override void LoadContent()
public override void Update(GameTime gameTime)
{
KeyboardState keyState = Keyboard.GetState();
MouseState mouseState = Mouse.GetState();

//Implement movement with WASD controls
if (keyState.IsKeyDown(Keys.W))
{
Expand All @@ -46,7 +49,12 @@ public override void Update(GameTime gameTime)
{
this.position.X += SPEED;
}
}


//Now make him look at the direction of the mouse

angle = Math.Atan2((double)mouseState.Y - this.position.Y, (double)mouseState.X - this.position.X); //this will return the angle(in radians) from sprite to mouse.
this.faceposition.X = (float)Math.Cos(angle);
this.faceposition.Y = (float)Math.Sin(angle);
}
}
}

0 comments on commit 10af5af

Please sign in to comment.