Author Topic: Ray-Polygon Collision problem  (Read 1912 times)

Offline Colli

  • byte
  • *
  • Posts: 13
    • View Profile
Ray-Polygon Collision problem
« on: June 27, 2012, 06:21:16 pm »
Hi,

I have a problem with collision detection of two objects. I use Ray-Polygon method, problem is that when a cube is coliding with a longish cuboid, the collision event come just when i hit the long side of the cuboid, when i try to hit the cuboid from the top, the collision event come when the cube is a little bit in the cuboid, so it is not so precise. How can i make to receive collision events when the objects just touches each other? I tried to change the collideOffset, but with no result.

here is the setup for the collison mode:
Code: [Select]
Cube.setCollisionMode(Object3D.COLLISION_CHECK_SELF );
Cuboid.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);

and this is how i call the checkForCollision method :
Code: [Select]
cube.checkForCollision(movingDirection, 1)
I am also not sure what does the "step" argument in checkForCollision function mean. should that be the actual speed of the cube?

and there is the collisonListener :
Code: [Select]
Cube.addCollisionListener(new CollisionListener() {
public void collision(CollisionEvent ce) {
Log.e("COLLISION"," ");
}
public boolean requiresPolygonIDs() {
return false;
}
});

PS: Sorry for my English

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11980
    • View Profile
    • http://www.jpct.net
Re: Ray-Polygon Collision problem
« Reply #1 on: June 27, 2012, 09:16:58 pm »
checkCollision() might not be the best method for this. Try to use calcMinDistance() instead. It's actually the same thing but it returns the distance that the cube can be moved before it hits the target. "step" in checkCollision() is the length of the ray. But as said, in your case, calcMinDistance() might be the better solution.

Offline Colli

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Ray-Polygon Collision problem
« Reply #2 on: July 01, 2012, 09:49:16 am »
Thanks for the answer, now it works great. But i realized that the most suitable detection method for my game is the Sphere-Polygon method. I have already implemented this method and it works, now i just need to know the angle between the colliding objects to calculate the my physics. I tried to use the SimpleVector which gave me the checkForCollisionSpherical() function and  to calculate the angle from this vector, it gives me some angle but it is pretty inaccurate sometimes. Is there another way to calculate the angle between tho colliding objects?

Thanks for the reply

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 11980
    • View Profile
    • http://www.jpct.net
Re: Ray-Polygon Collision problem
« Reply #3 on: July 01, 2012, 09:28:28 pm »
The vector that this method returns is the corrected translation vector. It's suitable to move the objects out of the collision, it's most likely not suitable to calculate the angle you want...but then again, i'm not really sure which angle you actually want!?

Offline Colli

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Ray-Polygon Collision problem
« Reply #4 on: July 04, 2012, 11:23:56 am »
I want the angle of the colliding objects, in order to calculate the reflection vector. I figured out how to do that. Get the polygonsID which was hit, and then calculate its normal through getTransformedNormal() method, and i also have the direction vector, then i found this in this forum:
Code: [Select]
public SimpleVector getReflectionVector(SimpleVector incoming, SimpleVector normal)
  {
    SimpleVector cross = incoming.calcCross(normal);
    cross = normal.calcCross(cross);
    cross.scalarMul(2);
    cross = incoming.calcSub(cross);
    return cross;
  }

it works, but sometimes a got wrong reflection vector (it is not quite wrong because it is mostly just rotated 1.54rad to left or right side). For example i have a normal vector : X = -1 ; Y = 0 then it works ok, but when i got normal vector X = -0.03407649 Y = 7.9632E-4 which should be almost same(except the lenght) vector as the first one, a i got suddenly wrong reflection vector. Why is that?

Offline Colli

  • byte
  • *
  • Posts: 13
    • View Profile
Re: Ray-Polygon Collision problem
« Reply #5 on: July 11, 2012, 09:09:24 am »
i solved it, the problem was in my calculations, i had one vector normalized, if i removed this normalization, it works now.

I have one more question. My physics almost works perfectly, when the ball hits the wall it reflects as it should, but when the ball is rolling on the wall (so it touches the wall, and there are many collision events) than the speed of the ball is bad, it is because on each hit of the wall the speed of the ball is reduced. How should i distinguish if the ball is just rolling on the wall(so that there are many collision events) or if the ball hits the wall just one time and reflects away? I have been thinking a lot about it, but i can't find any solution how to distinguish it.

Thanks for any help.

EDIT: Problem solved, problem was in reflection vector. Thanks for your help EgonOlsen  ;)
« Last Edit: July 23, 2012, 08:34:11 am by Colli »