Sign in to follow this  

shooting

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

If you intended to correct an error in the post then please contact us.

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[i].active==false)
       {
          bullets[i].x=objCamera.mPos.x;
          bullets[i].y=objCamera.mPos.y;
          bullets[i].z=objCamera.mPos.z;
          }
       if(bullets[i].active==true)
       {
          bulletdifx = bullets[i].x-bullets[i].targetx;
          bulletdify = bullets[i].y-bullets[i].targety;
          bulletdifz = bullets[i].z-bullets[i].targetz;
          if(bulletdifx>0)
          {
             bullets[i].x+=0.1;
             }
          if(bulletdifx<0)
          {
             bullets[i].x-=0.1;
          }
          if(bulletdify>0)
          {
             bullets[i].y+=0.1;
          }
          if(bulletdify<0)
          {
             bullets[i].y-=0.1;
          }
          if(bulletdifz>0)
          {
             bullets[i].z+=0.1;
          }
          if(bulletdifz<0)
          {
              bullets[i].z-=0.1;
          }
       glColor3f(0.3f,0.3f,1.0f);
       glBegin(GL_QUADS);
       glVertex3f(bullets[i].x,bullets[i].y,bullets[i].z);
       glVertex3f(bullets[i].x+10,bullets[i].y,bullets[i].z);
       glVertex3f(bullets[i].x+10,bullets[i].y+10,bullets[i].z);
       glVertex3f(bullets[i].x,bullets[i].y+10,bullets[i].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 this post


Link to post
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 this post


Link to post
Share on other sites
made another attempt, to no avail.

for(int i=0; i<1000; i++)
{
bullets[i].startx=0;
bullets[i].starty=0.5;
bullets[i].startz=0;
if(bullets[i].active==true)
{
bullets[i].dx=bullets[i].startx-bullets[i].viewx*bulletspeed;
bullets[i].dy=bullets[i].starty-bullets[i].viewy*bulletspeed;
bullets[i].dz=bullets[i].startz-bullets[i].viewz*bulletspeed;
bullets[i].x+=bullets[i].dx;
bullets[i].y+=bullets[i].dy;
bullets[i].z+=bullets[i].dz;
glBegin(GL_QUADS);
glVertex3f(bullets[i].x,bullets[i].y,bullets[i].z);
glVertex3f(bullets[i].x+1,bullets[i].y,bullets[i].z);
glVertex3f(bullets[i].x+1,bullets[i].y+1,bullets[i].z);
glVertex3f(bullets[i].x,bullets[i].y+1,bullets[i].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 this post


Link to post
Share on other sites

//...
bullets[i].dx=bullets[i].startx-bullets[i].viewx*bulletspeed;
bullets[i].dy=bullets[i].starty-bullets[i].viewy*bulletspeed;
bullets[i].dz=bullets[i].startz-bullets[i].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 this post


Link to post
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)

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this