< Back to OSY 1.0 thread list

OSY 1.0 Thread Viewer

Thread #: 1614

collision detection, balls and boxes...

OscarWilde

Fri Mar 22 05:03:08 2002

i'm going to start a new project just to play around with collision detection.

the idea is balls that randomly start bouncing inside a box and of each other. This way once I get some decent collision functions going i can implement it in my game which i've been taking my sweet time to finish. Well the game it self is just to play around with ideas and concepts till i actually decide on what i really want to develop.

My current game is just a sprite based snake game like you have on nokia phones. I've pretty much got everything to work but i need to add:
1) random function to create a 'meal' in a new position after the last one has been eaten and the snake grows one unit. this should be easy although i have to add a function that allows for the meal to be put in an empty space within the borders and not where the snake body is.
2) use some buffer key so that the snake moves constantly instead of me having to keep tapping the key done. THis is easy.
3) I need to add a timer function to make things easier to handle. RIght now its event based. :tongue: this should be easy too.

THe cool thing about OpenGL and my code is that i can interchange the modelling. For example before I used quad sprites. THen i decided to use shperes instead, so now my snake looks like a string of beads. And for the fun of it i added a teapot to represent the meal. :biggrin:

BUT! the current collision detection is brute force where every frame i compare the vertices of the head to the meal position (grow snake, move food), plus after that i compare the vertices of the head to every 'part' of the snake body (kill snake).

So to my demo project of balls and a box.

My idea is such:
calculate the distance between the center of two spheres, and then add then get the sum of the radius of both spheres. If the radius is less then the distance between the two spheres they aren't touching yet. Now to get away from the brute force of calculating the differences at every frame/instance, what i'm going to do is:
2) once i the condition of no collision is satisfied i take the distance between the two spheres and calculate the time before the potential of the two spheres hitting. Then i tell the computer to calculate colision after the time has passed. Makes sense so far.

BUT!!!!

If i have many spheres and they are all bouncing in different directions then it would be silly to calculate all the differeces. So I'm going to sort them out by vectors, i.e. what direction they are heading. RIght now speed is constant so no balls will pass another one. Makes life easier for me. ;) Once i've sorted the balls out:
3) if vectors are equal then don't calculate possible time of collision.
Also if vectors are opposite, i.e. they add up, then ignore too. For example ball a is moving to the left and ball b is moving AWAY to the right on the same plane. THey obviously are not going to hit at any time till their vectors are negative opposites, such as when they are moving towards each other.

Now add too all that, i'm going to need to test collision agains the borders of the box. :eek: THat should be fairly simple too as i'll be using a similar concept of time and distance. Don't test till the calculated time of when the ball should hit the wall of the box.

So far does it sound good?

any flaws in my logic?

more importantly, i forget how to calculate a distance between two points on a graph.
Is it (x1-x2)/(y1-y2) = distance between points?
Now since its 3d space i'm going to have to first have to adjust the z values as the plane of the axis. Like a typical x,y graph is sitting where z = 0
but if vertices have x,y,z then i have to rotate the x,y axis to sit upon where the graph is now z1,x1->z2,x2

make sense so far?

i think my head is hurting...

:cheesy:

OscarWilde

Fri Mar 22 06:19:17 2002

3D geometry sucks ass. Looks like it won't be as easy to calculate the distance between points in 3d space without me putting some thought into the formulas.

DId i menton i suck at math!

anyway, i also realised i can use the SIMD units to do the sorting of the vectors.

Cool! I'm gonna use vector units!!!

i'm so cool no?

yay!!!

AllYorBaseRBelong2Us

Fri Mar 22 07:19:52 2002

i'm so cool no?

Cold as poop.

OscarWilde

Fri Mar 22 07:27:41 2002

:tongue:
:cheesy:

anyway i've been doing some research:

(x1,y1,z1), (x2,y2,z2)

xd=x2-x1
yd=y2-y1
zd=z2-z1

distance = sqrrt(xd^2+yd^2+zd^2)

since the square roots are expensive to do on computers there was a suggestion to replace:

if squareRoot(xd*xd+yd*yd) < diameter
to
if (xd*xd+yd*yd) < diameter*diameter

Makes sense...

OscarWilde

Fri Mar 22 07:37:04 2002

now could you be so kind as to explain some of the code below a little.

[code]

BOOL bShpereTest(Object3D* obj1, Object3D* obj2)
{
    D3DVECTOR relPos = obj1->prPosition - obj2->prPosition;
    float dist = relPos.x*relPos.x + relPos.y*relPos.y+relPos.z*relPos.z;
    float mindist = obj1->fRadius + obj2->fRadius;
    return dist <= mindist + mindist;
}
[/code]

Now i get the bool function is testing the sum of the distance between the two spheres and if its is less then or equal to the sum of 2xmindist. Which is what happens with 'return dist....'

