Orienting a Ship With the Right Stick

Started by
15 comments, last by Lunar180 14 years, 1 month ago
I'm trying to make a 360 degree, two-dimensional shooter, but I can't figure out how to make a ship point in the direction that I point the right stick on a 360 controller. I'm programming in C# with XNA.
----------It will blow your freaking mind.
Advertisement
Heya! Here's how...

#1 - Get the X and Y values of the stick
#2 - I believe they give you a "short" back (not a float) so convert the short to a float and map it from -1 to 1 on each axis. IE I forget what the max and min values it returns is but lets say it is -32767 to 32768. When you get a value back of say -1224, you divide it by 32768 to get -0.03
#3 - use Math.Atan2 to get the angle (in radians, not degrees) that they are pressing on the stick
#4 - rotate your sprite by this angle
#5 - PROFIT

(:

PS - if you are also using the stick to MOVE the player, you can use the 2 floats you got from the stick in step 2 as a vector of movement. Multiply those 2 floats by your maximum player movement speed and you have your X and Y movement vectors.

PPS - if you want to make your game feel more sophisticated, instead of setting the angle to the angle on the stick, interpolate the current angle to the angle of the stick... IE move the current angle to the destination angle over time (a small amount of time like 1/10th of a second). This will give it a more polished feeling and if the interpolation time is short enough it shouldn't interfere with game play.

Hope this helps!
Since you are in XNA, your values are already from -1,1


Don't need to convert back to degrees, I beleive the spriteBatch.Draw call gets a radiant in parameter.
spriteBatch.Draw(yourSpriteTexture, position, ..., [float rotation] Math.Atan2(x, y), ...)
Thanks, both of you, for the help. But I have another question. Why can't I get my projects to recognize when the BigButton is pressed? I thought the BigButton was the big button with the X on it on the center of the controller, but pushing that button didn't seem to have any effect. It didn't even go into the if statement I had for when the button is pushed.

Oh, and is there any code to close the Home menu? I'm trying to make the BigButton open and close the account menu.

EDIT: Oh, and I've come across a NullReferenceException that I can't figure out.
            tsb.Draw(shipTexture, location, null, Color.White, (float)angle, new Vector2(shipWidth * .5f, shipHeight * .5f), 1, SpriteEffects.None, 0f);

The literally "null" parameter is supposed to mean that I want to draw the entirety of my shipTexture, so I don't think that is what's causing my problem.

[Edited by - Lunar180 on February 24, 2010 12:29:50 PM]
----------It will blow your freaking mind.
What is the class of tsb? What parameters is its .Draw function supposed to take?

Could shipTexture be null? Where does it come from?

Could location be null? (probably not; what type is it?)
Ack, sorry. I forgot to mention those.
tsb is a temporary SpriteBatch, shipTexture wasn't null when I was in debug, and location was not null either (it is a Vector2)

I suppose it wouldn't hurt to just show the whole method
public override void Draw(GameTime gameTime)        {            shipHeight = shipTexture.Height;            shipWidth = shipTexture.Width;            SpriteBatch tsb = (SpriteBatch)mahGaem.Services.GetService(typeof(SpriteBatch));            tsb.Draw(shipTexture, location, null, Color.White, (float)angle, new Vector2(shipWidth * .5f, shipHeight * .5f), 1, SpriteEffects.None, 0f);            base.Draw(gameTime);        }


Draw (in this case) uses a Texture2D, Vector2 (position), Rectangle? (what section of texture, null if all), color (tint), float (rotation), Vector2 (origin), float (scale), SpriteEffects, float (layer depth)

Anything I've missed clarifying?
----------It will blow your freaking mind.
Can anyone at least point me in the right direction for my BigButton problem?
----------It will blow your freaking mind.
I think the big button refers to the big button on the big button control pad. The one that came with Scene it. I don't think you can map the xbox button as Microsoft surely wants to keep it to themselves to access the mini-dash on the 360.

Could be wrong though.
my blog contains ramblings and what I am up to programming wise.
Hi Lunar180,

if you don't know it already, you should read this:

MSDN Link ;

Is your project a windows or a xbox360 game?


- GWDev
Quote:Original post by Imgelling
I think the big button refers to the big button on the big button control pad. The one that came with Scene it. I don't think you can map the xbox button as Microsoft surely wants to keep it to themselves to access the mini-dash on the 360.

Could be wrong though.


Well, I was just trying to make the BigButton/XBox button open/close the mini-dash on the computer, but pushing it did nothing. Yeah, I'm not talking about sceneIt's big button pad. I tried looking up my problem before, and about 90% of my results were that.

Quote:Original post by GWDev
Hi Lunar180,

if you don't know it already, you should read this:

MSDN Link ;

Is your project a windows or a xbox360 game?


- GWDev


Windows. Thinking about it, the BigButton would automatically have the regular function if I decide to convert to a 360 game. And that guide doesn't help me with my problem. I am using the BigButton property.
----------It will blow your freaking mind.

This topic is closed to new replies.

Advertisement