cutting a sphere shape
-
btomko
- Posts: 11
- Joined: Mon Aug 31, 2009 9:05 pm
cutting a sphere shape
Is it possible to cut a sphere shape? I would like to use the top quarter or third of a sphere on top of a cylinder. Can I apply some sort of clipping so that I only use a certain portion of the sphere in the physics engine?
-
sparkprime
- Posts: 508
- Joined: Fri May 30, 2008 2:51 am
- Location: Ossining, New York
Re: cutting a sphere shape
You can create your own collision shape to use any procedural mechanism you like for choosing whether a given point intersects the shape, how deep it is inside, and in what direction it should be rejected.
I would quite like a cylinder with variable diameters at top and bottom, so that it is easy to create the kind of shapes you would make on a spinning lathe (banisters, bowling pins, etc) using a compound of such topless cones. I don't need it enough to actually implement it though
I would quite like a cylinder with variable diameters at top and bottom, so that it is easy to create the kind of shapes you would make on a spinning lathe (banisters, bowling pins, etc) using a compound of such topless cones. I don't need it enough to actually implement it though
-
Dragonlord
- Posts: 198
- Joined: Mon Sep 04, 2006 5:31 pm
- Location: Switzerland
Re: cutting a sphere shape
Such a tapered cylinder should be doable as the convex hull of two cylinders with different radius and small height. Not a perfect cylinder but it would be close to one. The same can be done for tapered capsules just using two spheres of different radius in a convex hull. Only problem is that the convex hull code is not very stable. In the case of the tapered capsule one sphere can drop through the ground while the other stays on top of it causing limbs to protrude into the ground unable to ever free. CCD should though fix this once it comes... which hopefully is not when DNF comes out 
-
btomko
- Posts: 11
- Joined: Mon Aug 31, 2009 9:05 pm
Re: cutting a sphere shape
Where can I define an algorithm to determine whether a given point intersects the shape, etc?You can create your own collision shape to use any procedural mechanism you like for choosing whether a given point intersects the shape, how deep it is inside, and in what direction it should be rejected.
Thanks
-
Flix
- Posts: 456
- Joined: Tue Dec 25, 2007 1:06 pm
Re: cutting a sphere shape
Never tried, but I think that in theory it's possible to create that shape without "cutting" a sphere.btomko wrote:I would like to use the top quarter or third of a sphere on top of a cylinder.
First of all you need "a top quarter or third of a sphere": well, if you scale a sphere shape (btMultiSphereShape with only one point and radius plus local vertical scaling) you can get a surface like the one you're looking for (except that it's mirrored vertically).
So, you can use a btCompoundShape with a cylinder + a scaled sphere (placed over it) to get the shape you're looking for almost exactly, without using convex hulls (thus preserving the full "rotational behavior" of the lateral surface).
-
Flix
- Posts: 456
- Joined: Tue Dec 25, 2007 1:06 pm
Re: cutting a sphere shape
Just to show you a raytrace image of a similiar shape made with Bullet with this approach:
You do not have the required permissions to view the files attached to this post.
-
btomko
- Posts: 11
- Joined: Mon Aug 31, 2009 9:05 pm
Re: cutting a sphere shape
Thank you all for your help so far. Flix, I am trying your technique but am having some issues. I am trying to achieve exactly what you have in the picture. I am working in openscenegraph and trying to match the squished sphere with a mesh I create in blender. However, I get some mesh penetrations on the sides of the squished sphere but not the top. It seems that bullet and blender scale spheres a little differently. My code is:
which happens to be the same as:
I will let you know when I figure this out. In the mean time any suggestions would be greatly appreciated. Thanks Again.
Code: Select all
btVector3 pos(0,0,0);
m_multiSphereShape = new btMultiSphereShape(&pos,&RADIUS, 1);
m_multiSphereShape->setLocalScaling(btVector3(1.f,0.235294118f,1.f));Code: Select all
m_capsuleShape = new btCapsuleShape(RADIUS,0); //capsule with height 0
m_capsuleShape->setLocalScaling(btVector3(1.f,0.235294118f,1.f));-
btomko
- Posts: 11
- Joined: Mon Aug 31, 2009 9:05 pm
Re: cutting a sphere shape
I think the scaling is buggy in bullet. I did some tests with the raytrace demo using a multisphere shape and scaled the Y at different percentages. The shapes that I scaled look very messed up.
You do not have the required permissions to view the files attached to this post.
-
sparkprime
- Posts: 508
- Joined: Fri May 30, 2008 2:51 am
- Location: Ossining, New York
Re: cutting a sphere shape
If that is a bug, it may just be a bug with rays, though.
-
btomko
- Posts: 11
- Joined: Mon Aug 31, 2009 9:05 pm
Re: cutting a sphere shape
I'm doing a particle simulation with spheres bouncing off this "squished sphere." The particles will penetrate/tunnel through the squished sphere depending on where they hit the squished sphere and at what angle they hit it. Right now I am contemplating a minkowski sum shape with a cylinder and a sphere, but am not sure how well that's all going to work out.
-
Flix
- Posts: 456
- Joined: Tue Dec 25, 2007 1:06 pm
Re: cutting a sphere shape
The only thing I can do is to post my own code...
This is the base-code I used for the raytrace image (probably with different dimensions).
The code seems to work, but the initial dimension (localAABB) seems wrong (i.e. the dimensions of the real shape is different).
I think it's just a question of tuning it a bit...
Hope it helps.
This is the base-code I used for the raytrace image (probably with different dimensions).
The code seems to work, but the initial dimension (localAABB) seems wrong (i.e. the dimensions of the real shape is different).
I think it's just a question of tuning it a bit...
Code: Select all
#define TEST_CYLSPHERE
#ifdef TEST_CYLSPHERE
{
// A cylinder with a flattened sphere on the top of it.
// Only good for halfHeight > radius
// TWICKABLES:
const btVector3 localAABB(4,5,4); // of the cylinder only (localAABB.x()==localAABB.z())
const btScalar verticalScalingFactor(2); // how much the top sphere is flat ( > 1, 1 = half capsule shape )
btTransform startTransform;startTransform.setIdentity();
startTransform.setOrigin(btVector3(0,localAABB.y()*2.f,0));
//startTransform.setRotation(btQuaternion(btVector3(0,0,1),SIMD_PI)); // Start with round part down
const btScalar mass(2.f);
const bool deferScalingToCompoundShape = false; // "true" should be tested as well, but I don't know if and how localScaling works with compound shapes in Bullet, and
// for sure my "graphic drawer" isn't smart enough to understand it. Set it to "false" to be sure of the result.
// END TWICKABLES
//============================================================
const btVector3 halfLocalAABB(localAABB*0.5f);
const btScalar halfHeight = halfLocalAABB.y();
const btScalar radius = halfLocalAABB.x();
btCollisionShape* myShape = NULL;
//============================================================================
btCylinderShape* cylShape = new btCylinderShape(deferScalingToCompoundShape ? btVector3(halfLocalAABB.x(),halfLocalAABB.y()*verticalScalingFactor,halfLocalAABB.z()) : halfLocalAABB);
//============================================================================
const int numSpheres = 2; // 1 or 2;
btAlignedObjectArray<btVector3> positions;positions.resize(numSpheres);
btAlignedObjectArray<btScalar> radii;radii.resize(numSpheres);
positions[0] = btVector3(0.f,halfHeight*verticalScalingFactor,0.f);
radii[0] = radius;
if (numSpheres>1) {
positions[1] = btVector3(0.f,(-halfHeight+radius)*verticalScalingFactor,0.f); // The second sphere (which is optional) is here to prevent little objects
radii[1] = radius; // from being stuck between the cylinder and the multisphere inside the compound shape
}
btMultiSphereShape* mss = new btMultiSphereShape(&positions[0],&radii[0],numSpheres);
if (!deferScalingToCompoundShape) mss->setLocalScaling(btVector3(1,1.f/verticalScalingFactor,1));
//=============================================================================
btCompoundShape* cs = new btCompoundShape();
btTransform T(btTransform::getIdentity());
cs->addChildShape(T,mss);
cs->addChildShape(T,cylShape);
if (deferScalingToCompoundShape) cs->setLocalScaling(btVector3(1,1.f/verticalScalingFactor,1));
//==============================================================================
myShape = cs;
m_collisionShapes.push_back(myShape); // Or something like that for cleaning up on exit (you may want to add all the child shapes too, if you
// don't do some kind of "shape processing and clening" on exit).
// just creating body here:
const bool isDynamic = (mass != 0.f);
btVector3 localInertia(0,0,0);
if (isDynamic) myShape->calculateLocalInertia(mass,localInertia); // Note: I could have used cylShape instead of myShape here because:
// 1) Calculating the "real" inertia for a compound shape is more complex than this (although this works well in most cases).
// 2) According to my experience the inertia calculated for btMultiSphereShape (with possible local scaling) isn't good enough.
// 3) The cylinder approximates the final shape better.
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,myShape,isDynamic ? localInertia : btVector3(0,0,0));
btRigidBody* body = new btRigidBody(rbInfo);
btWorld->addRigidBody(body);
}
#endif//TEST_CYLSPHERE-
btomko
- Posts: 11
- Joined: Mon Aug 31, 2009 9:05 pm
Re: cutting a sphere shape
Thanks for all your help. I just scaled Y by 50% instead of 25% which seems to be more visually accurate. It seems scaling gets very unstable when you squash the object too much.
-
Flix
- Posts: 456
- Joined: Tue Dec 25, 2007 1:06 pm
Re: cutting a sphere shape
Glad it worked
If you don't like the "default" Bullet raytracer, you may try the one I've just posted here: http://bulletphysics.org/Bullet/phpBB3/ ... =17&t=5613.
(It's similiar to the one I used to take the raytrace screenshot I posted above).
If you don't like the "default" Bullet raytracer, you may try the one I've just posted here: http://bulletphysics.org/Bullet/phpBB3/ ... =17&t=5613.
(It's similiar to the one I used to take the raytrace screenshot I posted above).