• ### Announcements

#### Archived

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

# Bullet movement

## Recommended Posts

hey everybody. I am writing a 3d first person shooter type game using OpenGL. I am having trouble finding an algorithm to calculate the x and z of where the bullet should be. ok, here''s what I have so far. My bullet data structure
  struct tag_bullet { int x,y,z; int counter; }Bullet, *buller_ptr; Bullet bullet1; 
ok, the x,y, and z should be self explanatory. (the y stays constant because the bullet won''t move up or down, for my purposes) After each loop of the game the bullet1.counter variable will be incremented by 1. This will represent the movement away from the player that the bullet is moving. I want to use this variable for calculating later. The player has a direction (in degrees 0-360) that he is facing. Let''s say he''s facing 45 degrees. How do I calculate the x and z coordinates of the bullet with this information? Do I need more info to calculate this? Thanks, Joe

/ -two increments
/ -one increment
x -the player

JoeMont001@aol.com

#### Share this post

##### Share on other sites
if F is the angle the player is facing, then

x = cos(F)*counter
z = sin(F)*counter,

assuming z goes "up and down" and x goes "across".

you''ll probably want to use floats, not ints.

finally, just incrementing a counter is a really bad idea. that will lock the speed of the bullet into the frame rate. if you get 10 fps, then the bullet will only move 10 "units" after 1 second, but if you get 30 fps, it will move 30 "units" after 1 second. you really want to use the current time, or an incremental time delta, instead of a plus-plus counter.

#### Share this post

##### Share on other sites
Thanks for your reply. yes, I actually was planning on using floats, I just hacked out a data structure as fast as possible. Thanks for your response, I will try that.

JoeMont001@aol.com

#### Share this post

##### Share on other sites
Julio,

Don''t forget to only use that method for rockets, or relatively slow moving projectiles.

For bullets, strike a ray from the player to see what it hits as soon as the trigger is pulled. That way you get the effect of a very fast moving bullet.

Matt

Check out my project at:www.btinternet.com/~Matthew.Bennett

#### Share this post

##### Share on other sites
I just got a burst of inspiration (or something). Since I'm using OpenGL. Couldn't I calculate the bullet x and z using glRotatef()? Like this:
  bullet1.counter+=whatever;glRotatef(Angle,0.0f,1.0f,0.0f); //around the y axis

Then draw it. Of course I would have to use glPushMatrix() and glPopMatrix() to keep everything else the same. Would this work? I think this would be easier on the proccessor than two cos calls every loop.
Thanks,
Joe

JoeMont001@aol.com

Edited by - Julio on June 16, 2000 2:46:42 PM

#### Share this post

##### Share on other sites
I don''t recommend using the gl matrices for persistent information. Or any matrix, for that matter. As you use the matrix more and more, it slowly goes out of alignment (rounding error and such), unless you run it through a correction algo every few frames. Plus, push and popmatrix aren''t all that efficient; they can end up stalling the pipeline.

#### Share this post

##### Share on other sites
ok, I tried both and they both work fine. thanks for the info Morbo. (Hey that ryhmes)

JoeMont001@aol.com

#### Share this post

##### Share on other sites
The bullet should have its own angle, independent of the player''s. Otherwise, if you use the player''s current angle every time you''re calculating the new bullet position, your bullet will be rotated around the player (in mid-flight) as the player turns.

#### Share this post

##### Share on other sites
yes, I have that. It''s float bullet_angle;

JoeMont001@aol.com

• ### Forum Statistics

• Total Topics
627701
• Total Posts
2978698

• 21
• 14
• 12
• 10
• 12