2 Replies - 6950 Views - Last Post: 01 March 2012 - 10:48 AM

#1 weddi_eddy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 04-September 11

XNA Moving Sprite Between Two Locations

Posted 04 September 2011 - 06:43 AM

Hello there,

I'm trying to create a game using XNA. I'm new to developing and I'm having trouble trying to get a desired effect for my character.

What I want is to be able input a command where the sprite moves from position 1 to position 2. If the same or another command is input, I want to be able to move the sprite from position 2 back to one. I have managed to get this working using some if commands, however this flashes the sprite to the position. I would like to move the sprite slowly from one position to the next.

I tried using some
public enum VerticalLevel
{
top,
middle,
bottom,
}


to be able to use for each position. This is in the character.cs. I'm still having trouble creating the smooth transitions. Is anyone able to help?

Is This A Good Question/Topic? 0
  • +

Replies To: XNA Moving Sprite Between Two Locations

#2 bonyjoe  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 178
  • View blog
  • Posts: 548
  • Joined: 08-September 10

Re: XNA Moving Sprite Between Two Locations

Posted 04 September 2011 - 07:12 AM

You would need the character to have some sort of speed variable, then you would calculate the direction between point A and point B, normalise the vector. Then adding that vector * speed to your characters location each step will move it smoothly.

You would need to check if it has reached the new position so that you can stop moving it as well.

If any of this sounds complicated you should probably read up on vectors and possibly other linear algebra.
Was This Post Helpful? 2
  • +
  • -

#3 Hernaldo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-February 12

Re: XNA Moving Sprite Between Two Locations

Posted 01 March 2012 - 10:48 AM

In http://forums.create...3690/71746.aspx appear the solution in the Andrew Broadbent post:

Quote

ust out of interest, did you want your sprite to move directly to the location? because the code above will not create a direct path to do that you will need a line drawing algorithm, or you could simply move the sprite with a vector:

get a vector from the sprite to the location; Vector2 velocity = Vector2.Subtract(vector location/destination, vector spritePos)

Normalize this vector: velocity.Normalize();

multiply this vector by the speed you want the sprite to move at: velocity = Vector2.Multiply(velocity, desiredSpeed)

add this velocity to the sprites position every frame: spritePos = Vector2.Add(spritePos, velocity);


This solution works in my proyect!

Bye!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1