reactphysics3d
reactphysics3d copied to clipboard
Can this library determine whether two objects in the three-dimensional space intersect?
Can this library determine whether two objects in the three-dimensional space intersect? I use this library to test to determine whether two cylinders intersect in 3D space, I used rp3d :: CollisionWorld world; I assume three cylinders: 1: the starting point is (0,0,0) the ending point is (10,0,0) the radius is 0.4; 2: the starting point is (10,0,0) and the ending point is (15,0,0 ) Radius of 0.4; 3: starting point (5,0,0) ending point (15,0,0) radius of 0.4; Test results 1 and 2 Contact, 1 and 3not Contact.
Hello. Yes with a CollisionWorld, you should be able to test if there are collisions between two shapes in 3D.
When you say "starting/ending point", what value are you exactly talking about ? Cylinder position, Cylinder height ? Can you provide the code you use to create and set the positions of your cylinders in your scene ?
Thank you for your reply, I describe the cylinder axis and radius in the program, so the starting point and the end point is actually the two ends of the axis, the following is my program `rp3d::Transform calCylinderPara(rp3d::Vector3 startPt, rp3d::Vector3 endPt, double r,double &fheight,bool bBottomCoordinates = true) { rp3d::Vector3 realstartPt; rp3d::Vector3 realendPt; rp3d::Vector3 rshift(0,0,r); rp3d::Vector3 x_axis(1, 0,0 ); rp3d::Vector3 y_axis(0, 1, 0); rp3d::Vector3 z_axis(0, 0, 1); double angle_x = 0; double angle_y = 0; double angle_z = 0;
if (bBottomCoordinates)
{
realstartPt = startPt + rshift;
realendPt = endPt + rshift;
}
else
{
realstartPt = startPt;
realendPt = endPt;
}
rp3d::Vector3 vec_pipe = realendPt - realstartPt;
double len_pipe = vec_pipe.length();
angle_x = acos(vec_pipe.dot(x_axis) / len_pipe);
angle_y = acos(vec_pipe.dot(y_axis) / len_pipe);
angle_z = acos(vec_pipe.dot(z_axis) / len_pipe);
rp3d::Quaternion initOrientation1(angle_x, angle_y, angle_z);
rp3d::Transform transform1(realstartPt, initOrientation1);
fheight = len_pipe;
return transform1;
} //Create the collision world rp3d::CollisionWorld world; // Initial position and orientation of the collision body rp3d::Vector3 pt1(0, 0, 0); rp3d::Vector3 pt2(10, 0, 0); rp3d::Vector3 pt5(5, 0, 0); rp3d::Vector3 pt6(15, 0, 0); // Create a collision body in the world rp3d::CollisionBody* CylinderBody1; rp3d::CollisionBody* CylinderBody2; double len1 = 0, len2 = 0; rp3d::Transform transform1 = calCylinderPara(pt1, pt2, 0.4,len1); rp3d::Transform transform2 = calCylinderPara(pt5, pt6, 0.35,len2); CylinderShape* mCylinderShape1 = new CylinderShape(0.4,len1); CylinderShape* mCylinderShape2 = new CylinderShape(0.4, len2); ProxyShape* mCylinderProxyShape1; ProxyShape* mCylinderProxyShape2;
CylinderBody1 = world.createCollisionBody(transform1);
CylinderBody2 = world.createCollisionBody(transform2);
mCylinderProxyShape1 = CylinderBody1->addCollisionShape(mCylinderShape1, Transform::identity());
mCylinderProxyShape2 = CylinderBody2->addCollisionShape(mCylinderShape2, Transform::identity());
mCylinderProxyShape1->setCollisionCategoryBits(CATEGORY_1);
mCylinderProxyShape2->setCollisionCategoryBits(CATEGORY_1);
WorldCollisionCallback mCollisionCallback;
mCollisionCallback.cylinderBody1 = CylinderBody1;
mCollisionCallback.cylinderBody2 = CylinderBody2;
mCollisionCallback.reset();
world.testCollision(&mCollisionCallback);
bool bCollision = mCollisionCallback.cylinderCollideWithCylinder;`
In your code, can you tell me what exactly are the values of transform1 and transform2 (both position vector and orientation quaternion) ?
