convexTest with rotation

Post Reply
lsgmasa
Posts: 8
Joined: Thu Dec 06, 2007 7:10 pm

convexTest with rotation

Post by lsgmasa »

Hello

I am developing a game which has a user level creation, and I started using convexTest(...) function to find out the intersection point between placeable objects and static background.
It worked good at the beginning, but I realized I have to rotate the object. Is there any way to use btCollisionWorld::convexTest(...) with rotated shapes?

Thanks
lsgmasa
Posts: 8
Joined: Thu Dec 06, 2007 7:10 pm

Re: convexTest with rotation

Post by lsgmasa »

Hello again. I looked at convexTest(...) function and created my version of it

it takes extra parameter rotation, and the rotation value is used to calculate convexFromTrans, convexToTrans, and value "I".
I tested this function by casting box shape with rotation value, and it seems working good.
However, I wonder if what I am doing is correct thing, and if you guys give me any opinion, that would be great.

Thanks in advance.

Code: Select all

void convexTestWithRotation( 
                            const btConvexShape* castShape, 
                            const btVector3& convexFromWorld, 
                            const btVector3& convexToWorld, 
                            const btQuaternion& rotation, //added 
                            btCollisionWorld::ConvexResultCallback& resultCallback,
                            short int collisionFilterMask )
{
    btTransform	convexFromTrans,convexToTrans;
    convexFromTrans.setIdentity();
    convexFromTrans.setOrigin(convexFromWorld); //<-added
    convexFromTrans.setRotation( rotation );
    convexToTrans.setIdentity();
    convexToTrans.setOrigin(convexToWorld);
    convexToTrans.setRotation( rotation ); //<-added
    btVector3 castShapeAabbMin, castShapeAabbMax;
    btTransform I;
    I.setIdentity();
    I.setRotation( rotation );//<-added
    castShape->getAabb (I, castShapeAabbMin, castShapeAabbMax);

    btCollisionObjectArray& collisionObjects = g_bullet.m_pWorld->getCollisionObjectArray();
    /// go over all objects, and if the ray intersects their aabb + cast shape aabb,
    // do a ray-shape query using convexCaster (CCD)
    int i;

    for (i=0;i<collisionObjects.size();i++)
    {
        btCollisionObject*	collisionObject= collisionObjects[i];
        //only perform raycast if filterMask matches
        if(collisionObject->getBroadphaseHandle()->m_collisionFilterGroup & collisionFilterMask) { 
            //RigidcollisionObject* collisionObject = ctrl->GetRigidcollisionObject();
            btVector3 collisionObjectAabbMin,collisionObjectAabbMax;
            collisionObject->getCollisionShape()->getAabb(collisionObject->getWorldTransform(),collisionObjectAabbMin,collisionObjectAabbMax);
            AabbExpand (collisionObjectAabbMin, collisionObjectAabbMax, castShapeAabbMin, castShapeAabbMax);
            btScalar hitLambda = btScalar(1.); //could use resultCallback.m_closestHitFraction, but needs testing
            btVector3 hitNormal;
            if (btRayAabb(convexFromWorld,convexToWorld,collisionObjectAabbMin,collisionObjectAabbMax,hitLambda,hitNormal))
            {
                g_bullet.m_pWorld->objectQuerySingle(castShape, convexFromTrans,convexToTrans,
                    collisionObject,
                    collisionObject->getCollisionShape(),
                    collisionObject->getWorldTransform(),
                    resultCallback);
            }	
        }
    }

}
reltham
Posts: 66
Joined: Fri Oct 12, 2007 6:28 pm
Location: San Diego

Re: convexTest with rotation

Post by reltham »

I would guess that you should not need to apply the rotation to both the shape and from/to transforms. Probably just one or the other would accomplish what you want.
lsgmasa
Posts: 8
Joined: Thu Dec 06, 2007 7:10 pm

Re: convexTest with rotation

Post by lsgmasa »

I removed the rotation from the shape, and now it doesn't work anymore.
it seems that I need to apply the rotation to the both

Reltham Thanks you for your feedback though
User avatar
John McCutchan
Posts: 133
Joined: Wed Jul 27, 2005 1:05 pm
Location: Berkeley, CA
Contact:

Re: convexTest with rotation

Post by John McCutchan »

I will post the fix that will be in the next version of Bullet tomorrow. I finished it earlier today but I didn't have time to post it. It's essentially the same as the one you posted but it will allow for the from and to rotations to be different.
lsgmasa
Posts: 8
Joined: Thu Dec 06, 2007 7:10 pm

Re: convexTest with rotation

Post by lsgmasa »

