# shooting

This topic is 3938 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm having trouble shooting in my fps. I've made an attempt, it compiles, but nothing shoots... my camera uses glulookat and is camera 3b from here: http://www.morrowland.com/apron/tut_gl.php It's down near the middle. bullets structure:
struct bullet {
float x;
float y;
float z;
bool active;
float targetx;
float targety;
float targetz;
};
bullet bullets[1000];


Trying to get it to shoot when the left mouse button gets pressed.
	for(int i; i<1000; i++)
{
if(bullets.active==false)
{
bullets.x=objCamera.mPos.x;
bullets.y=objCamera.mPos.y;
bullets.z=objCamera.mPos.z;
}
if(bullets.active==true)
{
bulletdifx = bullets.x-bullets.targetx;
bulletdify = bullets.y-bullets.targety;
bulletdifz = bullets.z-bullets.targetz;
if(bulletdifx>0)
{
bullets.x+=0.1;
}
if(bulletdifx<0)
{
bullets.x-=0.1;
}
if(bulletdify>0)
{
bullets.y+=0.1;
}
if(bulletdify<0)
{
bullets.y-=0.1;
}
if(bulletdifz>0)
{
bullets.z+=0.1;
}
if(bulletdifz<0)
{
bullets.z-=0.1;
}
glColor3f(0.3f,0.3f,1.0f);
glVertex3f(bullets.x,bullets.y,bullets.z);
glVertex3f(bullets.x+10,bullets.y,bullets.z);
glVertex3f(bullets.x+10,bullets.y+10,bullets.z);
glVertex3f(bullets.x,bullets.y+10,bullets.z);
}
}


and input:
    if(keys[VK_LBUTTON])
{
bullets[a].active=true;
bullets[a].targetx=objCamera.mView.x;
bullets[a].targety=objCamera.mView.y;
bullets[a].targetz=objCamera.mView.z;
a++;
}


thanks for spending your time to check this!

##### Share on other sites
Skimming yur code, I'm not sure what you were trying to get at, but I hope this helps:
Imagine the bullet is the same thing as your camera, but going further toward where you are facing. If you make the x,y,z for the bullet the same as the camera, then use the same moving variables you use for your camera, but turn them into usable forms for your bullets.

##### Share on other sites
alright, ya got me thinking. i'll get back to you on what I get!

##### Share on other sites
made another attempt, to no avail.
	for(int i=0; i<1000; i++)	{       bullets.startx=0;       bullets.starty=0.5;       bullets.startz=0;       if(bullets.active==true)       {          bullets.dx=bullets.startx-bullets.viewx*bulletspeed;          bullets.dy=bullets.starty-bullets.viewy*bulletspeed;          bullets.dz=bullets.startz-bullets.viewz*bulletspeed;          bullets.x+=bullets.dx;          bullets.y+=bullets.dy;          bullets.z+=bullets.dz;          glBegin(GL_QUADS);          glVertex3f(bullets.x,bullets.y,bullets.z);          glVertex3f(bullets.x+1,bullets.y,bullets.z);          glVertex3f(bullets.x+1,bullets.y+1,bullets.z);          glVertex3f(bullets.x,bullets.y+1,bullets.z);          glEnd();          }}

dx,dy,and dz are parts of a direction vector that isn't really declared, but they act as parts of a vector.
	if((GetKeyState(VK_LBUTTON) & 0x80))	{        bullets[a].active=true;        bullets[a].viewx=objCamera.mView.x;        bullets[a].viewy=objCamera.mView.y;        bullets[a].viewz=objCamera.mView.z;        a++;    }

sets the bullets view as the camera's view at the time of shooting.

##### Share on other sites
//...        bullets.dx=bullets.startx-bullets.viewx*bulletspeed;          bullets.dy=bullets.starty-bullets.viewy*bulletspeed;          bullets.dz=bullets.startz-bullets.viewz*bullets//...

Why are you subtracting?
I'm still not entirely sure if you get my point. Treat the bullets as several different cameras, but they go in the path it is angled towards. Use the same camera code, but during your step events, make the bullet path increase in the direction.

##### Share on other sites
Here's how I fire projectiles.

I see you have an array of bullets, which is great. One of the important concepts is to try and set up your graphical objects in advance of when you will need them.

Important properties:

velocity - how fast will the projectile travel?
position - where is the projectile now?
heading - where is the projectile heading? this is a normalized vector
timeout - how long will the bullet travel before being deactivated?
(as on option, you can use distance for your timeout)

Ok, now its time to fire. I do these things:

1) grab an available projectile
2) set its position to be that of the camera (or whatever object is pulling the trigger)
3) set its heading the same way. (normalized vector)
4) activate the projectile

Ok, so now you have an active projectile. Its in the right spot (position of camera) and its pointing the right direction (heading of camera).

In your game, you probably have whats commonly called the 'game loop'. At the beginning of the rendering phase of the game loop, you probably set up your camera, and position your world objects, right?

Near this area of code is where you will want to animate your projectiles. One way to do this is determine how much time has elapsed since the previous animation cycle, and use that number for deciding how far to move your projectile. The goal of this calculation is to come up with a number which represents a fraction of a second.

Take a copy of the heading vector and scale it down using the time fraction you just created. Now, scale it back up again by multiplying by your velocity. Then, add that adjusted heading to the current position. Your projectile has now moved a small amount.

NewPosition = Position + (Heading * Elapsed * Velocity).

Once you have this set up, your projectile will be moved by a small amount every time your game loop runs. The next thing to add to the animation loop might be the 'timeout' logic, so that the projectile STOPS moving eventually, and is reset to an 'available' state.

hope that helps!

oh, one more hint. make sure you know what your projectile heading TRULY is. If the projectile heads the wrong way, you'll never see it on the screen. Also, use small numbers at first for velocity, so you can see it moving. And, use large numbers for your bullet size, so that you can see the thing. Once you have your logic worked out for velocity, position and heading, you can shrink down your projectiles and speed them up!

(edited because I spaced out velocity)

1. 1
Rutin
19
2. 2
JoeJ
15
3. 3
4. 4
5. 5

• 24
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001778
×