# Re: [Audyssey] game objects in memory

Hi Rynhardt;

ACtually collision detection really isn't that expensive. Here's some C++ for
checking two axis-aligned bounding boxes in 3D.

// set the lower and upper coordinates of each box
// and set their position

Box A (-1.0, -1.0, -1.0, 1.0, 1.0, 1.0);
A.setPosition (3.0, 3.0, 3.0);

Box B (-1.0, -1.0, -1.0, 1.0, 1.0, 1.0);
B.setPosition (0.0, 0.0, 0.0);

// check to see if any part of the above boxes touch or overlap

if((A.lower.x <= B.upper.x && A.lower.y <= B.upper.y && A.lower.z <= B.upper.z)
&& (A.upper.x >= B.lower.x && A.upper.y >= B.lower.y && A.upper.z >=
B.lower.z)) {

// Manage collision

}

In this case, the two boxes are not touching, as the bottom-most point of A is
at 2.0, 2.0, 2.0 and the top-most point of B is at 1.0, 1.0, 1.0 so there's a
length of one coordinate between them. Does this make sense?…

The above code is all you need to detect whether two boxes in 3D space touch or
overlap each other. This really isn't overly intensive. YOu can also check for
collision of spheres easily too, but it is a bit more expensive than the above,
but it is equally simple, relying on the Pythagorus theorem. Basically you can
just check the length of the line between the centers of two spheres and see if
it's less than or greater than the radii of each sphere. So if the two radii
add up to (or are greater than) the distance you just found, then the spheres
are touching or overlapping.

On the subject of arrays, (depending on the type of array) it's not necessarily
any less expensive to access an array vs doing something like the above every
frame of a game, since the system may need to move through the array to find
what it needs, regardless of how easy it looks to the user. so just because you
can call an element of an array as in array[x] doesn't mean the system doesn't
need to do any work to access that element.

There are faster ways of storing data which are easier on the system in C++ but
I'm not as familiar with them as I'd like, so forgive me but I just know
they're there, but have no explanation for you. :)

Anyway, hope this sheds some light…

Smiles,

Cara :)
On Dec 6, 2010, at 3:19 PM, Rynhardt Kruger wrote:

Hi,

Another question to the devs on this list:

What have you find to be the best way of representing the map and other game
objects in software?
Two ways I can think of is to either have everything in an big 3d array, or to
give each object attributes specifying
the 3d position.

Advantages of giving each object an x y and z attribute:
* objects can have floating point positions
* uses less memory than the array method (not really an issue anymore)
* More than one object can be in the same position (think of doors or walking
through walls in SOD)
* Possible for objects to be bigger than one unit e.a. having a start position
as well as an end position

* Detecting collisions may be expensive as all the objects need to be searched
to find an object at a specific position.
Something like binary search may be useful, but then all the objects must be
sorted every time an object moves.
* Increasing the number of objects in the game may decrease performance as the
list of objects to be searched gets
longer.

Advantages of the 3d array method:
* Very fast to reference an object at a specific position.
This makes path finding and collision detection very fast.
* One object may be placed in several array locations, e.g. having a lot of
references/pointers in the array point to
one
wall object.
* If the array size stays constant, adding more objects will not have such a
great effect on game performance.

* Not possible to have more than one object at the same position, unless each
array location points to another list.
* Not possible for an object to be bigger than one unit.
* Not possible for objects to have floating point positions.

Something I've missed?

Another way might be to combine the two approaches, e.g. having all fixed
objects like walls and floors be placed in a
3d array, and the movable objects like the player and items use the other
method.

What are your thoughts on this?

Take care,

Rynhardt

---
Gamers mailing list __ Gamers@audyssey.org
If you want to leave the list, send E-mail to gamers-unsubscr...@audyssey.org.
You can make changes or update your subscription via the web, at
http://audyssey.org/mailman/listinfo/gamers_audyssey.org.
All messages are archived and can be searched and read at
http://www.mail-archive.com/gam...@audyssey.org.
If you have any questions or concerns regarding the management of the list,
please send E-mail to gamers-ow...@audyssey.org.

---
Gamers mailing list __ Gamers@audyssey.org
If you want to leave the list, send E-mail to gamers-unsubscr...@audyssey.org.
You can make changes or update your subscription via the web, at
http://audyssey.org/mailman/listinfo/gamers_audyssey.org.
All messages are archived and can be searched and read at
http://www.mail-archive.com/gam...@audyssey.org.
If you have any questions or concerns regarding the management of the list,
please send E-mail to gamers-ow...@audyssey.org.