Skip to main content
gamedev.net
🔒 Locked

Help! With Positions

Started by flashlaser Oct 11, 2001 at 2:36 AM 2 replies 300+ views
Original Post
flashlaser
flashlaser
My GTA2 style game was coming along nicely until I got to collision detection. For collision detection I put my car in a bounding box and checked every vertice in the box against my city mesh and if no collision are detected then the car is moved to the destination. Now what I have relized is that my cars position is defined from its center so I would have to add the the box vertice I want to check with my cars position to get a accurate result. Here''s a picture - v1|-0--0-|v4 | p | * view from top, v1-v4 are the boxes v3|-0--0-|v5 vetices and p is the car''s position. so to check v1 for example I would have to minus v1 from p,(both are vectors) then check if it collides with the mesh. What I would like to know is if this is necessary, and if so how sould I go about doing it, since if v1 was smaller then pos I would have to minus v2 from pos, but if v1 was bigger than pos then I would have to minus pos from v1. I have tried every thing I could think of but my car still stops after the building is halfway though it. I am using DirectX8 with VisualC++ 6.0. I load the map and car from X. files into seperate vertice buffers. I lock the vertice buffers and index buffers when I need access to the vertices.
WarMachine
WarMachine
You can use a bounding box technique, but that is usually used for 2D games. I''m not sure if I understand if your game is 2D or 3D, if 2D, bounding box is ok, although what I would do is store the center of the object and the height and width of the bounding box, instead of its vertices. This way, you update only the center vertex.
To get the points of the bounding box, for example the top left vertex, use x=center.x-width/2 and y=center.y-length/2.
Remember to divide by 2, since the bounding box is symmetrical.

If on the other hand your game is 3D, I advise you to use a bounding sphere.
"You can get out of prison, but not out of the cemetery"
flashlaser
flashlaser
I''ll try to explain more about what I''m trying to do.

I have a car mesh, I worked out the bounding box for it, So now I have eight points. My collision detection method works by checking each the eight points of the box against my city mesh. If none of the points collide then the car is moved to its destination. My problem is this, I input the 8 points into the collision detection function with the cars destination one at a time. When no collision occurs the car is moved to the destination. The destination is worked out using the cars position (p) and speed. Since p is the center of the car the car goes a little into the building. What I want to do is somehow add the points to p so this will stop.


|--------------------|
| building |
| +-------+ |
| | | |
-----|-------|--------
| p |
| |
| car |
+-------+


I have tried some code already but it does''t work. Here it is so you can see.

D3DXVECTOR3 Car::point(D3DXVECTOR3 point) {
D3DXVECTOR3 posold;
if (point.x <= 0) {
if (pos.x < 0) {
if (point.x < pos.x) {
posold.x = point.x + pos.x;
}
else {
posold.x = point.x - pos.x;
}
}
else {
posold.x = point.x-pos.x;
}
}
else {
if (pos.x >=0) {
if (point.x>pos.x) {
posold.x = point.x + pos.x;
}
else {
posold.x = point.x - pos.x;
}
}
else {
posold.x = point.x-pos.x;
}
}

if (point.y < 0) {
if (pos.y < 0) {
if (point.y < pos.y) {
posold.y = point.y + pos.y;
}
else {
posold.y = point.y - pos.y;
}
}
else {
posold.y = point.y-pos.y;
}
}
else {
if (pos.y>0) {
if (point.y>pos.y) {
posold.y = point.y + pos.y;
}
else {
posold.y = point.y - pos.y;
}
}
else {
posold.y = point.y-pos.y;
}
}

if (point.z < 0) {
if (pos.z < 0) {
if (point.z < pos.z) {
posold.z = point.z + pos.z;
}
else {
posold.z = point.z - pos.z;
}
}
else {
posold.z = point.z-pos.z;
}
}
else {
if (pos.z>0) {
if (point.z>pos.z) {
posold.z = point.z + pos.z;
}
else {
posold.z = point.z - pos.z;
}
}
else {
posold.z = point.z-pos.z;
}
}
return posold;
}

WarMachine
WarMachine
Your code doesn''t make much sense, but oh well.
If you are doing your game like GTA, you only need to use 4 points, not 8, since chaces are you don''t really need the bottom points (car is always on the ground sort of thing).
Try moving the car first, then test for collision.
If collison occurs, move the car back a bit, then test form collision again. Repeat this until there is no collision. The tests for collision after the first one should be only against the object you collided with before, since there is no need to check the whole city when you know where the collision is (much faster this way).
"You can get out of prison, but not out of the cemetery"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.