Advertisement

Firing a bullet

Started by March 12, 2003 10:34 PM
8 comments, last by UponTheEnd 21 years, 11 months ago
I just want to know some way that other people go about doing this.. right now, i am getting the direction of the player and making it into a velocity and just adding the velocity each time to the bullet until i get a hit in the collsion detection... i want to know what other people do to do this. or if you have any code or anything like it, it would be very much apprectiated.. thanks alot
"What we do in life, echos in eternity" -- Gladiator
I''m doing the same thing also. I don''t really see any other way to do it either.. But i''ve only been doing opengl for 3 months.. so .. My grappling hook is taking a lot of time too hehe. Its all a proccess when your new to it.. Its amazing what can be done though..
Advertisement
In terms of game development you will want to just consider that the moment the attacker fires his gun, the bullet hits the ''target''.

Think of it as a railgun. The instant a user fires the gun, the point he has the crosshair over at that exact moment will be the collision (intercept is a line hitting a plane probably).
To make the variance of distance/time that you would normally attribute to real-world physics you can add random circular variation with the target.

By this I mean, if you are using a very powerful gun and the user shoots long distance, then, by adding a +/- around the actual target, the target becomes harder to shoot.

Overall, it''s not a perfect solution, but it will reduce the complication when dealing with hundreds of shots, but if you can, this method will make calculations and multi-player code much simpler.

(PS)
In games like quake 3 with the plasma gun, a stream of individual entities are fired and created dynamically. Each plasma ''burst'' has it''s own velocity and direction which will live out it''s life until a target is hit, which is exactly the method you''re using at the moment. Obviously the method you choose will reflect the gun you want to create.
I like pie! - weebl.
Oneiric - Another graphics engine in the works.
Simply adding the vector won''t work well at all, even with some epsilon around the target.

Here''s a 1-D example of the problem:
Person A stands at the origin
Person B stand 5 feet away

Person A shoots a bullet at 2 feet/second

At time t=0: bullet is at 0
At time t=1: bullet is at 2
At time t=2: bullet is at 4
At time t=3: bullet is at 6 (missed the target of 5)

Now, assuming that your framerate is more than 1 fps, you can interpolate the bullet''s position. This gives you much greater accuracy when determining if the bullet hit the target. Let''s see this in action with fps of 2:

At time t=0: bullet is at 0
At time t=.5: bullet is at 1
At time t=1: bullet is at 2
At time t=1.5: bullet is at 3
At time t=2: bullet is at 4
At time t=2.5: bullet is at 5 (we have a hit)

Realtime collision detection is an age-old problem which still isn''t entirely perfect, even in newer games collisions are still faked by adding in the +/- epsilon factor.

---
K-1 Productions: Come visit us here.
---K-1 Productions: Come visit us here.
Anyone have any examples of firing a bullet with better collsion in a engine or game?

Thanks
"What we do in life, echos in eternity" -- Gladiator
in K-1''s example, wouldn''t it be better to test a line segment from the last position to the current position than just the point. This would solve the problem of "skipping" over some points.

[size="3"]Halfway down the trail to Hell...
Advertisement
Good, someone figured it out. What''s the difference between raytracing and collision detection? Nothing. But even with some raytracing algorithms there are "steps" that can miss an object. Some people just deal with approximating a collision instead of an absolute collision because it''s faster. And we all know that faster is better

---
K-1 Productions: Come visit us here.
---K-1 Productions: Come visit us here.
Anyone of an example of this in any form?

Phesocode, real code,

Thanks for the replys
"What we do in life, echos in eternity" -- Gladiator
A method i like is the second the built is fired you do a "rail" as mentioned above.

I just divide the target up into a bunch of circles. Then compare the position of my test as i move down the rail.

I increment in distances 1/2 the size of the targets radius. Every increment i check to see if the magnitude (the absolute value of the difference of the x, y, z components) are less that the sum of the 2 radius, if at any point it is, its a hit.

Here is a sample code from a game:

Ill explain what some of the functions are the Get_xoff() returns the offset (normal) vector component so the built flies the direction the player is facing.

the col(x,x,x,x) member function is at the very bottom. It checks as for a collision as i described above.

I would write a clean and more simple example but im short on time so ill just leave it at this.



for (i = 0; i <100; i++)
{
if ( bd1.col( player.Get_xoff()*i, player.Get_yoff()*i, player.Get_zoff()*i, 2 ) )
{
i=100;
bd1.Shit(true);
}
else
{
bd1.Shit(false);
}
}




bool ent::col(float x, float y, float z, float radi2)
{
float xcomp, ycomp, zcomp, magn;
xcomp = x - xpos;
ycomp = y - ypos;
zcomp = z - zpos;
if ( xcomp < 0 )
xcomp = -xcomp;
if ( ycomp < 0 )
ycomp = -ycomp;
if ( xcomp < 0 )
ycomp = -ycomp;
magn = sqrt(xcomp*xcomp+ycomp*ycomp+zcomp*zcomp);
if (magn < radi+radi2 )
return true;
else
return false;
}
Thanks for the replys.

The last reply was very helpful.

Thanks alot.

Does anyone have any other code that is much more simplier and just gets the players angles and turns them into the bullets velocity?

And then just moves the bullet at the players postion in the velocity that was retrieved ealier. and then just stop the bullet after a certain amount of time without a hit.

thanks alot.

i dont really need collsion detection code, but actually the bullets code..


thanks
"What we do in life, echos in eternity" -- Gladiator

This topic is closed to new replies.

Advertisement