What I don't get is the '->' part.
in the begining we have 'Object3D* obj1' which if i know my C well means that we are creating a pointer.
when we do 'obj1->prPosition' are we storing the value in the address of prPosition? I thought '->' is a operator to pointer address... me needs to look through my C++ book again. I suck at programming. :(

:confused:

oh yeah, the code above I got from another site. I'm now looking through the vector part of it for testing with the time function.

PaulHill

Fri Mar 22 08:30:12 2002

It derefences the pointer for the structure, allowing you to explicity manipulate the field. Kinda like

foo.*bar

if you will.

sphere collision detection in 3d is easy.  Just check the dot product against the diameter.

Damn, I really need to start working on my game again.  Cheers!

AllYorBaseRBelong2Us

Fri Mar 22 08:38:54 2002

What I don't get is the '->' part.
in the begining we have 'Object3D* obj1' which if i know my C well means that we are creating a pointer.
when we do 'obj1->prPosition' are we storing the value in the address of prPosition? I thought '->' is a operator to pointer address... me needs to look through my C++ book again.

'->' is the same as '.*'

What you're doing is getting the value that obj1.prPosition is holding.  you must do this as obj1 is itself a pointer.

And my back hurts :(

OscarWilde

Fri Mar 22 08:42:54 2002

ahhh!!! I get it now, thanks for pointing out the derefences.

Makes sense.

I was at a loss to where the values for the objects were coming from, i forgot I was looking at a function and that the values were being passed to the function.

Now here is the question, how do i pass the values to the function?

I have:
BOOL bSphereTest(Object3D* obj1, Object3D* obj2)
{
//do collision test
}

int DrawSphere();
{
//draw sphere
bSphereTest(?,?);// what goes in here then?
}

Ho ho ho!!!!

you see i actually learn by trial and error. I can't learn from books or teachers. So I end up hacking code together all day till i finally get how some aspect works.

cheers (or thank you)
;)

by the by, isn't cheers in the UK 'slang' for thankyou? all my brit friends cheer each other when they want to say thankyou.

edit:

What you're doing is getting the value that obj1.prPosition is holding.  you must do this as obj1 is itself a pointer.

sorry AYB, i was typing my post while you posted your reply up. My question is now, how do i pass the information.

I hope you back gets better. I did warn you that if you get to carried away with making love to katie holmes your back would eventually fail.

did ya listen?

noooooo!!!!

(Edited by OscarWilde at 3:46 pm on Mar. 22, 2002)

AllYorBaseRBelong2Us

Fri Mar 22 09:03:01 2002

sorry AYB, i was typing my post while you posted your reply up. My question is now, how do i pass the information.

It depends what the data type of prPosition is.  if it is a float, then

float poop [color=green]/*poop does indeed sometimes float */[/color] = obj1->prPosition;

I hope you back gets better. I did warn you that if you get to carried away with making love to katie holmes your back would eventually fail.

did ya listen?

noooooo!!!!

hehe, AYB wishes :)

OscarWilde

Fri Mar 22 09:44:42 2002

okay, since we are testing for two objects we'd have something like this:

struct Obect
{
float prPosition;
float fRadius;
}
typedef struct Object Object3D;

BOOL TestSphere(Object3D* obj1, Object3D* obj2)
{
//do collisiontest
}

int DrawSphere()
{

float Object3D.fRadius[0] = firstradius;
float Object3D.fRadius[1] = secondradius;

glSolidSphere( firstradius, 10); //10 is the number of slices, if i remember my OpenGL functions correctly
glSolidSphere( secondradius, 20); //assuming that the second radius is bigger

TestSphere(?,?); //how do i pass the information?
}

obviously i don't understand pointers at all hence why i tend to avoid them. So how do i draw the different objects without having to: "float Obect3D.fRadius[0]=firstradius;"

OscarWilde

Fri Mar 22 09:46:00 2002

i'm assuming that fRadius is a float and prPosition is a pointer since its standard to put in front of the variable the type it is. like iRadius would be integer, fRadius would be float and so on.
Robocop Q Einstein

Fri Mar 22 16:47:32 2002

I like it when collisions happen between my balls and her chin.
AllYorBaseRBelong2Us

Fri Mar 22 16:55:31 2002

[code]
float Object3D.fRadius[0] = firstradius;
float Object3D.fRadius[1] = secondradius;
[/code]

Being that float Object3D.fRadius doesn't exist, you are likely to get a big "What Yuo Say?" from your compiler

Harbinger

Fri Mar 22 16:57:47 2002

<Reminisce>
Back in the early days of Computer Science (when I attended university), we had a single graphics class, and its only prerequisite was that you took the class for C (pre-C++ ;)).  

Oh, what a grand time we had programming graphics on 12" monochrome green terminals!

"Evans and Sutherland" was mentioned quite a lot.  Like I said, this was quite some time ago. ;)

I kinda envy you guys studying this stuff today, what with all the advanced technology you get to play with (compared to what I had).  [The most advanced personal computers in my dorm were my Amiga 1000 and another guy's Tanfy 1000.]  But I temper that with the fact that I don't care to program any more, so it all kind of balances out.
</Reminisce>

DrPizza

Fri Mar 22 18:21:21 2002

'->' is the same as '.*'

No it isn't.

.* is "use this member-function pointer on this object".  It takes an object on the left-hand side and a member function pointer on the right-hand side.

-> is "access this member of this pointed-to object".  It takes a pointer to an object on the left-hand side, and nothing on the right hand side (you need to write something on the right-hand side, but -> doesn't itself use it).

pObject->member is equivalent to (*pObject).member, except when -> or * are overloaded.