• Create Account

## Help with a 2D Space Shooter!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

### #1Carlows  Members

141
Like
0Likes
Like

Posted 24 June 2013 - 01:29 PM

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.

### #2Álvaro  Members

20268
Like
3Likes
Like

Posted 24 June 2013 - 01:51 PM

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.

### #3Carlows  Members

141
Like
0Likes
Like

Posted 24 June 2013 - 02:05 PM

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.

### #4Álvaro  Members

20268
Like
0Likes
Like

Posted 24 June 2013 - 03:42 PM

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.

### #50r0d  Members

1575
Like
1Likes
Like

Posted 24 June 2013 - 07:21 PM

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.

### #6Edvinas Kilbauskas  Members

1167
Like
2Likes
Like

Posted 25 June 2013 - 04:25 AM

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, 25 June 2013 - 04:26 AM.

“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

5832
Like
2Likes
Like

Posted 25 June 2013 - 05:14 AM

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;


Edited by Paradigm Shifter, 25 June 2013 - 06:14 AM.

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

### #8Edvinas Kilbauskas  Members

1167
Like
1Likes
Like

Posted 25 June 2013 - 06:10 AM

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

5832
Like
0Likes
Like

Posted 25 June 2013 - 06:13 AM

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

### #10Carlows  Members

141
Like
0Likes
Like

Posted 25 June 2013 - 12:43 PM

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?

5832
Like
0Likes
Like

Posted 25 June 2013 - 01:12 PM

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.

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

### #12Waterlimon  Members

4373
Like
1Likes
Like

Posted 25 June 2013 - 02:59 PM

It can be called "unit"

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

o3o

### #13Norman Barrows  Members

6339
Like
1Likes
Like

Posted 26 June 2013 - 08:28 AM

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.

.

Norm Barrows

Rockland Software Productions

"Building PC games since 1989"

rocklandsoftware.net

PLAY CAVEMAN NOW!

http://rocklandsoftware.net/beta.php

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.