# Help with a 2D Space Shooter!

## Recommended Posts

I'm wanting to start a little project (I'm a beginner), about a Space Shooter, but, I need help with a few things...

The first thing is implementing the shooting, I mean, the bullets will go at the mouse direction, how can I implement the angles of shooting?

The second is, how to implement a simply mini-map? Just showing where my ship is, some bases, etc...

Lol, I wish I could explain a lot better but, English isn't my first language :(, anyway, any help, link, tutorial, will be appreciated!

I'm using SFML and C++ btw.

##### Share on other sites
alvaro    21246

The first thing is implementing the shooting, I mean, the bullets will go at the mouse direction, how can I implement the angles of shooting?

Don't use angles: Compute the vector from the shooter to the mouse and normalize it (i.e. divide it by its length). Then advance the position of the bullet by adding that vector multiplied by the bullet's speed.

The second is, how to implement a simply mini-map? Just showing where my ship is, some bases, etc...

This sounds fairly straight forward. You need to be more specific about what problem you are having.

##### Share on other sites

This sounds fairly straight forward. You need to be more specific about what problem you are having.

I have not started It yet, I just want a simple idea, just for know where to start.

##### Share on other sites
alvaro    21246

This sounds fairly straight forward. You need to be more specific about what problem you are having.

I have not started It yet, I just want a simple idea, just for know where to start.

Well, write down in steps how you would perform the task of displaying a minimap, then code it. If there is a part you are having trouble with, feel free to ask. But please make an honest effort before you ask us to spend any time on it.

##### Share on other sites
0r0d    1932

I'm wanting to start a little project (I'm a beginner), about a Space Shooter, but, I need help with a few things...

The first thing is implementing the shooting, I mean, the bullets will go at the mouse direction, how can I implement the angles of shooting?

The second is, how to implement a simply mini-map? Just showing where my ship is, some bases, etc...

Lol, I wish I could explain a lot better but, English isn't my first language , anyway, any help, link, tutorial, will be appreciated!

You need to have specific questions about specific problems, or otherwise the answers you're going to get will be very general and will not help you achieve what you want to achieve.

Step 2: Break down goal into smaller steps, and repeat until the steps are small enough to be manageable.

Step 3: Complete each step.  If you have a problem with one, ask for help.

Step 4: Done!

Right now it seems like you're somewhere between steps 1 and 2.

##### Share on other sites
Pufixas    1167

As I see no one is willing to give you straight forward answer. But I think I can answer to your first question, about angles and shooting.

This is how you do it:

sf::Vector2f mousePos = your mouse position vector;
sf::Vector2f shipPos = you ship position vector;
sf::Vector2f bulletVelocity();
sf::Vector2f bulletPosition();

float shotAngle = atan2(mousePos.y - shipPos.y, mousePos.x - shipPos.x);

bulletVelocity.x = cos(shotAngle);
bulletVelocity.y = sin(shotAngle);

bulletPosition.x += bulletVelocity.x;
bulletPosition.y += bulletVelocity.y;



This should get you started. I can't really tell more, because it would kill all the point of the programming in the first place - which is problem solving.

You should go google some vector math, get comfortable with those before you dig deeper into game programming.

Edited by Edvinas Kilbauskas

##### Share on other sites

You don't need trig to do that. Alvaro already said what they need to do.

sf::Vector2f mousePos = your mouse position vector;
sf::Vector2f shipPos = you ship position vector;
sf::Vector2f bulletVelocity(mousePos.x - shipPos.x, mousePos.y - shipPos.y); // see comment in next post
sf::Vector2f bulletPosition = bullet position;

bulletVelocity.normalize();

bulletVelocity *= bulletSpeed;

bulletPosition.x += bulletVelocity.x;
bulletPosition.y += bulletVelocity.y;


##### Share on other sites
Pufixas    1167

You are right, but just change

sf::Vector2f bulletVelocity(mousePos.y - shipPos.y, mousePos.x - shipPos.x);


to

sf::Vector2f bulletVelocity(mousePos.x - shipPos.x, mousePos.y - shipPos.y);


##### Share on other sites

Yep, I just copy/pasted your code and didn't check that bit ;)

##### Share on other sites

You don't need trig to do that. Alvaro already said what they need to do.

sf::Vector2f mousePos = your mouse position vector;
sf::Vector2f shipPos = you ship position vector;
sf::Vector2f bulletVelocity(mousePos.x - shipPos.x, mousePos.y - shipPos.y); // see comment in next post
sf::Vector2f bulletPosition = bullet position;

bulletVelocity.normalize();

bulletVelocity *= bulletSpeed;

bulletPosition.x += bulletVelocity.x;
bulletPosition.y += bulletVelocity.y;


I solved it with a little difference, just one question, there you use a normalize function (.normalize()), Is that function on the sfml's headers?

##### Share on other sites

I dunno, it was pseudo-code-esque. I'm not familiar with sfml.

I'm sure there will be a normalize function somewhere, maybe not a method of sf::Vector2f though... Or work out the length of the vector and divide by the length.

##### Share on other sites
Waterlimon    4398
It can be called "unit"

or you can divide the vector by itself length (/magnitude)

##### Share on other sites
Norman Barrows    7179

for a mini map or look down radar, you'll draw in a manner similar to drawing the screen, but at much smaller scale.

so lets say you're running at 800x600, and your "world" or "level" is 2x2 screens (1600x1200 pix).

so you'd take the world coords of everything you want to draw in the mini map and divide by 10. that way your minimap is 160x120 pix in size (20% of the width and height of the screen in size).

add an x and y offset to each object on the map to effectively move the UL corner of the mini map where you want it on the screen and you're done.

then just draw a symbol or colored dot representing the object at the coords you've figured out:

minimap_x = world_x / 10 + minimap_xoffset;

minimap_ y = world_y / 10 + minimap_yoffset;

that divide by 10 is just an example.

it will actually be divide by "scale of world compared to minimap"

so if 1 pix on the minimap = 100 pix in the world, it would be divide by 100, and so on.

then you can get fancy and rotate objects around the player's position on the mini map based on the player's direction, so the mini map "turns" with the player.

.