Public Group

# Orienting a Ship With the Right Stick

This topic is 3156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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.

##### Share on other sites
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!

##### Share on other sites

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), ...)

##### Share on other sites
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]

##### Share on other sites
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?)

##### Share on other sites
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?

##### Share on other sites
Can anyone at least point me in the right direction for my BigButton problem?

##### Share on other sites
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.

##### Share on other sites
Hi Lunar180,

Is your project a windows or a xbox360 game?

- GWDev

##### Share on other sites
Quote:
 Original post by ImgellingI 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 GWDevHi 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.

1. 1
Rutin
44
2. 2
3. 3
4. 4
5. 5

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632997
• Total Posts
3009790
• ### Who's Online (See full list)

There are no registered users currently online

×