# www.jpct.net

## jPCT-AE - a 3d engine for Android => Support => Topic started by: ndr123 on November 08, 2011, 06:04:57 pm

Title: Collision detection problem
Post by: ndr123 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).
Title: Re: Collision detection problem
Post by: EgonOlsen 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.
Title: Re: Collision detection problem
Post by: ndr123 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() ).
Title: Re: Collision detection problem
Post by: EgonOlsen 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.
Title: Re: Collision detection problem
Post by: ndr123 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.
Title: Re: Collision detection problem
Post by: EgonOlsen 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... ???
Title: Re: Collision detection problem
Post by: EgonOlsen 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...
Title: Re: Collision detection problem
Post by: ndr123 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.
Title: Re: Collision detection problem
Post by: EgonOlsen 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?
Title: Re: Collision detection problem
Post by: ndr123 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.
Title: Re: Collision detection problem
Post by: EgonOlsen 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.
Title: Re: Collision detection problem
Post by: ndr123 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.
Title: Re: Collision detection problem
Post by: EgonOlsen on November 10, 2011, 10:32:07 pm
Have you tried to adjust Config.collideOffset?
Title: Re: Collision detection problem
Post by: ndr123 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)
Title: Re: Collision detection problem
Post by: EgonOlsen 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.
Title: Re: Collision detection problem
Post by: ndr123 on November 19, 2011, 12:01:33 pm
While doing other test with my application, the checkforcollisoinspherical suddenly started working. I don't know what can be the matter cause I didn't change anything in the way I check for collisions nor in the way collisions flags on the objects are set. Anyway, as the checkforcollisionspherical needs a much higher value of collideoffset respect to the checkforcollisionellipsoid (with checkforcollisionspherical I need a collideoffset of 100 while with checkforcollisionellipsoid a value of 5 is enought to make everything work fine), using the latter results in higher framerate so I'm using the second. Thanks for the help :)
Title: Re: Collision detection problem
Post by: K24A3 on November 02, 2012, 12:27:49 pm
 no worries I got collision working with the ellipse method rather than spherical...

But the returned SimpleVector is not returning the corrected point in space. The object is getting stuck below the floor for some reason. I'll keep on persisting.
Title: Re: Collision detection problem
Post by: EgonOlsen on November 02, 2012, 01:59:32 pm
I'm not sure how your post relates to the thread above...but anyway. The returned SimpleVector is the corrected translation vector, not the endpoint of the translation.
Title: Re: Collision detection problem
Post by: K24A3 on November 02, 2012, 02:42:04 pm
Ok so the returned SV is the position in world space placing the object just before the point of collision. But for some reason it's placing the object ahead of the collision point within the collided object. Maybe it's my code.. I'll double check it.

ps. I was trying to get checkforcollisoinspherical working but had no luck hence why I posted here. Only 1 or 2 out of 50 sphere's were detecting collision despite various collideOffset values.
Title: Re: Collision detection problem
Post by: EgonOlsen on November 02, 2012, 02:49:38 pm
No. It's a TRANSLATION vector, not a POSITION vector. You have to add it to the current position to get the final position. Just as you would with a normal, not corrected translation vector.
Title: Re: Collision detection problem
Post by: K24A3 on November 02, 2012, 02:51:45 pm
Oh ok I'll adjust my code, thanks.

..update: works fine now.
Title: Re: Collision detection problem
Post by: K24A3 on November 02, 2012, 03:01:18 pm
New issue, the objects seem to center to 0,0,0 when I add the returned SV.

float fTest = 25f;
SimpleVector vcoll = null;
//vcoll = obj3D.checkForCollisionSpherical(svPos, 5f);
vcoll = obj3D.checkForCollisionEllipsoid(svPos, new SimpleVector(fTest,fTest,fTest), 1);

// Was there a collision?
if(vcoll.equals(svPos)==false)
{
svPos.add(vcoll);
Global.Debug("COLLISION");
svMomentum.y = -50;
//svMomentum.y = -svMomentum.y;
}
else
{
svPos.set(vcoll);
}

The objects are simply moving vertically to a floor with no X and Z movement.. trying to figure out why the X and Z translation is centering to 0,0,0
Title: Re: Collision detection problem
Post by: K24A3 on November 02, 2012, 03:24:36 pm
The X and Z values returned have significant values from -9 to 9 which is odd since the object is not being moved at all in the x and Z axis. I assume the ellipsoid calculations are adding X and Z movement since ellipsoids are rounded despite the difference between the current translation and the new position being purely vertical on the Y axis?

If that's how it works then that's fine I can adjust the code to ignore X and Z values if need be.
Title: Re: Collision detection problem
Post by: EgonOlsen on November 02, 2012, 05:39:14 pm
What?... ??? Sorry, but i don't get it, especially the part with the "centering"... If you use ellipsoid collision detection, you should make sure that your ellipsoid doesn't intersect with any geometry before calling one of these methods. If it does, it might move you out of the collision if you are lucky or you'll stuck (more likely).
Title: Re: Collision detection problem
Post by: K24A3 on November 09, 2012, 02:34:40 am
Not sure why all the objects are moving to 0,0,0 as a result of the collision, I'll use cubes instead and add some more debugging output to see what's going on.