Original Post
Trouble code has its font enlarged so you will see it easily. EDIT- its merely wrapped in size[] code. Boo, it is listed below the code section.
Im getting nan errors where in the past using the same technique, it worked.
Im trying to simply move the sprite in the direction it is facing(dictated by the mouse position). If i put in code that ive used before, (basically finding the x and y velocities using the highlighted code) which has worked no problem in the past.
It's fairly simple code, but i cant find why it isnt working.
The highlighted code;
pVx = ((float)Math.Cos(pRot) * speed);
pVx = player X velocity
pRot = player Rotation in radians. (Which works fine)
speed is a float value, set to zero (but even setting it to 1 changes nothing, and speed has always started at zero in my other projects where this code worked)
I get Nan errors for pRot, and Ppos.X
Ive run the same code before and it's not been a problem, i just can't find the source of the problem, seems so simple.
Can anyone see why i would be getting a nan error?
Im getting nan errors where in the past using the same technique, it worked.
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
List<Texture2D> Tiles = new List<Texture2D>();
private int tileSize = 78;
MouseState mouse;
private Vector2 mousePos;
private Vector2 pPos = new Vector2(100,100);
private float pRot = 1.0f;
Camera2d cam = new Camera2d();
private SpriteFont font;
private Vector2 camPos;
private float mapX;
private float mapY;
public Vector2 mapV;
private Texture2D player;
private Vector2 temp;
private float pVx = 0.0f;
private float pVy = 0.0f;
private float speed = 0.00f;
public class Camera2d
{
protected float zoom;
public Matrix transform;
public Vector2 pos;
protected float rot;
public Camera2d()
{
zoom = 1.0f;
rot = 0.0f;
pos = new Vector2(640,400);
}
public float Zoom
{
get {return zoom;}
set {zoom = value; if (zoom < 0.2f) zoom = 0.2f;}
}
public float Rotation
{
get {return rot;}
set {rot = value;}
}
public void Move(Vector2 amount)
{
pos += amount;
}
public Vector2 Pos
{
get { return pos;}
set {pos = value;}
}
public Matrix getTransformation(GraphicsDevice graphicsDevice)
{
transform = Matrix.Identity *
Matrix.CreateTranslation(new Vector3(-pos.X, -pos.Y, 0)) *
Matrix.CreateRotationZ(rot) *
Matrix.CreateScale(new Vector3(zoom, zoom, 1)) *
Matrix.CreateTranslation(new Vector3
(graphicsDevice.Viewport.Width *
0.5f, graphicsDevice.Viewport.Height *
0.5f, 0));
return transform;
}
}
public Vector2 ToWorldLocation(Vector2 position)
{
return Vector2.Transform(position, Matrix.Invert(cam.transform));
}
//here is my array
public Vector2 ComputeAdd(Vector2 first, Vector2 second)
{
Vector2 result = new Vector2();
result = (first + second);
return result;
}
public Vector2 ComputeSub(Vector2 first, Vector2 second)
{
Vector2 result = new Vector2();
result = (first - second);
return result;
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 800;
graphics.ToggleFullScreen();
graphics.ApplyChanges();
this.IsMouseVisible = true;
base.Initialize();
}
/// <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.
spriteBatch = new SpriteBatch(GraphicsDevice);
Tiles.Add(Content.Load<Texture2D>("Tiles/Black"));
Tiles.Add(Content.Load<Texture2D>("Tiles/DungeonFloor"));
Tiles.Add(Content.Load<Texture2D>("Tiles/DungeonFloor2"));
Tiles.Add(Content.Load<Texture2D>("Tiles/marble2"));
Tiles.Add(Content.Load<Texture2D>("Tiles/grass1"));
Tiles.Add(Content.Load<Texture2D>("Tiles/dirt1"));
Tiles.Add(Content.Load<Texture2D>("Tiles/rock1"));
Tiles.Add(Content.Load<Texture2D>("Tiles/mud1"));
Tiles.Add(Content.Load<Texture2D>("Tiles/wood1"));
///////////////////////////////////////////////////
font = Content.Load<SpriteFont>("Fonts/font");
/////////////////////////////////////////////////////
player = Content.Load<Texture2D>("Entity/Sprite-player1");
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
mouse = Mouse.GetState();
mousePos = new Vector2(mouse.X, mouse.Y);
mousePos = ToWorldLocation(mousePos);
temp = ComputeSub(pPos, mousePos);
pRot = (float)Math.Atan2(temp.Y, temp.X); // This returns the rotation angle correctly.
[size="5"] pVx = ((float)Math.Cos(pRot) * speed);
// i have removed the pVy code, but it was also from 'good stock' that worked.
pPos = new Vector2(pPos.X + pVx, pPos.Y + pVy); // Not causing an error yet. But does when i uncomment the above pVx/y code.
if (Keyboard.GetState().IsKeyDown(Keys.PageUp))
{
cam.Zoom += 0.02f;
}
if (Keyboard.GetState().IsKeyDown(Keys.PageDown))
{
cam.Zoom -= 0.02f;
}
if (Keyboard.GetState().IsKeyDown(Keys.OemPeriod))
{
cam.Rotation += 0.02f;
}
if (Keyboard.GetState().IsKeyDown(Keys.OemComma))
{
cam.Rotation -= 0.02f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Back))
{
cam.Rotation = 0.0f;
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
cam.Move(new Vector2(0, -5));
}
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
cam.Move(new Vector2(0, 5));
}
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
cam.Move(new Vector2(5, 0));
}
if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
cam.Move(new Vector2(-5, 0));
}
if (Keyboard.GetState().IsKeyDown(Keys.W))
{
speed += 0.07f;
}
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
spriteBatch.Begin(SpriteSortMode.FrontToBack,null,
null,
null,
null,
null,
cam.getTransformation(GraphicsDevice));
for (int y = 0; y < map.GetLength(0); y++)
{
for (int x = 0; x < map.GetLength(1); x++)
{
spriteBatch.Draw(Tiles[map[y, x]],
new Rectangle(x * tileSize,
y * tileSize,
tileSize,
tileSize),
Color.White); //want a way to change the colour value, based on a location of map. SO from pos can find elements in array and the iterate them to particular colours.
}
}
spriteBatch.Draw(player, pPos , null, Color.White, pRot, new Vector2(35, 35), 0.62f, SpriteEffects.FlipHorizontally, 1.0f);
spriteBatch.End();
spriteBatch.Begin();
spriteBatch.DrawString(font, "mousePosX " + mousePos.X, new Vector2(100, 200), Color.White);
spriteBatch.DrawString(font, "mousePosY " + mousePos.Y, new Vector2(100, 220), Color.White);
spriteBatch.DrawString(font, "camPosX " + camPos.X, new Vector2(100, 240), Color.White);
spriteBatch.DrawString(font, "camPosY " + camPos.Y, new Vector2(100, 260), Color.White);
spriteBatch.DrawString(font, "ppos " + pPos, new Vector2(100, 280), Color.White);
spriteBatch.DrawString(font, "pVx " + pVx, new Vector2(100, 300), Color.White);
spriteBatch.DrawString(font, "pVy " + pVy, new Vector2(100, 320), Color.White);
spriteBatch.DrawString(font, "speed " + speed, new Vector2(100, 340), Color.White);
spriteBatch.DrawString(font, "pRot " + pRot, new Vector2(100, 360), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}Im trying to simply move the sprite in the direction it is facing(dictated by the mouse position). If i put in code that ive used before, (basically finding the x and y velocities using the highlighted code) which has worked no problem in the past.
It's fairly simple code, but i cant find why it isnt working.
The highlighted code;
pVx = ((float)Math.Cos(pRot) * speed);
pVx = player X velocity
pRot = player Rotation in radians. (Which works fine)
speed is a float value, set to zero (but even setting it to 1 changes nothing, and speed has always started at zero in my other projects where this code worked)
I get Nan errors for pRot, and Ppos.X
Ive run the same code before and it's not been a problem, i just can't find the source of the problem, seems so simple.
Can anyone see why i would be getting a nan error?