great thanks!
User avatar
John McCutchan
Posts: 133
Joined: Wed Jul 27, 2005 1:05 pm
Location: Berkeley, CA
Contact:

Re: convexTest with rotation

Post by John McCutchan »

The new convexSweepTest has been committed to SVN. Can you please test it to verify that it functions properly? Instead of a btVector3 from and to the function now takes a from and to transformation.

Thanks,
John
lsgmasa
Posts: 8
Joined: Thu Dec 06, 2007 7:10 pm

Re: convexTest with rotation

Post by lsgmasa »

OK I finally got time to test the new code, but it didn't work as it is in the SVN depot.

the things I did:
1. get the latest btCollisinWorld.{h,cpp}
2. build bullet 2..66 using these files.
3. run my game.

the result returned by convexSweepTest(...) was wrong.

I compared the new function with my version of function, and I realized the btTransform passed into calculateTemporalAabb(...) has translation since the new function uses "convexFromWorld" directly.

I changed the btCollisionWorld::convexSweepTest(...) as the following and it seems working again.

Code: Select all

void	btCollisionWorld::convexSweepTest(const btConvexShape* castShape, const btTransform& convexFromWorld, const btTransform& convexToWorld, ConvexResultCallback& resultCallback,short int collisionFilterMask)
{
	btTransform	convexFromTrans,convexToTrans;
	convexFromTrans = convexFromWorld;
	convexToTrans = convexToWorld;
	btVector3 castShapeAabbMin, castShapeAabbMax;
	/* Compute AABB that encompasses movement */
	{
		btVector3 linVel, angVel;
		btTransformUtil::calculateVelocity (convexFromTrans, convexToTrans, 1.0, linVel, angVel);
        btConvexShape* pTest = (btConvexShape*)castShape; //<-added I needed to cast the pointer from const btConvexShape* to btConvexShape*... I don't know why...

        btTransform I;//<-added
        I.setIdentity();//<-added
        I.setRotation( convexFromWorld.getRotation() );//<-added

		pTest->calculateTemporalAabb (I, linVel, angVel, 1.0, castShapeAabbMin, castShapeAabbMax);
	}

	/// go over all objects, and if the ray intersects their aabb + cast shape aabb,
	// do a ray-shape query using convexCaster (CCD)
	int i;
	for (i=0;i<m_collisionObjects.size();i++)
	{
		btCollisionObject*	collisionObject= m_collisionObjects[i];
		//only perform raycast if filterMask matches
		if(collisionObject->getBroadphaseHandle()->m_collisionFilterGroup & collisionFilterMask) { 
			//RigidcollisionObject* collisionObject = ctrl->GetRigidcollisionObject();
			btVector3 collisionObjectAabbMin,collisionObjectAabbMax;
			collisionObject->getCollisionShape()->getAabb(collisionObject->getWorldTransform(),collisionObjectAabbMin,collisionObjectAabbMax);
			AabbExpand (collisionObjectAabbMin, collisionObjectAabbMax, castShapeAabbMin, castShapeAabbMax);
			btScalar hitLambda = btScalar(1.); //could use resultCallback.m_closestHitFraction, but needs testing
			btVector3 hitNormal;
			if (btRayAabb(convexFromWorld.getOrigin(),convexToWorld.getOrigin(),collisionObjectAabbMin,collisionObjectAabbMax,hitLambda,hitNormal))
			{
				objectQuerySingle(castShape, convexFromTrans,convexToTrans,
					collisionObject,
						collisionObject->getCollisionShape(),
						collisionObject->getWorldTransform(),
						resultCallback);
			}	
		}
	}

}
reltham
Posts: 66
Joined: Fri Oct 12, 2007 6:28 pm
Location: San Diego

Re: convexTest with rotation

Post by reltham »

I think the reason you had to cast away the const was that you didn't get the new version of btCollisionShape.cpp/h.
lsgmasa
Posts: 8
Joined: Thu Dec 06, 2007 7:10 pm

Re: convexTest with rotation

Post by lsgmasa »

Thanks reltham :)
yes, that must be it.
User avatar
John McCutchan
Posts: 133
Joined: Wed Jul 27, 2005 1:05 pm
Location: Berkeley, CA
Contact:

Re: convexTest with rotation

Post by John McCutchan »

You're right there is a bug in the SVN version. A proper fix has been developed and will be committed soon. In the meantime you can continue to use the version you have developed.

Thanks for your feedback,
John
User avatar
John McCutchan
Posts: 133
Joined: Wed Jul 27, 2005 1:05 pm
Location: Berkeley, CA
Contact:

Re: convexTest with rotation

Post by John McCutchan »

Hey,

A fix based on your code was committed this morning. Please let us know if your issue persists.

Thanks,
John
Post Reply