View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

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.

11 replies to this topic

### #1phil67rpg  Members

Posted 12 August 2013 - 04:05 PM

I am programming a space invaders clone. I want to use a vector to move the aliens across the screen. I am unsure of how to proceed. I am studying vectors.

### #2David.M  Members

Posted 12 August 2013 - 09:30 PM

Are you wanting to use std::vector or a mathematical vector?

Edit: Only a mathematical vector would make sense really.

A vector has a direction and a magnitude. This Wolfram Alpha entry describes vectors. You can use vectors to specify an object's velocity and acceleration. What are you hung up on or having issues with?

Edited by David.M, 12 August 2013 - 09:35 PM.

### #3EarthBanana  GDNet+

Posted 12 August 2013 - 09:40 PM

well if you have a velocity vector of some speed at some angle - then you would multiply that speed by the amount of time you want the alien to move for that frame and then you would add that movement to the aliens current position (keeping in mind the angle).. practically it might go something like this..

Alien is at x = 0 and y = 0 .. you want the alien to move at 10 pixels per second in the 45 degree angle (from top left of screen to bottom right of screen)

ill call your time elapsed for the frame dt

lets say your screen is 1920 by 1080

so if this frame dt = .014 seconds to keep the speed 10 pixels per second the alien needs to move 10 pixels/sec * .014 sec = 0.14 pixels (assuming you are using float for the calculation) in the 45 degree direction..

so moving it in the 45 degree direction is the bit that is a tiny bit more tricky but not really.. since you are looking for the x and y lengths of a right triangle with a hypotenuse = 0.14 pixels and one angle = 45 degrees you know that x must = y and therefore by Pythagorean's theorem :

x^2 + y^2 = 0.14^2       :    since x = y

2(x^2) = 0.14^2    : solving for x

x = sqrt( 0.14^2 / 2)

and so you would shift x and y by sqrt( (speed * dt)^2 / 2) if you were at a 45 degree angle

however for more general angles you know that

sin (angle) = y / (speed * dt )      and

cos ( angle )  = x / (speed * dt)

so to find how much x and y positions should change for a frame that took dt time for a given speed and angle (which remember speed at an angle is a velocity vector) you solve for x and y

y = speed * dt * sin(angle)

x = speed * dt * cos(angle)

in the case of moving across the screen I would add this x and y amount to your aliens position every frame until its position was equal to 1920 by 1080

something like that

### #4phil67rpg  Members

Posted 12 August 2013 - 10:05 PM

wow that is a lot of math, I will brush up on my vectors, I was told to use the c++ vectors to move the alien sprites.

### #5EarthBanana  GDNet+

Posted 13 August 2013 - 12:09 AM

lol well c++ vectors are much different than the vectors I thought you were talking about

c++ vectors are just containers for data - so using them to move aliens doesn't exactly make sense.. but I bet if you were told to use vectors to move aliens it meant store your aliens in a vector, and then traverse that vector to move the aliens

here is a small example that should compile to show the idea

#include <iostream>
#include <vector>

class Alien
{
public :
Alien(int startX, int startY, int Id);
void move( int xAmount, int yAmount);
void draw();
int getX();
int getY();
int getID();

private:
int xPos;
int yPos;
int ID;
};

Alien::Alien(int startX, int startY, int Id): xPos(startX), yPos(startY), ID(Id)
{}

void Alien::move(int xAmount, int yAmount)
{
xPos += xAmount;
yPos += yAmount;
}

void Alien::draw()
{
std::cout << "\nAlien " << ID << " is now at x : " << xPos << "   and y : " << yPos;
}

int Alien::getX()
{
return xPos;
}
int Alien::getY()
{
return yPos;
}

int Alien::getID()
{
return ID;
}

void EachFrame(std::vector<Alien> & aliens, int amountToMove)
{
std::cout << "Moving aliens" << std::endl;
std::vector<Alien>::iterator iter = aliens.begin(); // using vector iterators is always a good idea
while (iter != aliens.end()) // while the begin iterator does not = the end iterator
{
iter->move(amountToMove, 0); // Move the alien in the x direction by amountToMove - dont move in the y direction
iter->draw();
++iter; // dont forget to increment the iterator - if at the end of the vector then it will be incremented
// to the "end" iterator which will end the while loop
}
}

int main()
{
int inputHold;  // just a dummy variable to keep the prompt openm after execution

const int ALIEN_COUNT = 10;  // Number of aliens to move
const int ALIEN_Y_SPACING = 5;  // How much to offset each alien vertically'
const int MOVE_PER_FRAME = 5;  // amount to move aliens per frame
const int SCREEN_WIDTH = 1920;
int currentMovement = 0; // The amount the aliens have moved so far

bool GameLoop = true;

std::vector<Alien> aliens; // forward declaration of the aliens vector - you are creating a vector here that holds your
// own custom type (Alien)

for (int i = 0; i < ALIEN_COUNT; ++i)     // Fill a vector full of ALIEN_COUNT aliens each spaced vertically by the above spacing
aliens.push_back(Alien(0,i*ALIEN_Y_SPACING,i)); // Add an alien to the vector at x = 0, y = i * spacing variable, and the alien ID = i

while (GameLoop) // While in the game keep moving the aliens across the screen
{
EachFrame(aliens, MOVE_PER_FRAME); // Call the EachFrame function passing in a reference to the vector and how much
// each alien should move
if ( currentMovement >= SCREEN_WIDTH ) // if the amount the aliens have moved has reached the screen width
GameLoop = false;              // then set the game loop to false
currentMovement += MOVE_PER_FRAME;
}
std::cout << "Aliens moved across screen" << std::endl;
std::cin >> inputHold;
}


