Advertisement

help me with that please....

Started by September 29, 2001 12:50 PM
13 comments, last by BiteThat 23 years, 4 months ago
i have a spaceship which u can move around on the screen (in ogl). so far so good, but now i want to make the spaceship shoot when the space key is pressed.. it should then shoot a small textured quad along the x axis.. how do i do that , i tried a lot of things but i did not work out how to do that... any (pseudo) code ? tnx in advance, biteTHAT
if (space=true)
shot=true;
if (shot=true)
xpos+=1;

glTranslatef(xpos, y, z);
Quad;

Advertisement
That''s odd you can move the spaceship but not the single textured quad?

NeHe''s tut #33 moves textured quads up & down and left & right on the screen. Try taking a look at that.

Basically gltranslatef(x,y,z) is the positioning command you want to use as the previous poster suggested.

If still having trouble try being a little more specific in what is going wrong with your current code.
the problem is i want to be able to shoot more than once, and also the shoots move when the ship is moved along y-axis,

or , could u check out my sourcecode ? just gimme ur email -addy and ill send u...

i think i undersatnd, when you move the spaceship, it moves the bullets as well? if thats true, when you possition the spaceship using gltranslatef(x,y,z), you need to reset opengl back to gltranslatef(0,0,0) before you call gltranslatef(x,y,z) again to position your bullet. to do this when you have positioned opengl to your spaceships x,y,z coordinates, and drawn the spacship, call gltranslatef(-x,-y,-z), this will move opengl back to point 0,0,0 then when you try and position the bullet it wont be affected by the position of the spaceship. sorry i cant really explain this very well, but heres what it should look like in code:

gltranslatef(spacship_x,spacship_y,spacship_z)
draw_spacship
gltranslatef(-spacship_x,-spacship_y,-spacship_z)

gltranslatef(bullet_x,bullet_y,bullet_z)
draw_bullet
gltranslatef(-bullet_x,-bullet_y,-bullet_z)

hope that helps if anyone understood it!
As for shooting more than once, you''d have to have a structure wich remembers each bullet''s position and direction, say like this (for 100 simultanious bullets on screen)

I''m assuming this is a 2D game here.

struct bullet{
GLfloat xpos,ypos,xdir,ydir;
bool active;
}

bullet shot[100]; //Variabledeclaraiton, maybe global?

Where xpos & ypos is where the bullet is and xdir & ydir is where it''s heading. then for each frame:

for(i=0;i<100;i++){
if(shot.active){
shot.xpos+=shot.xdir; //move the bullet<br> shot.ypos+=shot.ydir;<br> gltranslate(shot.x,shot.y,0); //set where to draw it<br> //draw bullet here<br> gltranslate(-shot.x,-shot.y,0);<br> }<br>}<br><br>Now, there is no rotation of the bullet, but you only wanted to shoot them in one direction right? Then I maybe did to much here, but something like this should work. </i>
Advertisement
As for shooting more than once, you''d have to have a structure wich remembers each bullet''s position and direction, say like this (for 100 simultanious bullets on screen)

I''m assuming this is a 2D game here.

struct bullet{
GLfloat xpos,ypos,xdir,ydir;
bool active;
}

bullet shot[100]; //Variabledeclaraiton, maybe global?

Where xpos & ypos is where the bullet is and xdir & ydir is where it''s heading. then for each frame:

for(i=0;i<100;i++){
if(shot.active){
shot.xpos+=shot.xdir; //move the bullet<br> shot.ypos+=shot.ydir;<br> gltranslate(shot.x,shot.y,0); //set where to draw it<br> //draw bullet here<br> gltranslate(-shot.x,-shot.y,0);<br> }<br>}<br><br>Now, there is no rotation of the bullet, but you only wanted to shoot them in one direction right? Then I maybe did to much here, but something like this should work. </i>
Joke/dST
gltranslatef(bullet_x,bullet_y,bullet_z)
draw_bullet
gltranslatef(-bullet_x,-bullet_y,-bullet_z)

translating back to 0,0,0 like that is quite slow. The proper and fast way to do it is

glPushMatrix();
gltranslatef(bullet_x,bullet_y,bullet_z);
draw_bullet
glPopMatrix();

Any translations or rotations you do after pushing a new matrix onto the stack is undone when you pop the matrix off.
Hi there, I recently made a 2D game with spaceships and shootin' stuff, crap but still
(check it out here if you want http://www.becko.freeserve.co.uk/Page4.htm )

This is how I did it:
DrawFire(); is called every frame

void DrawFire()
{
//Loop the same number of times as there are bullets so we can draw them all

for( BulletCounter=1; BulletCounter<=BulletNum; BulletCounter++)
{
if(!B_Jip[BulletCounter]) //If we don't Jip the bullet dues to a previous collision then draw it
{
FireMove[BulletCounter] = .05f; //Incrementing variable to move the bullets

Vertt[BulletCounter]+= FireMove[BulletCounter]; //Add the variable to the bullet's coordinates

glBindTexture(GL_TEXTURE_2D, texture[2]);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex2f( Horzz[BulletCounter]-1, (Vertt[BulletCounter]+2) );
glTexCoord2f(1,0); glVertex2f( Horzz[BulletCounter]+1, (Vertt[BulletCounter]+2) );
glTexCoord2f(1,1); glVertex2f( Horzz[BulletCounter]+1, (Vertt[BulletCounter]+4) );
glTexCoord2f(0,1); glVertex2f( Horzz[BulletCounter]-1, (Vertt[BulletCounter]+4) );
glEnd();

if( (Vertt[BulletCounter]) > 95.0f ) { B_Jip[BulletCounter]=true; }
Collision();
}

}
}

The B_Jip is a boolean variable which if true means the bullet is either offscreen or has hit an enemy and is destroyed (sorry, Jip is a colloquail term, dont even know how to spell it). Every time the shoot button is pressed BulletNum is incremented by 1. Vertt is the bullet's vertical position and by adding Firemove to it we can move it up the screen (in my game the action is a vertical type scroller though its easy to make it horizontal which you seem to want). The Collision(); at the bottom of the function is just when I test if any bullets have collided with enemies and the small code above it is if the bullet is offscreen we 'Jip' it. If you want me to post the source on my site just say though I must warn you it's not efficient or neat. I'm a beginner.

Finally, the reason I have -1,+1,+2,+4 beside my bullet's coordinates is so that my bullet is a little square.

Sorry if all my blabbing hasn't been of any use.



Edited by - AbeE on September 30, 2001 9:09:50 AM
yeah abee, that works ! thanks a lot,

BUT :

somewhere u have to decrease the BulletCounter, where do u do that ?

This topic is closed to new replies.

Advertisement