PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

shooting in 3d

Hi all, I'm working on a new game, with some 3D elements, example here

http://www.ozcreations.com.au/screenshot.jpg
(comments welcome)

Anyhows, the landscape in this pic is built on x,y co-ords that are created using info like texture, height, solid. So to reference an xyz point on the map it would be somthing like map[x][z].hieght or (y) etc...

anyhow I'm now starting to fire bullets around, and what I "think" is the best way to do this is via vectors x,y and z, these are set by the player in the bottom right of the window (red dots)... so the bullet starts with these vectors, then to calc its next position I just add the vectors etc, take into account the gravity and wind etc!

for checking that it has hit the ground or player I could do a simple check like is the height at x,z higher than the bullets y co-ord etc, but if the bullet it fired along the ground it could detect a hit a fair distance after "penetrating" land

has anyone played around in simillar situation before?

should I calculate all pixels in the bullets path? or each sub section (frame)?

I was hoping to calculate each sub movment because I'm going to have "live" wind, so it will effect the bullet in real time!

-anyhows comments, suggestions, code whatever are welcome!
p.s the sheep probably wont feature in the final ;-)
here is the current bullet code:

Code:

if (bullet.dead == 0)
{
//x,z
bullet.locx = bullet.locx - bullet.vecx / 2;
bullet.locz = bullet.locz - bullet.vecz / 2;

//height
bullet.locy = bullet.locy + bullet.vecy / 2;
//gravity
bullet.vecy --;
// term velocity
if (bullet.vecy < -9) bullet.vecy = -9;

//checks
if (bullet.locx > lvl_x-1) bullet.dead = 1;
else if (bullet.locx < 0) bullet.dead = 1;
else if (bullet.locz > lvl_z-1) bullet.dead = 1;
else if (bullet.locz < 0) bullet.dead = 1;

//exploded
if (bullet.locy < level[bullet.locx][bullet.locz].height)
{
//exp
bullet.dead = 1;
}
}
If your bullet was moving in a straight line, you would be able to calculate the point of impact as soon as the bullet was fired by looking for an intersection between the path of the bullet and the plane of each landscape tile/polygon (I assume your landscape is organised in a fixed grid-like fashion) - so you're doing one check every time the bullet passes over a grid boundary. I remember using something similar in my java maze raycaster (source available - see profile).

However, given that it's going to be affected by wind and gravity, you're not making this approach easy for yourself. Does the direction and/or speed of the bullet change during the "lifetime" of the bullet? Do you have any way to predict this change of speed or direction?

If you can predict changes in the direction of the bullet, you should be able to extend the techniques outlined in the first paragraph to include this. I can't give you any equations - I'd have to sit down with paper and thrash them out...

Sorry if this doesn't make much sense but it's just gone 9 in the morning! 8O
sorry, it's real time stuff, changing all the time... best I can do it calc a sub section of the "tragectory" path for (each frame) to check for boundry collisions...

heres my "latest code"
(improvnments welcome)
Code:

void move_bullet()
{
int xo,zo;
int dist;
if (bullet.dead == 0)
{
//x,z
bullet.locx = bullet.locx - bullet.vecx / 3;
bullet.locz = bullet.locz - bullet.vecz / 3;

//wind
bullet.vecx = bullet.vecx - game.windx / 32;
bullet.vecz = bullet.vecz - game.windz / 32;

//height
bullet.locy = bullet.locy + bullet.vecy / 2;
//gravity
bullet.vecy = bullet.vecy - 2;
// term velocity
if (bullet.vecy < -20) bullet.vecy = -20;

//checks
if (bullet.locx > lvl_x - 1)
{
bullet.locx = lvl_x - 1;
next_player();
}
else
if (bullet.locx < 0)
{
bullet.locx = 0;
next_player();
}
if (bullet.locz > lvl_z - 1)
{
bullet.locz = lvl_z - 1;
next_player();
}
else
if (bullet.locz < 0)
{
bullet.locz = 0;
next_player();
}
else
//exploded
if (bullet.locy < level[bullet.locx][bullet.locz].height)
{
//explosion

//check all players for damage
for (int loop = 0; loop < game.team_number * 2; loop++)
{
//players
xo = (bullet.locx - player[loop].locx) * (bullet.locx - player[loop].locx);
zo = (bullet.locz - player[loop].locz) * (bullet.locz - player[loop].locz);
dist = sqrt(xo + zo);

if (dist < 30)
{
player[loop].damage = player[loop].damage - ((30 - dist) / 5);
if (player[loop].damage <= 0)
{
player[loop].dead = 1;
}
}
}
// next players turn
next_player();
}
//otherwise keep traking the bullet
else move_to_current_bullet();
}
}
If the movement of the bullet between frames is small (it should be?) then "faking it" by checking each height along the line between the start and end points against the heightmap should be a good enough approximation. If you use a DDA like the Bresenham algorithm it should be pretty fast.

(I used a similar method to calculate line-of-sight between two arbitrary points on a heightmap terrain).
Interesting shot. Do I sence a little bit of Argentum in there. I mean how the maps are rendered... ;)

Good luck with the development!
I dont know, how do you do it? lol?

basically is based on a "ground" image, and a greyscale height map, makes it easy to create levels etc, and they look good, by changing the color slightly etc when it gets higher helps...
We do that in Argentum aswell, but we also do a filtered scale of the original 512x512 texture bitmap to 1024x1024. Same with the 256x256 heightmap. And we add shadows and stuff and generate stuff so we know when a unit goes behind a hill...
yes, I'm not looking forward to the "behind the hill" bit myself.... I think the easyest way may be to place the player in a temp buffer, then "re-render" background info over the top (if it is), then place the tamp buffer it on the real background image....

-nice work btw....
(Thinking out loud here 'cos I've only just got up).

Brendan:

If your map doesn't scroll:
you could calculate a static screen-sized z-buffer from the viewpoint to the terrain and use that when plotting objects.

If your map scrolls:
then you could compute a mini z-buffer for your plot area at run-time... it would be costly, but you're probably not plotting all that many objects. If you cut the screen into "sectors" you could cache the mini-z-buffers.
can you explain the concept a little more refractor?, anyone?

btw, new shots at: www.ozcreations.com.au

-B
The concept is really to just use a z-buffer. If you fill the z-buffer with the terrain as you plot it then you can plot your objects using the z-buffer.

If a full-screen z-buffer kills performance too much then you may be able to get hacky with it and create small z-buffers on the fly for objects when you need them. You could probably get away with an 8-bit z-buffer, looking at the screen-shots...
Conan · 26 Nov 2002, 13:27 UTC — More on Z-Buffers
Brendan, I lifted this from gamedev.net's articles:

A Z-buffer is a buffer filled with z co-ordinates. When rendering polygons you are required to interpolate every pixel's z value and compare it to the z-buffer. If your z is closer to the viewer than the current z value in the z-buffer, you plot your pixel and readjust the z-buffer value, otherwise the pixel is ignored.

In other words, the z-buffer has the same dimensions as your rendering surface, except instead of holding colour values, in holds z values. As you can see, the algorithm is extremely simple to explain and implement. Another advantage is that it has no ordering requirement. Polygons can be rendered in any order, unlike other systems which require them to be sorted.

The disadvantage here is that after every frame you must clear the z-buffer back to zero. You don't want to be comparing the new polygon's z values with old ones in the z-buffer from the previous frame.
----------------
note the article goes on to describe how to eliminate the clearing to zero each frame.

nb: PocketGL uses a Z-buffer the same res as the screen & manages 60FPS on an iPaq 3650