Edited by EarthBanana, 13 August 2013 - 12:16 AM.

### #6WhiskyJoe  Members

Posted 13 August 2013 - 02:27 AM

Math stuff

Although you're right, this is very likely a bit overwhelming considering his response. Also considering that the original space invaders only moved sideways and down, the explanation about how to incorporate angles is a bit to much I think. It's still something you want to know in the end, but perhaps this is not the time for a (what seems) beginner.

Anyway, You should get an idea of what a vector is both in it's "container" and mathematical form. Very likely when you're talking about movement you are talking about the mathematical form, but to make it a bit more understandable, you should just consider a point.

You should see your window as a graph where X is the horizontal part and Y is the vertical part and both are an X amount in length. (800 x 600 for example) When you position your alien on 400 x 300 it should be in the middle of your screen. When you now increment the X value (which is 400) by 10 for example, the new position would become 410 x 300. The alien is simply a point on this graph.

To make sure your aliens don't go off the screen, check against the boundaries of your screen. you know that your screen is 800 x 600. So if an alien gets over that, you know it's going out of screen and you should do something about it. Move them a row down if it's on the horizontal axis or game over on the vertical axis (whatever you choose).

Last thing to keep in mind is the framerate. You probably heard of the term "FPS" already, Frames Per Second. If you increment your aliens position by 10 every frame and your FPS is 60, the alien will move 600 pixels each second. This is why you calculate the time between frames (the deltatime or DT) and use that in your movement code. This is a topic that has been covered multiple times on this forum and a simple search will give you the proper explanation you need.

Good luck!

### #7phil67rpg  Members

Posted 13 August 2013 - 12:08 PM

your  right I am a beginner, maybe space invaders may a little too complicated for me, should I try something easier like pong?

### #8BeerNutts  Members

Posted 13 August 2013 - 01:53 PM

Why are you using OpenGL to write a simple 2d space invaders clone?  Why not use an API made for 2D games, instead of over-complicating things?  Check out SFML for a simple API for graphics, sound, Input, windows, system, and , networking.

Space invaders isn't too complicated.  I would use a 2 dimensional array to store the alien's class data (since, in space invaders, the aliens are aligned in a grid).  The only thing a vector might require is for alien bullets sinec there could be any number of bullets on screen at one time, but my recollection of space invaders is it only allows 1 of the player's bullet on screen at a time, so It doesn't need a vector.  Also, I recall the bullets (both the players and the aliens) simply go up or down, so there isn't any need for "a lot of math."

Have all the aliens move along the x-axis every so often (but speeding up as time goes on), and when it reaches some X boundary, shift them down vertically some amount and move them the other way.  When a bullet hits one, it disappears (basically, marking some variable in the alien's class, say, IsDead  = false), and don't draw aliens that are dead.  When they're all dead, go to next level.

Have some aliens be able to drop some bullets, and if it hits the player, lose a life, and start at that point.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

### #9WhiskyJoe  Members

Posted 13 August 2013 - 03:14 PM

+1 for what BeerNutts said.

Space invaders is also the first game I programmed, but I used a framework that already provided me with drawing sprites to take a lot of stuff I needn't worry about yet out of my hands so I could focus on the fundamentals of game programming itself.

Check out SFML or SDL for some nice APIs you can work in.

I used the framework provided in this tutorial (http://devmaster.net/posts/2843/introduction-to-c-with-game-development-part-1-intro). I attend this college so we had to work in it at the start. Perhaps it's also a tutorial you can benefit from, but there are plenty of SFML and SDL tutorials out there as well that provided more functionality.

### #10phil67rpg  Members

Posted 13 August 2013 - 04:30 PM

well I don't have any books on sfml or sdl. I wrote my breakout game using opengl and I took a class in opengl in college.I have also found a good site that teaches opengl called videotutorialsrock.com

### #11phil67rpg  Members

Posted 13 August 2013 - 04:34 PM

btw school starts tomorrow so I wont have much time for fun stuff like programming, but thanks for all the help this summer.

### #12Edvinas Kilbauskas  Members

Posted 14 August 2013 - 09:25 AM

well I don't have any books on sfml or sdl. I wrote my breakout game using opengl and I took a class in opengl in college.I have also found a good site that teaches opengl called videotutorialsrock.com

Why you need books on SFML? You can find plenty of resources on the net. https://github.com/LaurentGomila/SFML/wiki/Tutorials for example. SFML is very easy to pick up, you can be a pro in just a week.

“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

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.