XNA Creators Club Online
Page 1 of 1 (9 items)
Sort Posts: Previous Next

use thumbstick to move and rotate to face movement direction?

Last post 01/05/2009 15:51 by donpreston. 8 replies.
  • 13/04/2009 18:17

    use thumbstick to move and rotate to face movement direction?

    Hi.
    I now have a menu on my game but have got stuck on making my gameobject move using the thumbstick and rotate to look in the direction it is going in.
    can anyone please tell me how to do this or of a link that tells me.
    Letting The Wolves Roam Free
  • 13/04/2009 21:37 In reply to

    Re: use thumbstick to move and rotate to face movement direction?

    I'm at work so I'll make this brief...

    You can get the direction you need to move by taking the x and y position of the thumbstick.
    Something like Vector2 direction = GamePad.GetState().Thumbsticks.Right;

    If you normalise the direction, you can then simply multiply by the object's speed (per frame) and add it to it's current position.

    so Vector2 newPosition = oldPosition + direction.Normalize() * speed;

    To get the facing, you can do a bit of trig:
    float rotation = Math.Atan2(direction.Y, direction.X);

    That will give you the rotation in radians, which you can use to create a rotation matrix (eg. Matrix.CreateRotationZ(rotation))

    I hope that points you in the right direction.

  • 18/04/2009 2:07 In reply to

    Re: use thumbstick to move and rotate to face movement direction?

    when i put

    player.rotation =

    Math.Atan2(direction.Y, direction.X);

     


    it comes up error
    cannot inplicity convert type double to float. an explicit conversion exists(are you missing a cast?)
    can you please assist
    Letting The Wolves Roam Free
  • 22/04/2009 4:13 In reply to

    Re: use thumbstick to move and rotate to face movement direction?

    player.rotation = (float)Math.Atan2(direction.Y, direction.X);
  • 23/04/2009 18:15 In reply to

    Re: use thumbstick to move and rotate to face movement direction?

    Thankyou.
    but when i move up i go down and the ship looks like its facing  right. does anyone know whay?
    it does the same for the other directions.
    Letting The Wolves Roam Free
  • 24/04/2009 2:47 In reply to

    Re: use thumbstick to move and rotate to face movement direction?

    yeah that would be expected. This code works in World space, not View space. If your camera angle is fixed then it is a simple matter of flipping / rotating the values.
    For instance, if you press up but it goes down, then you want to use Atan2(-direction.Y, direction.X);
    For the problem of facing left/right when pressing up/down, you need to add a constant rotation on to the result of the Atan2 method. This is quite simple if you remember that you are working with radians here. So a 90 degree rotation is MathHelper.PiOver2.

    eg. to rotate the ship by 90 degrees and flip the vertical direction, you would use
    float direction = (float)Math.Atan2(-direction.Y, direction.X) + MathHelper.PiOver2;

    You will need to mess with the values a bit to get it correct, but it all depends on where your camera is placed.
    Now, if you camera angle is NOT fixed, then things get a lot more tricky and it will require some further discussion.
  • 27/04/2009 22:25 In reply to

    Re: use thumbstick to move and rotate to face movement direction?

    so do you think that i should learn more before moving onto these sort of contols until more xp as i can only make games like the getting started 2D game but slightly more advanced then them (menus, moving, firing, 2 player, act.)
    Letting The Wolves Roam Free
  • 28/04/2009 5:15 In reply to

    Re: use thumbstick to move and rotate to face movement direction?

    That is something you will have to decide. If you feel confident enough to move in to new territory then go for it, if you're feeling apprehensive then put it on hold until you feel confident enough in your ability to tackle the problem. When I was learning, the best way I found to learn new stuff was to run through some samples and mess around with the code in them so you can visually see what your changes are doing.
  • 01/05/2009 15:51 In reply to

    Re: use thumbstick to move and rotate to face movement direction?

    I think i will put this project on hold will i learn some more. thankyou for all the help tho. :D
    Letting The Wolves Roam Free
Page 1 of 1 (9 items) Previous Next
var gDomain='m.webtrends.com'; var gDcsId='dcschd84w10000w4lw9hcqmsz_8n3x'; var gTrackEvents=1; var gFpc='WT_FPC'; /*<\/scr"+"ipt>");} /*]]>*/
DCSIMG