Archived

This topic is now archived and is closed to further replies.

Zeus_

How to make my ship fire a laser?

Recommended Posts

Zeus_    122
I have a ship class, and every time the user hits the ''p'' key, the ship should emit a laser. I have code like this: player::fire(){ glPushMatrix(); glColor3f(0.0, 0.0, 1.0); glLineWidth(3); glBegin(GL_LINES); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glEnd(); for(float a=0.0; a<10.0; a+=0.05){ glTranslatef(0.0, a, 0.0);} glPopMatrix(); } But it doesn''t work... I want it so that whenever I hit the fire button, a little line appears, and translates to the top of the screen (as in out of site) along the y axis.. I cant seem to get this to work. Please help! ~Jesse

Share this post


Link to post
Share on other sites
Fruny    1658
You have to do the translation before the drawing. Plus, if you want an animation, you will have to do it over several frames. You cannot just put it into a single for loop.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
You need a shot class, such as:

class Shot
{
float x, y, z;

public:
Shot(float X, float Y, float Z): x(X), y(Y), z(Z) {}

void Update() {
glPushMatrix();
glColor3f(0.0, 0.0, 1.0);
glLineWidth(3);
glTranslatef(x, y, z);

glBegin(GL_LINES);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();

y += .05;
}

bool Done() {
return y == 10;
}
};

Then you can make a new Shot whenever your ship fires (keep a
list of the activve ones) then call Update on each frame and
remove them when Done == true

- Loki

Share this post


Link to post
Share on other sites
Zeus_    122
Uhh...

I saw that colon in there, where you put

Shot(float X, float Y, float Z): x(X), y(Y), z(Z) {}

...

I have never seen a colon used like that.. Honestly.

In 5 years of programming, I have never seen that. And it still compiled/executed perfectly.

Could you tell me what that means?!! And maybe the syntax? Whats it used for!?

Thanks.

I fell really stupid...

Share this post


Link to post
Share on other sites
Beer Hunter    712
It does look wierd... that''s not your fault

It''s purpose is simply to assign values to class members. It''s like saying:

x = X; y = Y; z = Z;

It''s generally done the "wierd" way, though. I''m not sure of the reason it''s done that way, but I''m sure someone can explain.

Share this post


Link to post
Share on other sites
AdmiralBinary    100
An initialization list calls the constructors of the members listed there with the parameters u pass to them. For instance:

class Number
{
public:
Number(int newval) { val = newval; }
int val;
};

class Word
{
public:
Word(string newval) { val = newval; }
string val;
};

class Pair
{
public:
Pair():a(5),b("Hello!") {}
Number a;
Word b;
};

Sometimes useful for those who dote on OOOP (Over-Object-Oriented-Programming).

---------------

I finally got it all together...
...and then forgot where I put it.

Share this post


Link to post
Share on other sites