Ray-Polygon Collision problem

Started by Colli, June 27, 2012, 06:21:16 PM

Previous topic - Next topic

Colli

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:
Cube.setCollisionMode(Object3D.COLLISION_CHECK_SELF );
Cuboid.setCollisionMode(Object3D.COLLISION_CHECK_OTHERS);


and this is how i call the checkForCollision method :
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 :
Cube.addCollisionListener(new CollisionListener() {
public void collision(CollisionEvent ce) {
Log.e("COLLISION"," ");
}
public boolean requiresPolygonIDs() {
return false;
}
});


PS: Sorry for my English

EgonOlsen

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.

Colli

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

EgonOlsen

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!?

Colli

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:
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?

Colli

#5
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  ;)