### Author Topic: Collision detection problem  (Read 6103 times)

#### ndr123

• byte
• Posts: 14
##### Collision detection problem
« on: November 08, 2011, 06:04:57 pm »
Hi,
I'm having a problem with collision detection. If I use checkforcollisionellipsoid, collisions are detected and everything works fine. The problem is when I try to use checkforcollisiospherical. When I do, collisions aren't detected and so nothing works anymore.
In particular, I have some spheres with radius 1 created by copying them from a sphere created through Primitives.getSphere(12, 1). I tried using checkforcollisionspherical as now I'm calling the checkforcollisionellipsoid with new SimpleVector(1.2, 1.2, 1.2) as ellipsoid and so I though I could save time using the faster checkforcollisionspherical (through what I read, I understood checkforcollisionspherical is faster than checkforcollisionellipsoid).

#### EgonOlsen

• Posts: 12244
##### Re: Collision detection problem
« Reply #1 on: November 08, 2011, 10:36:45 pm »
It is faster, but actually not that much. In your case, it's difficult to tell without knowing more about the specific scene. Spherical collision detection isn't a swept approach, i.e. it's possible to "jump over" obstacles if the translation is quite long compared to the collision objects. If , for example, you translate by 5,0,0 and at 2.5,0,0 there's a sphere of radius 1, this won't be detected. Might that cause the problem? Anyway, if it isn't a huge performance problem, i would stay with what works for you. Ellipsoid collision detection is simply more powerful, sliding is better etc. So prefer it if possible even if it's a tad slower.

#### ndr123

• byte
• Posts: 14
##### Re: Collision detection problem
« Reply #2 on: November 09, 2011, 01:05:33 am »
No, it is not the problem as the maximum translation is (1,1,1) but usually it is enough smaller (I create the components of the translation vector through Math.random() ).

#### EgonOlsen

• Posts: 12244
##### Re: Collision detection problem
« Reply #3 on: November 09, 2011, 12:05:49 pm »
In my test case, that collision method works just fine. If it doesn't work for you and you really have/want to use it, i need some test case to verify this problem.

#### ndr123

• byte
• Posts: 14
##### Re: Collision detection problem
« Reply #4 on: November 09, 2011, 08:54:53 pm »
Maybe it is something related to the fact that I create the spheres through copyObject() from an initial one cause I remember I tried to use the spherical collision when I used to create all the spheres through Primitives.getSphere and it worked at that time.

#### EgonOlsen

• Posts: 12244
##### Re: Collision detection problem
« Reply #5 on: November 09, 2011, 09:08:37 pm »
As long as you set the collision mode correctly on the copy, i don't see how this should affect collision detection...

#### EgonOlsen

• Posts: 12244
##### Re: Collision detection problem
« Reply #6 on: November 09, 2011, 09:34:42 pm »
I've just checked my test case again. It also uses cloned sphere objects and it runs just fine with spherical collision detection...

#### ndr123

• byte
• Posts: 14
##### Re: Collision detection problem
« Reply #7 on: November 10, 2011, 01:18:26 am »
I set collision mode correctly as the only thing I change while changing to ellipsoid to spherical is the checkforcollision call. And that's why I can't understand which the problem is.
This is how I create the spheres
Code: [Select]
`for(int i = 0; i < NR_SPHERES_LV5; i++){ sphere_velocity[i] = new SimpleVector(Math.random()*MAX_VELOCITY, Math.random()*MAX_VELOCITY,Math.random()*MAX_VELOCITY); sphere_initial_position[i] = new SimpleVector((Math.random()*(CARTESIAN_WIDTH-3))-((CARTESIAN_WIDTH-3)/2), (Math.random()*(CARTESIAN_HEIGHT-3))-((CARTESIAN_HEIGHT-3)/2), (Math.random()*(CARTESIAN_DEPTH-3))+1.5); sphere_exploded[i] = false; sphere_explosion_time[i] = 0; sphere_explosion_scale[i] = 1; chain_explosion_nr[i] = -1; spheres[i] = base_sphere.cloneObject(); spheres[i].setTexture(sphere_textures[color_index]); spheres[i].setCollisionMode(Object3D.COLLISION_CHECK_SELF); color_index++; if(color_index == sphere_textures.length) color_index = 0;}`and this is how now I check for collisions:
Code: [Select]
`v = spheres[i].checkForCollisionEllipsoid(sphere_velocity[i], ellipsoid, 3);`sphere_velocity is a vector containing the translation vector of each sphere and ellipsoid = new SimpleVector(1.2, 1.2, 1.2).
When I tried using the checkforcollisionspherical I called it with various radius but with no one I got collision.
« Last Edit: November 10, 2011, 01:21:25 am by ndr123 »

#### EgonOlsen

• Posts: 12244
##### Re: Collision detection problem
« Reply #8 on: November 10, 2011, 08:57:41 am »
That's the call for ellipsoid collision detection. Can you post the one for spherical in addition?

#### ndr123

• byte
• Posts: 14
##### Re: Collision detection problem
« Reply #9 on: November 10, 2011, 03:14:45 pm »
Code: [Select]
`v = spheres[i].checkForCollisionSpherical(sphere_velocity[i], 1);`I tried also with other radius (e.g. 5, 10) but collisions weren't detected too.

#### EgonOlsen

• Posts: 12244
##### Re: Collision detection problem
« Reply #10 on: November 10, 2011, 06:37:03 pm »
No idea then...what puzzles me, is the collision mode that you set for the spheres. It seems wrong to me and i would expect it to be

Code: [Select]
`spheres[i].setCollisionMode(Object3D.COLLISION_CHECK_SELF || Object3D.COLLISION_CHECK_OTHERS);`
if each sphere should be able to collide with all the others. However, this should prevent ellipsoid collision detection from working too. Maybe you change that part and try again? If it works then, something is wrong with these modes when using ellipsoid.

#### ndr123

• byte
• Posts: 14
##### Re: Collision detection problem
« Reply #11 on: November 10, 2011, 07:58:41 pm »
No the collision mode is correct as spheres should not collide with all the others. At this first stage, they should collide only with walls to remain inside the screen but they don't and go out of the screen.
To make more clear, I'm deleveloping a game in which you have spheres running around in a 3D room; they not collide with each other but just with walls. Then by touching the screen, you shoot a sphere and by touching again you make it explode. After the explosion, if a sphere collides with the exploded sphere, it explodes too (when a sphere explodes, it stops moving and grows a little and I change its collision mode from check self to check others) and the goal is to make explode as much spheres as possible.

#### EgonOlsen

• Posts: 12244
##### Re: Collision detection problem
« Reply #12 on: November 10, 2011, 10:32:07 pm »
Have you tried to adjust Config.collideOffset?

#### ndr123

• byte
• Posts: 14
##### Re: Collision detection problem
« Reply #13 on: November 11, 2011, 12:31:14 am »
Yes usually I have it set to 5 but I changed it to more than 100 during my tests (I don't remember the exact number)

#### EgonOlsen

• Posts: 12244
##### Re: Collision detection problem
« Reply #14 on: November 11, 2011, 06:56:21 am »
5 is a little too small IMHO...however, if 100 doesn't help either, then i'm out of ideas. I need a test case then or otherwise, i can't verify nor explain this problem.