moving an rigidbody under the mouse cursor

Xemame
Posts: 8
Joined: Mon Dec 20, 2010 3:47 pm

moving an rigidbody under the mouse cursor

Post by Xemame »

Hello!

I have to move bullet objects with the cursor of my mouse.

To do so, I am thinking about creating a sphere (with btSphereShape) and set its position under my mouse. However, I suppose this is not enough: What about the speed? I should set the speed of the sphere to the speed of the mouse? And what about the torque? Finally, is it possible to do it that way :?: :?: :?:

I have seen the methods applyTorque, applyForce, etc, but I think it won't work since these are forces and I want position :?

Any ideas? any documentation?

Thanks!

PS: I am very new to Bullet (my first post!!! :D)and there must be options I am not aware of (ex: the constraints)
Covenant
Posts: 4
Joined: Wed Dec 22, 2010 2:31 am

Re: moving an rigidbody under the mouse cursor

Post by Covenant »

All of the demos have this built into them, so I imagine you could look through the core demo code to copy what they did.
Xemame
Posts: 8
Joined: Mon Dec 20, 2010 3:47 pm

Re: moving an rigidbody under the mouse cursor

Post by Xemame »

I look at the Basic Demo.

I found this in DemoApplication.cpp , line 748. It creates a constraint on the picked cube and then set the constraint position under the mouse cursor, making the cube moving.

However, I do not understand what pickedBody->setActivationState(DISABLE_DEACTIVATION); and pickedBody->forceActivationState(ACTIVE_TAG); are for. I did not find documentation about the ActivationState :(
Somebody has explainations?

Code: Select all

	{
			if (state==0)
			{


				//add a point to point constraint for picking
				if (m_dynamicsWorld)
				{
					
					btVector3 rayFrom;
					if (m_ortho)
					{
						rayFrom = rayTo;
						rayFrom.setZ(-100.f);
					} else
					{
						rayFrom = m_cameraPosition;
					}
					
					btCollisionWorld::ClosestRayResultCallback rayCallback(rayFrom,rayTo);
					m_dynamicsWorld->rayTest(rayFrom,rayTo,rayCallback);
					if (rayCallback.hasHit())
					{


						btRigidBody* body = btRigidBody::upcast(rayCallback.m_collisionObject);
						if (body)
						{
							//other exclusions?
							if (!(body->isStaticObject() || body->isKinematicObject()))
							{
								pickedBody = body;
								pickedBody->setActivationState(DISABLE_DEACTIVATION);


								btVector3 pickPos = rayCallback.m_hitPointWorld;
								printf("pickPos=%f,%f,%f\n",pickPos.getX(),pickPos.getY(),pickPos.getZ());


								btVector3 localPivot = body->getCenterOfMassTransform().inverse() * pickPos;

								

								


								if (use6Dof)
								{
									btTransform tr;
									tr.setIdentity();
									tr.setOrigin(localPivot);
									btGeneric6DofConstraint* dof6 = new btGeneric6DofConstraint(*body, tr,false);
									dof6->setLinearLowerLimit(btVector3(0,0,0));
									dof6->setLinearUpperLimit(btVector3(0,0,0));
									dof6->setAngularLowerLimit(btVector3(0,0,0));
									dof6->setAngularUpperLimit(btVector3(0,0,0));

									m_dynamicsWorld->addConstraint(dof6);
									m_pickConstraint = dof6;

									dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,0);
									dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,1);
									dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,2);
									dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,3);
									dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,4);
									dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,5);

									dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,0);
									dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,1);
									dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,2);
									dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,3);
									dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,4);
									dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,5);
								} else
								{
									btPoint2PointConstraint* p2p = new btPoint2PointConstraint(*body,localPivot);
									m_dynamicsWorld->addConstraint(p2p);
									m_pickConstraint = p2p;
									p2p->m_setting.m_impulseClamp = mousePickClamping;
									//very weak constraint for picking
									p2p->m_setting.m_tau = 0.001f;
                           /*
									p2p->setParam(BT_CONSTRAINT_CFM,0.8,0);
									p2p->setParam(BT_CONSTRAINT_CFM,0.8,1);
									p2p->setParam(BT_CONSTRAINT_CFM,0.8,2);
									p2p->setParam(BT_CONSTRAINT_ERP,0.1,0);
									p2p->setParam(BT_CONSTRAINT_ERP,0.1,1);
									p2p->setParam(BT_CONSTRAINT_ERP,0.1,2);
									*/
									

								}
								use6Dof = !use6Dof;

								//save mouse position for dragging
								gOldPickingPos = rayTo;
								gHitPos = pickPos;

								gOldPickingDist  = (pickPos-rayFrom).length();
							}
						}
					}
				}

            } else
			{

				if (m_pickConstraint && m_dynamicsWorld)
				{
					m_dynamicsWorld->removeConstraint(m_pickConstraint);
					delete m_pickConstraint;
					//printf("removed constraint %i",gPickingConstraintId);
					m_pickConstraint = 0;
					pickedBody->forceActivationState(ACTIVE_TAG);
					pickedBody->setDeactivationTime( 0.f );
					pickedBody = 0;
				}


			}