Help with a 2D Space Shooter!

Started by
11 comments, last by Norman Barrows 10 years, 10 months ago

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.

Advertisement


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.


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.


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.

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 sad.png, 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 1: Define your goal

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.

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.

“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”? Nigel Marsh

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;
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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);
“There are thousands and thousands of people out there leading lives of quiet, screaming desperation, where they work long, hard hours at jobs they hate to enable them to buy things they don't need to impress people they don't like.”? Nigel Marsh

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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?

This topic is closed to new replies.

Advertisement