Ejemplo n.º 1
0
void Player::Movement( const float aDelta )
{
	const float physUnitsPerSecond = 250.f * aDelta;
	Vector2f velocity;
	
	if(KeyboardInput->KeyDown(SDL_SCANCODE_D))
	{
		velocity.x += physUnitsPerSecond;
	}
	if(KeyboardInput->KeyDown(SDL_SCANCODE_A))
	{
		velocity.x -= physUnitsPerSecond;
	}
	if(KeyboardInput->KeyDown(SDL_SCANCODE_S))
	{
		velocity.y += physUnitsPerSecond;
	}
	if(KeyboardInput->KeyDown(SDL_SCANCODE_W))
	{
		velocity.y -= physUnitsPerSecond;
	}
	myBody->SetLinearVelocity( b2Vec2( velocity.x, velocity.y ) );
	 
	const float clampRadius = 150.f; 
	const b2Vec2& physPos = myBody->GetPosition();
	Vector2f worldPos(physPos.x * PTM_RATIO, ( physPos.y * PTM_RATIO ) - 40);
	myPosition = worldPos;
}
Ejemplo n.º 2
0
vec3f MyD3DAssets::transformObjectToWorldGamecube(const BufferCPU *VSConstants, const vec3f &basePos, int blendMatrixStart) const
{
    if (VSConstants->data.size() < sizeof(VertexShaderConstantsGamecube))
    {
        return vec3f::origin;
    }

    const VertexShaderConstantsGamecube &constants = *((const VertexShaderConstantsGamecube *)VSConstants->data.data());

    vec4f rawPos(basePos, 1.0f);

    vec4f m0 = constants.posnormalmatrix[0];
    vec4f m1 = constants.posnormalmatrix[1];
    vec4f m2 = constants.posnormalmatrix[2];
    if (blendMatrixStart != -1)
    {
        m0 = constants.transformmatrices[blendMatrixStart + 0];
        m1 = constants.transformmatrices[blendMatrixStart + 1];
        m2 = constants.transformmatrices[blendMatrixStart + 2];
    }
    
    vec3f worldPos(m0 | rawPos,
                   m1 | rawPos,
                   m2 | rawPos);

    return worldPos;
}
Ejemplo n.º 3
0
Vector3 Ray::planeIntersect()
{
    float s   = -getOrigin().z / getDirection().z;
    Vector3 worldPos(0, 0, 0);
    worldPos.x = getOrigin().x + getDirection().x*s;
    worldPos.y = getOrigin().y + getDirection().y*s;
    worldPos.z = 0;

    return worldPos;
}
Ejemplo n.º 4
0
    void MapLevel::initSprites()
    {
        LOG_CALL("MapLevel::initSprites");
        for (auto& pos : mDestinationList) {
            sf::Sprite sprite(mTexture);
            sf::Vector2f worldPos(WorldOffsetX + pos.x * TileWidth, WorldOffsetY + pos.y * TileHeight);
            Entity::Ptr entity(new Flag(sprite, mBoxWorld, worldPos));
            Flag* flag = dynamic_cast<Flag*>(entity.get());
            mEntities.push_back(std::move(entity));
        }

        for (auto& pos : mSourceList) {
            sf::Sprite sprite(mTexture);
            Entity::Ptr entity(new PackageEntity(sprite, mBoxWorld));
            PackageEntity* package = dynamic_cast<PackageEntity*>(entity.get());
            package->setPosition(sf::Vector2f(WorldOffsetX + pos.x * TileWidth, WorldOffsetY + pos.y * TileHeight));
            mEntities.push_back(std::move(entity));
        }
    }
Ejemplo n.º 5
0
glm::vec3
BodyParticle::modelTransform(unsigned int a_index, const btTransform &a_transform) const{
	const btMatrix3x3& basis(a_transform.getBasis());
	const btVector3& row_x(basis[0]);
	const btVector3& row_y(basis[1]);
	const btVector3& row_z(basis[2]);
	const btVector3& origin(a_transform.getOrigin());
	glm::vec3 worldPos(0.0);
	const glm::vec3& objectPos(m_particles[a_index]);

	worldPos.x = row_x.getX() * objectPos.x + row_x.getY() * objectPos.y + row_x.getZ() * objectPos.z;
	worldPos.y = row_y.getX() * objectPos.x + row_y.getY() * objectPos.y + row_y.getZ() * objectPos.z;
	worldPos.z = row_z.getX() * objectPos.x + row_z.getY() * objectPos.y + row_z.getZ() * objectPos.z;

	worldPos.x += origin.getX();
	worldPos.y += origin.getY();
	worldPos.z += origin.getZ();

	return worldPos;
}
Ejemplo n.º 6
0
//-----------------------------------------------------------------------------------------------------------------------------------
void GameObject::CheckVisible()
{
  Vector2 worldPos(ScreenManager::GetCamera().GameToScreenCoords(GetWorldPosition()));
  SetVisible(ScreenManager::GetCamera().GetBoundingFrame().Contains(worldPos) != ContainmentType::DISJOINT);

  if (!IsVisible())
  {
    const Vector2& screenDimensions = ScreenManager::GetScreenDimensions();
    const Vector2& size = GetSize();

    if (size.x > screenDimensions.x || size.y > screenDimensions.y)
    {
      // If we have a really big object just set it's visibility to true for now - improves stability
      SetVisible(true);
      return;
    }

    float halfMaxDimensions = max(size.x, size.y) * 0.5f;

    if (worldPos.x <= 0)
    {
      worldPos.x += halfMaxDimensions;
    }
    else if (worldPos.x >= ScreenManager::GetScreenDimensions().x)
    {
      worldPos.x -= halfMaxDimensions;
    }

    if (worldPos.y <= 0)
    {
      worldPos.y += halfMaxDimensions;
    }
    else if (worldPos.y >= ScreenManager::GetScreenDimensions().y)
    {
      worldPos.y -= halfMaxDimensions;
    }

    SetVisible(ScreenManager::GetCamera().GetBoundingFrame().Contains(worldPos) != ContainmentType::DISJOINT);
  }
}
Ejemplo n.º 7
0
 virtual void prepare(CameraInfo const &cam)
 {
     Vec3 wPos(worldPos());
     Vec3 back(wPos);
     subFrom(back, cam_->lookAt);
     normalize(back);
     cam_->back = back;
     Vec3 up(0, 0, 1);
     Vec3 right;
     cross(right, up, back);
     normalize(right);
     cross(up, back, right);
     cam_->mmat_.setRow(0, right);
     cam_->mmat_.setRow(1, up);
     cam_->mmat_.setRow(2, back);
     Vec3 zPos;
     cam_->mmat_.setTranslation(zPos);
     cam_->mmat_.setRow(3, zPos);
     subFrom(zPos, wPos);
     multiply(cam_->mmat_, zPos);
     cam_->mmat_.setTranslation(zPos);
 }
Ejemplo n.º 8
0
int SceneNode::VRenderChildren( Scene *pScene )
   {
   for( auto it : m_Children )
      {
      if( it->VPreRender( pScene ) == S_OK )
         {
         if( it->VIsVisible( pScene ) )
            {
            float alpha = it->VGetProperties().GetAlpha( );
            if( alpha == fOPAQUE )
               {
               it->VRender( pScene );
               }
            else if( alpha != fTRANSPARENT )
               {
               // The object isn't totally transparent...
               AlphaSceneNode *asn = ENG_NEW AlphaSceneNode;
               ENG_ASSERT( asn );
               asn->m_pNode = it;
               asn->m_Concat = pScene->GetTopTransform();

               Vec4 worldPos( asn->m_Concat.GetToWorldPosition() );

               Mat4x4 fromWorld = pScene->GetCamera( )->VGetProperties().GetFromWorld();

               Vec4 screenPos = fromWorld.Xform( worldPos );

               asn->m_ScreenZ = screenPos.z;

               pScene->AddAlphaSceneNode( asn );
               }

            it->VRenderChildren( pScene );
            }
         }
      it->VPostRender( pScene );
      }
   return S_OK;
   }
Ejemplo n.º 9
0
bool Game::onMouseMotion(const SDL_MouseMotionEvent &_event)
{
	b2Vec2 worldPos(_event.x, _event.y + m_cameraOffset);
	m_lastPos = worldPos;

	if(m_grab)
	{
		if(m_isRotating)
		{
			if(Entities::Box *box = dynamic_cast<Entities::Box*>(m_grabbed))
			{
				const float rotAmt = (_event.xrel / float(800)) * M_PI * 2;
				b2Body *body = box->body().body();
				body->SetTransform(body->GetPosition(), body->GetAngle() + rotAmt);
			}
		}
		else
			m_grab->SetTarget(worldToPhysics(worldPos));
	}

	return false;
}
Ejemplo n.º 10
0
void MkWindowBaseNode::_StartCursorReport(ePA_SceneNodeEvent evt, const MkInt2& position)
{
	// report
	MkFloat2 worldPos(static_cast<float>(position.x), static_cast<float>(position.y));
	MkFloat2 localPos = worldPos - GetWorldPosition();

	MkDataNode arg;
	arg.CreateUnit(MkWindowBaseNode::ArgKey_CursorLocalPosition, MkVec2(localPos.x, localPos.y));
	arg.CreateUnit(MkWindowBaseNode::ArgKey_CursorWorldPosition, MkVec2(worldPos.x, worldPos.y));
	StartNodeReportTypeEvent(evt, &arg);

	// call back
	if (!m_CallBackTargetWindowPath.Empty())
	{
		do
		{
			MkSceneNode* mgrNode = FindNearestDerivedNode(ePA_SNT_WindowManagerNode); // 소속 window mananger
			if (mgrNode == NULL)
				break;

			MkSceneNode* targetNode = mgrNode->GetChildNode(m_CallBackTargetWindowPath); // call back을 호출 할 node
			if ((targetNode == NULL) || (!targetNode->IsDerivedFrom(ePA_SNT_WindowBaseNode)))
				break;

			MkWindowBaseNode* winBase = dynamic_cast<MkWindowBaseNode*>(targetNode);
			if (winBase == NULL)
				break;

			MkArray<MkHashStr> myPath;
			GetWindowPath(myPath);
			winBase->CallBackOperation(evt, &arg, myPath);
			return;
		}
		while (false);

		m_CallBackTargetWindowPath.Clear(); // 부정 경로. 초기화
	}
}
Ejemplo n.º 11
0
void SliderConstraintDemo::initPhysics()
{
    setTexturing(true);
    setShadows(true);

    setCameraDistance(26.f);

    // init world
    m_collisionConfiguration = new btDefaultCollisionConfiguration();
    m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
    btVector3 worldMin(-1000,-1000,-1000);
    btVector3 worldMax(1000,1000,1000);
    m_overlappingPairCache = new btAxisSweep3(worldMin,worldMax);

#if SLIDER_DEMO_USE_ODE_SOLVER
    m_constraintSolver = new btOdeQuickstepConstraintSolver();
#else
    m_constraintSolver = new btSequentialImpulseConstraintSolver();
#endif

    btDiscreteDynamicsWorld* wp = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_constraintSolver,m_collisionConfiguration);
    //	wp->getSolverInfo().m_numIterations = 20; // default is 10
    m_dynamicsWorld = wp;

    // add floor
    //btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
    btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
    m_collisionShapes.push_back(groundShape);
    btTransform groundTransform;
    groundTransform.setIdentity();
    groundTransform.setOrigin(btVector3(0,-56,0));
    btRigidBody* groundBody = localCreateRigidBody(0, groundTransform, groundShape);

    // add box shape (will be reused for all bodies)
    btCollisionShape* shape = new btBoxShape(btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS));
    m_collisionShapes.push_back(shape);
    // mass of dymamic bodies
    btScalar mass = btScalar(1.);

    // add dynamic rigid body A1
    btTransform trans;
    trans.setIdentity();
    btVector3 worldPos(0,0,0);
    trans.setOrigin(worldPos);
    btRigidBody* pRbA1 = localCreateRigidBody(mass, trans, shape);
    pRbA1->setActivationState(DISABLE_DEACTIVATION);

    // add dynamic rigid body B1
    worldPos.setValue(-10,0,0);
    trans.setOrigin(worldPos);
    btRigidBody* pRbB1 = localCreateRigidBody(mass, trans, shape);
    pRbB1->setActivationState(DISABLE_DEACTIVATION);

    // create slider constraint between A1 and B1 and add it to world
    btTransform frameInA, frameInB;
    frameInA = btTransform::getIdentity();
    frameInB = btTransform::getIdentity();

#if SLIDER_DEMO_USE_6DOF
    spSlider1 = new btGeneric6DofConstraint(*pRbA1, *pRbB1, frameInA, frameInB, true);
    btVector3 lowerSliderLimit = btVector3(-20,0,0);
    btVector3 hiSliderLimit = btVector3(-10,0,0);
//	btVector3 lowerSliderLimit = btVector3(-20,-5,-5);
//	btVector3 hiSliderLimit = btVector3(-10,5,5);
    spSlider1->setLinearLowerLimit(lowerSliderLimit);
    spSlider1->setLinearUpperLimit(hiSliderLimit);
    spSlider1->setAngularLowerLimit(btVector3(0,0,0));
    spSlider1->setAngularUpperLimit(btVector3(0,0,0));
#else
    spSlider1 = new btSliderConstraint(*pRbA1, *pRbB1, frameInA, frameInB, true);
    spSlider1->setLowerLinLimit(-15.0F);
    spSlider1->setUpperLinLimit(-5.0F);
//	spSlider1->setLowerLinLimit(-10.0F);
//	spSlider1->setUpperLinLimit(-10.0F);
    spSlider1->setLowerAngLimit(-SIMD_PI / 3.0F);
    spSlider1->setUpperAngLimit( SIMD_PI / 3.0F);
#endif

    m_dynamicsWorld->addConstraint(spSlider1, true);

    // add kinematic rigid body A2
    worldPos.setValue(0,2,0);
    trans.setOrigin(worldPos);
    btRigidBody* pRbA2 = localCreateRigidBody(0, trans, shape);
    pRbA2->setActivationState(DISABLE_DEACTIVATION);

    // add dynamic rigid body B2
    worldPos.setValue(-10,2,0);
    trans.setOrigin(worldPos);
    btRigidBody* pRbB2 = localCreateRigidBody(mass, trans, shape);
    pRbB2->setActivationState(DISABLE_DEACTIVATION);

    // create slider constraint between A2 and B2 and add it to world
#if SLIDER_DEMO_USE_6DOF
    spSlider2 = new btGeneric6DofConstraint(*pRbA2, *pRbB2, frameInA, frameInB, true);
    spSlider2->setLinearLowerLimit(lowerSliderLimit);
    spSlider2->setLinearUpperLimit(hiSliderLimit);
    spSlider2->setAngularLowerLimit(btVector3(0,0,0));
    spSlider2->setAngularUpperLimit(btVector3(0,0,0));
#else
    spSlider2 = new btSliderConstraint(*pRbA2, *pRbB2, frameInA, frameInB, true);
    spSlider2->setLowerLinLimit(-25.0F);
    spSlider2->setUpperLinLimit(-5.0F);
    spSlider2->setLowerAngLimit(SIMD_PI / 2.0F);
    spSlider2->setUpperAngLimit(-SIMD_PI / 2.0F);
    // add motors
    spSlider2->setPoweredLinMotor(true);
    spSlider2->setMaxLinMotorForce(0.1);
    spSlider2->setTargetLinMotorVelocity(5.0);

    spSlider2->setPoweredAngMotor(true);
//	spSlider2->setMaxAngMotorForce(0.01);
    spSlider2->setMaxAngMotorForce(10.0);
    spSlider2->setTargetAngMotorVelocity(1.0);

    // change default damping and restitution

    spSlider2->setDampingDirLin(0.005F);
    spSlider2->setRestitutionLimLin(1.1F);

    // various ODE tests
//	spSlider2->setDampingLimLin(0.1F); // linear bounce factor for ODE == 1.0 - DampingLimLin;
//	spSlider2->setDampingLimAng(0.1F); // angular bounce factor for ODE == 1.0 - DampingLimAng;
//	spSlider2->setSoftnessOrthoAng(0.1);
//	spSlider2->setSoftnessOrthoLin(0.1);
//	spSlider2->setSoftnessLimLin(0.1);
//	spSlider2->setSoftnessLimAng(0.1);
#endif
    m_dynamicsWorld->addConstraint(spSlider2, true);


} // SliderConstraintDemo::initPhysics()
Ejemplo n.º 12
0
void	AllConstraintDemo::initPhysics()
{
	m_guiHelper->setUpAxis(1);

	m_Time = 0;

	setupEmptyDynamicsWorld();

	m_guiHelper->createPhysicsDebugDrawer(m_dynamicsWorld);

	//btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(40.),btScalar(50.)));
	btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),40);

	m_collisionShapes.push_back(groundShape);
	btTransform groundTransform;
	groundTransform.setIdentity();
	groundTransform.setOrigin(btVector3(0,-56,0));
	btRigidBody* groundBody;
	groundBody= createRigidBody(0, groundTransform, groundShape);



	btCollisionShape* shape = new btBoxShape(btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS));
	m_collisionShapes.push_back(shape);
	btTransform trans;
	trans.setIdentity();
	trans.setOrigin(btVector3(0,20,0));

	float mass = 1.f;

#if ENABLE_ALL_DEMOS
///gear constraint demo

#define THETA SIMD_PI/4.f
#define L_1 (2 - tan(THETA))
#define L_2 (1 / cos(THETA))
#define RATIO L_2 / L_1

	btRigidBody* bodyA=0;
	btRigidBody* bodyB=0;

	{
		btCollisionShape* cylA = new btCylinderShape(btVector3(0.2,0.25,0.2));
		btCollisionShape* cylB = new btCylinderShape(btVector3(L_1,0.025,L_1));
		btCompoundShape* cyl0 = new btCompoundShape();
		cyl0->addChildShape(btTransform::getIdentity(),cylA);
		cyl0->addChildShape(btTransform::getIdentity(),cylB);

		btScalar mass = 6.28;
		btVector3 localInertia;
		cyl0->calculateLocalInertia(mass,localInertia);
		btRigidBody::btRigidBodyConstructionInfo ci(mass,0,cyl0,localInertia);
		ci.m_startWorldTransform.setOrigin(btVector3(-8,1,-8));

		btRigidBody* body = new btRigidBody(ci);//1,0,cyl0,localInertia);
		m_dynamicsWorld->addRigidBody(body);
		body->setLinearFactor(btVector3(0,0,0));
		body->setAngularFactor(btVector3(0,1,0));
		bodyA = body;
	}

	{
		btCollisionShape* cylA = new btCylinderShape(btVector3(0.2,0.26,0.2));
		btCollisionShape* cylB = new btCylinderShape(btVector3(L_2,0.025,L_2));
		btCompoundShape* cyl0 = new btCompoundShape();
		cyl0->addChildShape(btTransform::getIdentity(),cylA);
		cyl0->addChildShape(btTransform::getIdentity(),cylB);

		btScalar mass = 6.28;
		btVector3 localInertia;
		cyl0->calculateLocalInertia(mass,localInertia);
		btRigidBody::btRigidBodyConstructionInfo ci(mass,0,cyl0,localInertia);
		ci.m_startWorldTransform.setOrigin(btVector3(-10,2,-8));


		btQuaternion orn(btVector3(0,0,1),-THETA);
		ci.m_startWorldTransform.setRotation(orn);

		btRigidBody* body = new btRigidBody(ci);//1,0,cyl0,localInertia);
		body->setLinearFactor(btVector3(0,0,0));
		btHingeConstraint* hinge = new btHingeConstraint(*body,btVector3(0,0,0),btVector3(0,1,0),true);
		m_dynamicsWorld->addConstraint(hinge);
		bodyB= body;
		body->setAngularVelocity(btVector3(0,3,0));

		m_dynamicsWorld->addRigidBody(body);
	}

	btVector3	axisA(0,1,0);
	btVector3	axisB(0,1,0);
	btQuaternion orn(btVector3(0,0,1),-THETA);
	btMatrix3x3 mat(orn);
	axisB = mat.getRow(1);

	btGearConstraint* gear = new btGearConstraint(*bodyA,*bodyB, axisA,axisB,RATIO);
	m_dynamicsWorld->addConstraint(gear,true);


#endif


#if ENABLE_ALL_DEMOS
	//point to point constraint with a breaking threshold
	{
		trans.setIdentity();
		trans.setOrigin(btVector3(1,30,-5));
		createRigidBody( mass,trans,shape);
		trans.setOrigin(btVector3(0,0,-5));

		btRigidBody* body0 = createRigidBody( mass,trans,shape);
		trans.setOrigin(btVector3(2*CUBE_HALF_EXTENTS,20,0));
		mass = 1.f;
	//	btRigidBody* body1 = 0;//createRigidBody( mass,trans,shape);
		btVector3 pivotInA(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,0);
		btTypedConstraint* p2p = new btPoint2PointConstraint(*body0,pivotInA);
		m_dynamicsWorld->addConstraint(p2p);
		p2p ->setBreakingImpulseThreshold(10.2);
		p2p->setDbgDrawSize(btScalar(5.f));
	}
#endif



#if ENABLE_ALL_DEMOS
	//point to point constraint (ball socket)
	{
		btRigidBody* body0 = createRigidBody( mass,trans,shape);
		trans.setOrigin(btVector3(2*CUBE_HALF_EXTENTS,20,0));

		mass = 1.f;
//		btRigidBody* body1 = 0;//createRigidBody( mass,trans,shape);
//		btRigidBody* body1 = createRigidBody( 0.0,trans,0);
		//body1->setActivationState(DISABLE_DEACTIVATION);
		//body1->setDamping(0.3,0.3);

		btVector3 pivotInA(CUBE_HALF_EXTENTS,-CUBE_HALF_EXTENTS,-CUBE_HALF_EXTENTS);
		btVector3 axisInA(0,0,1);

	//	btVector3 pivotInB = body1 ? body1->getCenterOfMassTransform().inverse()(body0->getCenterOfMassTransform()(pivotInA)) : pivotInA;
//		btVector3 axisInB = body1?
//			(body1->getCenterOfMassTransform().getBasis().inverse()*(body1->getCenterOfMassTransform().getBasis() * axisInA)) :
		body0->getCenterOfMassTransform().getBasis() * axisInA;

#define P2P
#ifdef P2P
		btTypedConstraint* p2p = new btPoint2PointConstraint(*body0,pivotInA);
		//btTypedConstraint* p2p = new btPoint2PointConstraint(*body0,*body1,pivotInA,pivotInB);
		//btTypedConstraint* hinge = new btHingeConstraint(*body0,*body1,pivotInA,pivotInB,axisInA,axisInB);
		m_dynamicsWorld->addConstraint(p2p);
		p2p->setDbgDrawSize(btScalar(5.f));
#else
		btHingeConstraint* hinge = new btHingeConstraint(*body0,pivotInA,axisInA);
		
		//use zero targetVelocity and a small maxMotorImpulse to simulate joint friction
		//float	targetVelocity = 0.f;
		//float	maxMotorImpulse = 0.01;
		float	targetVelocity = 1.f;
		float	maxMotorImpulse = 1.0f;
		hinge->enableAngularMotor(true,targetVelocity,maxMotorImpulse);
		m_dynamicsWorld->addConstraint(hinge);
		hinge->setDbgDrawSize(btScalar(5.f));
#endif //P2P
		

		

	}
#endif

	
#if ENABLE_ALL_DEMOS
	{
		btTransform trans;
	trans.setIdentity();
	btVector3 worldPos(-20,0,30);
	trans.setOrigin(worldPos);

	btTransform frameInA, frameInB;
	frameInA = btTransform::getIdentity();
	frameInB = btTransform::getIdentity();

	btRigidBody* pRbA1 = createRigidBody(mass, trans, shape);
//	btRigidBody* pRbA1 = createRigidBody(0.f, trans, shape);
	pRbA1->setActivationState(DISABLE_DEACTIVATION);

	// add dynamic rigid body B1
	worldPos.setValue(-30,0,30);
	trans.setOrigin(worldPos);
	btRigidBody* pRbB1 = createRigidBody(mass, trans, shape);
//	btRigidBody* pRbB1 = createRigidBody(0.f, trans, shape);
	pRbB1->setActivationState(DISABLE_DEACTIVATION);

	// create slider constraint between A1 and B1 and add it to world
	
	btSliderConstraint* spSlider1 = new btSliderConstraint(*pRbA1, *pRbB1, frameInA, frameInB, true);
//	spSlider1 = new btSliderConstraint(*pRbA1, *pRbB1, frameInA, frameInB, false);
	spSlider1->setLowerLinLimit(-15.0F);
	spSlider1->setUpperLinLimit(-5.0F);
//	spSlider1->setLowerLinLimit(5.0F);
//	spSlider1->setUpperLinLimit(15.0F);
//	spSlider1->setLowerLinLimit(-10.0F);
//	spSlider1->setUpperLinLimit(-10.0F);

	spSlider1->setLowerAngLimit(-SIMD_PI / 3.0F);
	spSlider1->setUpperAngLimit( SIMD_PI / 3.0F);


	m_dynamicsWorld->addConstraint(spSlider1, true);
	spSlider1->setDbgDrawSize(btScalar(5.f));
	}
#endif

#if ENABLE_ALL_DEMOS	
	//create a slider, using the generic D6 constraint
	{
		mass = 1.f;
		btVector3 sliderWorldPos(0,10,0);
		btVector3 sliderAxis(1,0,0);
		btScalar angle=0.f;//SIMD_RADS_PER_DEG * 10.f;
		btMatrix3x3 sliderOrientation(btQuaternion(sliderAxis ,angle));
		trans.setIdentity();
		trans.setOrigin(sliderWorldPos);
		//trans.setBasis(sliderOrientation);
		sliderTransform = trans;

		d6body0 = createRigidBody( mass,trans,shape);
		d6body0->setActivationState(DISABLE_DEACTIVATION);
		btRigidBody* fixedBody1 = createRigidBody(0,trans,0);
		m_dynamicsWorld->addRigidBody(fixedBody1);

		btTransform frameInA, frameInB;
		frameInA = btTransform::getIdentity();
		frameInB = btTransform::getIdentity();
		frameInA.setOrigin(btVector3(0., 5., 0.));
		frameInB.setOrigin(btVector3(0., 5., 0.));

//		bool useLinearReferenceFrameA = false;//use fixed frame B for linear llimits
		bool useLinearReferenceFrameA = true;//use fixed frame A for linear llimits
		spSlider6Dof = new btGeneric6DofConstraint(*fixedBody1, *d6body0,frameInA,frameInB,useLinearReferenceFrameA);
		spSlider6Dof->setLinearLowerLimit(lowerSliderLimit);
		spSlider6Dof->setLinearUpperLimit(hiSliderLimit);

		//range should be small, otherwise singularities will 'explode' the constraint
//		spSlider6Dof->setAngularLowerLimit(btVector3(-1.5,0,0));
//		spSlider6Dof->setAngularUpperLimit(btVector3(1.5,0,0));
//		spSlider6Dof->setAngularLowerLimit(btVector3(0,0,0));
//		spSlider6Dof->setAngularUpperLimit(btVector3(0,0,0));
		spSlider6Dof->setAngularLowerLimit(btVector3(-SIMD_PI,0,0));
		spSlider6Dof->setAngularUpperLimit(btVector3(1.5,0,0));

		spSlider6Dof->getTranslationalLimitMotor()->m_enableMotor[0] = true;
		spSlider6Dof->getTranslationalLimitMotor()->m_targetVelocity[0] = -5.0f;
		spSlider6Dof->getTranslationalLimitMotor()->m_maxMotorForce[0] = 6.0f;


		m_dynamicsWorld->addConstraint(spSlider6Dof);
		spSlider6Dof->setDbgDrawSize(btScalar(5.f));

	}
#endif
#if ENABLE_ALL_DEMOS
	{ // create a door using hinge constraint attached to the world
		btCollisionShape* pDoorShape = new btBoxShape(btVector3(2.0f, 5.0f, 0.2f));
		m_collisionShapes.push_back(pDoorShape);
		btTransform doorTrans;
		doorTrans.setIdentity();
		doorTrans.setOrigin(btVector3(-5.0f, -2.0f, 0.0f));
		btRigidBody* pDoorBody = createRigidBody( 1.0, doorTrans, pDoorShape);
		pDoorBody->setActivationState(DISABLE_DEACTIVATION);
		const btVector3 btPivotA(10.f +  2.1f, -2.0f, 0.0f ); // right next to the door slightly outside
		btVector3 btAxisA( 0.0f, 1.0f, 0.0f ); // pointing upwards, aka Y-axis

		spDoorHinge = new btHingeConstraint( *pDoorBody, btPivotA, btAxisA );

//		spDoorHinge->setLimit( 0.0f, SIMD_PI_2 );
		// test problem values
//		spDoorHinge->setLimit( -SIMD_PI, SIMD_PI*0.8f);

//		spDoorHinge->setLimit( 1.f, -1.f);
//		spDoorHinge->setLimit( -SIMD_PI*0.8f, SIMD_PI);
//		spDoorHinge->setLimit( -SIMD_PI*0.8f, SIMD_PI, 0.9f, 0.3f, 0.0f);
//		spDoorHinge->setLimit( -SIMD_PI*0.8f, SIMD_PI, 0.9f, 0.01f, 0.0f); // "sticky limits"
		spDoorHinge->setLimit( -SIMD_PI * 0.25f, SIMD_PI * 0.25f );
//		spDoorHinge->setLimit( 0.0f, 0.0f );
		m_dynamicsWorld->addConstraint(spDoorHinge);
		spDoorHinge->setDbgDrawSize(btScalar(5.f));

		//doorTrans.setOrigin(btVector3(-5.0f, 2.0f, 0.0f));
		//btRigidBody* pDropBody = createRigidBody( 10.0, doorTrans, shape);
	}
#endif
#if ENABLE_ALL_DEMOS
	{ // create a generic 6DOF constraint

		btTransform tr;
		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(10.), btScalar(6.), btScalar(0.)));
		tr.getBasis().setEulerZYX(0,0,0);
//		btRigidBody* pBodyA = createRigidBody( mass, tr, shape);
		btRigidBody* pBodyA = createRigidBody( 0.0, tr, shape);
//		btRigidBody* pBodyA = createRigidBody( 0.0, tr, 0);
		pBodyA->setActivationState(DISABLE_DEACTIVATION);

		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(0.), btScalar(6.), btScalar(0.)));
		tr.getBasis().setEulerZYX(0,0,0);
		btRigidBody* pBodyB = createRigidBody(mass, tr, shape);
//		btRigidBody* pBodyB = createRigidBody(0.f, tr, shape);
		pBodyB->setActivationState(DISABLE_DEACTIVATION);

		btTransform frameInA, frameInB;
		frameInA = btTransform::getIdentity();
		frameInA.setOrigin(btVector3(btScalar(-5.), btScalar(0.), btScalar(0.)));
		frameInB = btTransform::getIdentity();
		frameInB.setOrigin(btVector3(btScalar(5.), btScalar(0.), btScalar(0.)));

		btGeneric6DofConstraint* pGen6DOF = new btGeneric6DofConstraint(*pBodyA, *pBodyB, frameInA, frameInB, true);
//		btGeneric6DofConstraint* pGen6DOF = new btGeneric6DofConstraint(*pBodyA, *pBodyB, frameInA, frameInB, false);
		pGen6DOF->setLinearLowerLimit(btVector3(-10., -2., -1.));
		pGen6DOF->setLinearUpperLimit(btVector3(10., 2., 1.));
//		pGen6DOF->setLinearLowerLimit(btVector3(-10., 0., 0.));
//		pGen6DOF->setLinearUpperLimit(btVector3(10., 0., 0.));
//		pGen6DOF->setLinearLowerLimit(btVector3(0., 0., 0.));
//		pGen6DOF->setLinearUpperLimit(btVector3(0., 0., 0.));

//		pGen6DOF->getTranslationalLimitMotor()->m_enableMotor[0] = true;
//		pGen6DOF->getTranslationalLimitMotor()->m_targetVelocity[0] = 5.0f;
//		pGen6DOF->getTranslationalLimitMotor()->m_maxMotorForce[0] = 6.0f;


//		pGen6DOF->setAngularLowerLimit(btVector3(0., SIMD_HALF_PI*0.9, 0.));
//		pGen6DOF->setAngularUpperLimit(btVector3(0., -SIMD_HALF_PI*0.9, 0.));
//		pGen6DOF->setAngularLowerLimit(btVector3(0., 0., -SIMD_HALF_PI));
//		pGen6DOF->setAngularUpperLimit(btVector3(0., 0., SIMD_HALF_PI));

		pGen6DOF->setAngularLowerLimit(btVector3(-SIMD_HALF_PI * 0.5f, -0.75, -SIMD_HALF_PI * 0.8f));
		pGen6DOF->setAngularUpperLimit(btVector3(SIMD_HALF_PI * 0.5f, 0.75, SIMD_HALF_PI * 0.8f));
//		pGen6DOF->setAngularLowerLimit(btVector3(0.f, -0.75, SIMD_HALF_PI * 0.8f));
//		pGen6DOF->setAngularUpperLimit(btVector3(0.f, 0.75, -SIMD_HALF_PI * 0.8f));
//		pGen6DOF->setAngularLowerLimit(btVector3(0.f, -SIMD_HALF_PI * 0.8f, SIMD_HALF_PI * 1.98f));
//		pGen6DOF->setAngularUpperLimit(btVector3(0.f, SIMD_HALF_PI * 0.8f,  -SIMD_HALF_PI * 1.98f));

		
		
//		pGen6DOF->setAngularLowerLimit(btVector3(-0.75,-0.5, -0.5));
//		pGen6DOF->setAngularUpperLimit(btVector3(0.75,0.5, 0.5));
//		pGen6DOF->setAngularLowerLimit(btVector3(-0.75,0., 0.));
//		pGen6DOF->setAngularUpperLimit(btVector3(0.75,0., 0.));
//		pGen6DOF->setAngularLowerLimit(btVector3(0., -0.7,0.));
//		pGen6DOF->setAngularUpperLimit(btVector3(0., 0.7, 0.));
//		pGen6DOF->setAngularLowerLimit(btVector3(-1., 0.,0.));
//		pGen6DOF->setAngularUpperLimit(btVector3(1., 0., 0.));

		m_dynamicsWorld->addConstraint(pGen6DOF, true);
		pGen6DOF->setDbgDrawSize(btScalar(5.f));
	}
#endif
#if ENABLE_ALL_DEMOS
	{ // create a ConeTwist constraint

		btTransform tr;
		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(-10.), btScalar(5.), btScalar(0.)));
		tr.getBasis().setEulerZYX(0,0,0);
		btRigidBody* pBodyA = createRigidBody( 1.0, tr, shape);
//		btRigidBody* pBodyA = createRigidBody( 0.0, tr, shape);
		pBodyA->setActivationState(DISABLE_DEACTIVATION);

		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(-10.), btScalar(-5.), btScalar(0.)));
		tr.getBasis().setEulerZYX(0,0,0);
		btRigidBody* pBodyB = createRigidBody(0.0, tr, shape);
//		btRigidBody* pBodyB = createRigidBody(1.0, tr, shape);

		btTransform frameInA, frameInB;
		frameInA = btTransform::getIdentity();
		frameInA.getBasis().setEulerZYX(0, 0, SIMD_PI_2);
		frameInA.setOrigin(btVector3(btScalar(0.), btScalar(-5.), btScalar(0.)));
		frameInB = btTransform::getIdentity();
		frameInB.getBasis().setEulerZYX(0,0,  SIMD_PI_2);
		frameInB.setOrigin(btVector3(btScalar(0.), btScalar(5.), btScalar(0.)));

		m_ctc = new btConeTwistConstraint(*pBodyA, *pBodyB, frameInA, frameInB);
//		m_ctc->setLimit(btScalar(SIMD_PI_4), btScalar(SIMD_PI_4), btScalar(SIMD_PI) * 0.8f);
//		m_ctc->setLimit(btScalar(SIMD_PI_4*0.6f), btScalar(SIMD_PI_4), btScalar(SIMD_PI) * 0.8f, 1.0f); // soft limit == hard limit
		m_ctc->setLimit(btScalar(SIMD_PI_4*0.6f), btScalar(SIMD_PI_4), btScalar(SIMD_PI) * 0.8f, 0.5f);
		m_dynamicsWorld->addConstraint(m_ctc, true);
		m_ctc->setDbgDrawSize(btScalar(5.f));
		// s_bTestConeTwistMotor = true; // use only with old solver for now
		s_bTestConeTwistMotor = false;
	}
#endif
#if ENABLE_ALL_DEMOS
	{ // Hinge connected to the world, with motor (to hinge motor with new and old constraint solver)
		btTransform tr;
		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(0.), btScalar(0.), btScalar(0.)));
		btRigidBody* pBody = createRigidBody( 1.0, tr, shape);
		pBody->setActivationState(DISABLE_DEACTIVATION);
		const btVector3 btPivotA( 10.0f, 0.0f, 0.0f );
		btVector3 btAxisA( 0.0f, 0.0f, 1.0f );

		btHingeConstraint* pHinge = new btHingeConstraint( *pBody, btPivotA, btAxisA );
//		pHinge->enableAngularMotor(true, -1.0, 0.165); // use for the old solver
		pHinge->enableAngularMotor(true, -1.0f, 1.65f); // use for the new SIMD solver
		m_dynamicsWorld->addConstraint(pHinge);
		pHinge->setDbgDrawSize(btScalar(5.f));
	}
#endif

#if ENABLE_ALL_DEMOS
	{ 
		// create a universal joint using generic 6DOF constraint
		// create two rigid bodies
		// static bodyA (parent) on top:
		btTransform tr;
		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(20.), btScalar(4.), btScalar(0.)));
		btRigidBody* pBodyA = createRigidBody( 0.0, tr, shape);
		pBodyA->setActivationState(DISABLE_DEACTIVATION);
		// dynamic bodyB (child) below it :
		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(20.), btScalar(0.), btScalar(0.)));
		btRigidBody* pBodyB = createRigidBody(1.0, tr, shape);
		pBodyB->setActivationState(DISABLE_DEACTIVATION);
		// add some (arbitrary) data to build constraint frames
		btVector3 parentAxis(1.f, 0.f, 0.f); 
		btVector3 childAxis(0.f, 0.f, 1.f); 
		btVector3 anchor(20.f, 2.f, 0.f);

		btUniversalConstraint* pUniv = new btUniversalConstraint(*pBodyA, *pBodyB, anchor, parentAxis, childAxis);
		pUniv->setLowerLimit(-SIMD_HALF_PI * 0.5f, -SIMD_HALF_PI * 0.5f);
		pUniv->setUpperLimit(SIMD_HALF_PI * 0.5f,  SIMD_HALF_PI * 0.5f);
		// add constraint to world
		m_dynamicsWorld->addConstraint(pUniv, true);
		// draw constraint frames and limits for debugging
		pUniv->setDbgDrawSize(btScalar(5.f));
	}
#endif

#if ENABLE_ALL_DEMOS
	{ // create a generic 6DOF constraint with springs 

		btTransform tr;
		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(-20.), btScalar(16.), btScalar(0.)));
		tr.getBasis().setEulerZYX(0,0,0);
		btRigidBody* pBodyA = createRigidBody( 0.0, tr, shape);
		pBodyA->setActivationState(DISABLE_DEACTIVATION);

		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(-10.), btScalar(16.), btScalar(0.)));
		tr.getBasis().setEulerZYX(0,0,0);
		btRigidBody* pBodyB = createRigidBody(1.0, tr, shape);
		pBodyB->setActivationState(DISABLE_DEACTIVATION);

		btTransform frameInA, frameInB;
		frameInA = btTransform::getIdentity();
		frameInA.setOrigin(btVector3(btScalar(10.), btScalar(0.), btScalar(0.)));
		frameInB = btTransform::getIdentity();
		frameInB.setOrigin(btVector3(btScalar(0.), btScalar(0.), btScalar(0.)));

		btGeneric6DofSpringConstraint* pGen6DOFSpring = new btGeneric6DofSpringConstraint(*pBodyA, *pBodyB, frameInA, frameInB, true);
		pGen6DOFSpring->setLinearUpperLimit(btVector3(5., 0., 0.));
		pGen6DOFSpring->setLinearLowerLimit(btVector3(-5., 0., 0.));

		pGen6DOFSpring->setAngularLowerLimit(btVector3(0.f, 0.f, -1.5f));
		pGen6DOFSpring->setAngularUpperLimit(btVector3(0.f, 0.f, 1.5f));

		m_dynamicsWorld->addConstraint(pGen6DOFSpring, true);
		pGen6DOFSpring->setDbgDrawSize(btScalar(5.f));
		
		pGen6DOFSpring->enableSpring(0, true);
		pGen6DOFSpring->setStiffness(0, 39.478f);
		pGen6DOFSpring->setDamping(0, 0.5f);
		pGen6DOFSpring->enableSpring(5, true);
		pGen6DOFSpring->setStiffness(5, 39.478f);
		pGen6DOFSpring->setDamping(0, 0.3f);
		pGen6DOFSpring->setEquilibriumPoint();
	}
#endif
#if ENABLE_ALL_DEMOS
	{ 
		// create a Hinge2 joint
		// create two rigid bodies
		// static bodyA (parent) on top:
		btTransform tr;
		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(-20.), btScalar(4.), btScalar(0.)));
		btRigidBody* pBodyA = createRigidBody( 0.0, tr, shape);
		pBodyA->setActivationState(DISABLE_DEACTIVATION);
		// dynamic bodyB (child) below it :
		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(-20.), btScalar(0.), btScalar(0.)));
		btRigidBody* pBodyB = createRigidBody(1.0, tr, shape);
		pBodyB->setActivationState(DISABLE_DEACTIVATION);
		// add some data to build constraint frames
		btVector3 parentAxis(0.f, 1.f, 0.f); 
		btVector3 childAxis(1.f, 0.f, 0.f); 
		btVector3 anchor(-20.f, 0.f, 0.f);
		btHinge2Constraint* pHinge2 = new btHinge2Constraint(*pBodyA, *pBodyB, anchor, parentAxis, childAxis);
		pHinge2->setLowerLimit(-SIMD_HALF_PI * 0.5f);
		pHinge2->setUpperLimit( SIMD_HALF_PI * 0.5f);
		// add constraint to world
		m_dynamicsWorld->addConstraint(pHinge2, true);
		// draw constraint frames and limits for debugging
		pHinge2->setDbgDrawSize(btScalar(5.f));
	}
#endif
#if  ENABLE_ALL_DEMOS
	{ 
		// create a Hinge joint between two dynamic bodies
		// create two rigid bodies
		// static bodyA (parent) on top:
		btTransform tr;
		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(-20.), btScalar(-2.), btScalar(0.)));
		btRigidBody* pBodyA = createRigidBody( 1.0f, tr, shape);
		pBodyA->setActivationState(DISABLE_DEACTIVATION);
		// dynamic bodyB:
		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(-30.), btScalar(-2.), btScalar(0.)));
		btRigidBody* pBodyB = createRigidBody(10.0, tr, shape);
		pBodyB->setActivationState(DISABLE_DEACTIVATION);
		// add some data to build constraint frames
		btVector3 axisA(0.f, 1.f, 0.f); 
		btVector3 axisB(0.f, 1.f, 0.f); 
		btVector3 pivotA(-5.f, 0.f, 0.f);
		btVector3 pivotB( 5.f, 0.f, 0.f);
		spHingeDynAB = new btHingeConstraint(*pBodyA, *pBodyB, pivotA, pivotB, axisA, axisB);
		spHingeDynAB->setLimit(-SIMD_HALF_PI * 0.5f, SIMD_HALF_PI * 0.5f);
		// add constraint to world
		m_dynamicsWorld->addConstraint(spHingeDynAB, true);
		// draw constraint frames and limits for debugging
		spHingeDynAB->setDbgDrawSize(btScalar(5.f));
	}
#endif

#if ENABLE_ALL_DEMOS
	{ // 6DOF connected to the world, with motor
		btTransform tr;
		tr.setIdentity();
		tr.setOrigin(btVector3(btScalar(10.), btScalar(-15.), btScalar(0.)));
		btRigidBody* pBody = createRigidBody( 1.0, tr, shape);
		pBody->setActivationState(DISABLE_DEACTIVATION);
		btTransform frameB;
		frameB.setIdentity();
		btGeneric6DofConstraint* pGen6Dof = new btGeneric6DofConstraint( *pBody, frameB, false );
		m_dynamicsWorld->addConstraint(pGen6Dof);
		pGen6Dof->setDbgDrawSize(btScalar(5.f));

		pGen6Dof->setAngularLowerLimit(btVector3(0,0,0));
		pGen6Dof->setAngularUpperLimit(btVector3(0,0,0));
		pGen6Dof->setLinearLowerLimit(btVector3(-10., 0, 0));
		pGen6Dof->setLinearUpperLimit(btVector3(10., 0, 0));

		pGen6Dof->getTranslationalLimitMotor()->m_enableMotor[0] = true;
		pGen6Dof->getTranslationalLimitMotor()->m_targetVelocity[0] = 5.0f;
		pGen6Dof->getTranslationalLimitMotor()->m_maxMotorForce[0] = 6.0f;
	}
#endif

	m_guiHelper->autogenerateGraphicsObjects(m_dynamicsWorld);


}
Ejemplo n.º 13
0
Plane PhysPlane::plane() const
{
	return Plane(normal(), worldPos());
}
Ejemplo n.º 14
0
void SliderConstraintDemo::initPhysics()
{
	setTexturing(true);
	setShadows(true);

	setCameraDistance(26.f);

	// init world
	m_collisionConfiguration = new btDefaultCollisionConfiguration();
	m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
	btVector3 worldMin(-1000,-1000,-1000);
	btVector3 worldMax(1000,1000,1000);
	m_overlappingPairCache = new btAxisSweep3(worldMin,worldMax);

#if SLIDER_DEMO_USE_ODE_SOLVER
	m_constraintSolver = new btOdeQuickstepConstraintSolver();
#else
	m_constraintSolver = new btSequentialImpulseConstraintSolver();
#endif

	btDiscreteDynamicsWorld* wp = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_constraintSolver,m_collisionConfiguration);
	//	wp->getSolverInfo().m_numIterations = 20; // default is 10
	m_dynamicsWorld = wp;
//	wp->getSolverInfo().m_erp = 0.8;

	// add floor
	//btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0,1,0),50);
	btCollisionShape* groundShape = new btBoxShape(btVector3(btScalar(50.),btScalar(50.),btScalar(50.)));
	m_collisionShapes.push_back(groundShape);
	btTransform groundTransform;
	groundTransform.setIdentity();
	groundTransform.setOrigin(btVector3(0,-76,0));
	btRigidBody* groundBody;
	groundBody = localCreateRigidBody(0, groundTransform, groundShape);
	
	// add box shape (will be reused for all bodies)
	btCollisionShape* shape = new btBoxShape(btVector3(CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS,CUBE_HALF_EXTENTS));
	m_collisionShapes.push_back(shape);
	// mass of dymamic bodies
	btScalar mass = btScalar(1.);
	
	// add dynamic rigid body A1
	btTransform trans;
	trans.setIdentity();
	btVector3 worldPos(-20,0,0);
	trans.setOrigin(worldPos);
	btTransform frameInA, frameInB;
	frameInA = btTransform::getIdentity();
	frameInB = btTransform::getIdentity();

#if SLIDER_ENABLE_ALL_DEMOS
	btRigidBody* pRbA1 = localCreateRigidBody(mass, trans, shape);
//	btRigidBody* pRbA1 = localCreateRigidBody(0.f, trans, shape);
	pRbA1->setActivationState(DISABLE_DEACTIVATION);

	// add dynamic rigid body B1
	worldPos.setValue(-30,0,0);
	trans.setOrigin(worldPos);
	btRigidBody* pRbB1 = localCreateRigidBody(mass, trans, shape);
//	btRigidBody* pRbB1 = localCreateRigidBody(0.f, trans, shape);
	pRbB1->setActivationState(DISABLE_DEACTIVATION);

	// create slider constraint between A1 and B1 and add it to world
	
#if SLIDER_DEMO_USE_6DOF
	spSlider1 = new btGeneric6DofConstraint(*pRbA1, *pRbB1, frameInA, frameInB, true);
	btVector3 lowerSliderLimit = btVector3(-20,0,0);
	btVector3 hiSliderLimit = btVector3(-10,0,0);
//	btVector3 lowerSliderLimit = btVector3(-20,-5,-5);
//	btVector3 hiSliderLimit = btVector3(-10,5,5);
	spSlider1->setLinearLowerLimit(lowerSliderLimit);
	spSlider1->setLinearUpperLimit(hiSliderLimit);
	spSlider1->setAngularLowerLimit(btVector3(0,0,0));
	spSlider1->setAngularUpperLimit(btVector3(0,0,0));
#else
	spSlider1 = new btSliderConstraint(*pRbA1, *pRbB1, frameInA, frameInB, true);
//	spSlider1 = new btSliderConstraint(*pRbA1, *pRbB1, frameInA, frameInB, false);
	spSlider1->setLowerLinLimit(-15.0F);
	spSlider1->setUpperLinLimit(-5.0F);
//	spSlider1->setLowerLinLimit(5.0F);
//	spSlider1->setUpperLinLimit(15.0F);
//	spSlider1->setLowerLinLimit(-10.0F);
//	spSlider1->setUpperLinLimit(-10.0F);

	spSlider1->setLowerAngLimit(-SIMD_PI / 3.0F);
	spSlider1->setUpperAngLimit( SIMD_PI / 3.0F);
#endif

	m_dynamicsWorld->addConstraint(spSlider1, true);
	spSlider1->setDbgDrawSize(btScalar(5.f));
#endif

#if SLIDER_ENABLE_ALL_DEMOS
	// add kinematic rigid body A2
//	worldPos.setValue(20,4,0);
	worldPos.setValue(5,-20,0);
	trans.setOrigin(worldPos);
	btRigidBody* pRbA2 = localCreateRigidBody(0., trans, shape);
//	btRigidBody* pRbA2 = localCreateRigidBody(mass, trans, shape);
//	btRigidBody* pRbA2 = localCreateRigidBody(mass * 10000, trans, shape);
	pRbA2->setActivationState(DISABLE_DEACTIVATION);

	// add dynamic rigid body B2
//	worldPos.setValue(-20,4,0);
	worldPos.setValue(-5,-20,0);
	trans.setOrigin(worldPos);
//	btRigidBody* pRbB2 = localCreateRigidBody(0., trans, shape);
	btRigidBody* pRbB2 = localCreateRigidBody(mass, trans, shape);
//	btRigidBody* pRbB2 = localCreateRigidBody(mass * 10000, trans, shape);
	pRbB2->setActivationState(DISABLE_DEACTIVATION);

//	frameInA.getBasis().setEulerZYX(1.f, 1.f, 1.f);
//	frameInB.getBasis().setEulerZYX(1.f, 1.f, 1.f);
//	frameInA.getBasis().setEulerZYX(1.f, 1.f, 1.f);
//	frameInB.getBasis().setEulerZYX(1.f, 1.f, 1.f);


//	frameInA.setOrigin(btVector3(-20., 5., 0));
//	frameInB.setOrigin(btVector3( 20., 5., 0));
	frameInA.setOrigin(btVector3(-5., 20., 0));
	frameInB.setOrigin(btVector3( 5., 20., 0));


	// create slider constraint between A2 and B2 and add it to world
#if SLIDER_DEMO_USE_6DOF
	spSlider2 = new btGeneric6DofConstraint(*pRbA2, *pRbB2, frameInA, frameInB, true);
	spSlider2->setLinearLowerLimit(lowerSliderLimit);
	spSlider2->setLinearUpperLimit(hiSliderLimit);
	spSlider2->setAngularLowerLimit(btVector3(0,0,0));
	spSlider2->setAngularUpperLimit(btVector3(0,0,0));
#else
	spSlider2 = new btSliderConstraint(*pRbA2, *pRbB2, frameInA, frameInB, true);
//	spSlider2 = new btSliderConstraint(*pRbA2, *pRbB2, frameInA, frameInB, false);
//	spSlider2->setLowerLinLimit(0.0F);
//	spSlider2->setUpperLinLimit(0.0F);
	spSlider2->setLowerLinLimit(-2.0F);
	spSlider2->setUpperLinLimit(2.0F);
//	spSlider2->setLowerLinLimit(5.0F);
//	spSlider2->setUpperLinLimit(25.0F);
//	spSlider2->setUpperLinLimit(-5.0F);
//	spSlider2->setUpperLinLimit(-9.99F);


//	spSlider2->setLowerAngLimit(SIMD_PI / 2.0F);
//	spSlider2->setUpperAngLimit(-SIMD_PI / 2.0F);

	//	spSlider2->setLowerAngLimit(-SIMD_PI / 2.0F);
//	spSlider2->setUpperAngLimit(SIMD_PI / 2.0F);

//	spSlider2->setLowerAngLimit(-SIMD_PI);
//	spSlider2->setUpperAngLimit(SIMD_PI *0.8F);


//	spSlider2->setLowerAngLimit(-0.01F);
//	spSlider2->setUpperAngLimit(0.01F);
	spSlider2->setLowerAngLimit(-1.570796326F * 0.5f);
	spSlider2->setUpperAngLimit(1.570796326F * 0.5f);
//	spSlider2->setLowerAngLimit(1.F);
//	spSlider2->setUpperAngLimit(-1.F);


//	spSlider2->setDampingLimLin(0.5f);

#if 0
	// add motors
	spSlider2->setPoweredLinMotor(true);
	spSlider2->setMaxLinMotorForce(0.1);
	spSlider2->setTargetLinMotorVelocity(-5.0);

	spSlider2->setPoweredAngMotor(true);
//	spSlider2->setMaxAngMotorForce(0.01);
	spSlider2->setMaxAngMotorForce(10.0);
	spSlider2->setTargetAngMotorVelocity(1.0);

	// change default damping and restitution

	spSlider2->setDampingDirLin(0.005F);
	spSlider2->setRestitutionLimLin(1.1F);
#endif

	// various ODE tests
//	spSlider2->setDampingLimLin(0.1F); // linear bounce factor for ODE == 1.0 - DampingLimLin;
//	spSlider2->setDampingLimAng(0.1F); // angular bounce factor for ODE == 1.0 - DampingLimAng;
//	spSlider2->setSoftnessOrthoAng(0.1);
//	spSlider2->setSoftnessOrthoLin(0.1);
//	spSlider2->setSoftnessLimLin(0.1);
//	spSlider2->setSoftnessLimAng(0.1);
#endif
	m_dynamicsWorld->addConstraint(spSlider2, true);
	spSlider2->setDbgDrawSize(btScalar(5.f));
#endif

#if SLIDER_ENABLE_ALL_DEMOS
{
	// add dynamic rigid body A1
	trans.setIdentity();
	worldPos.setValue(20,0,0);
	trans.setOrigin(worldPos);
	btRigidBody* pRbA3 = localCreateRigidBody(0.0F, trans, shape);
	pRbA3->setActivationState(DISABLE_DEACTIVATION);

	// add dynamic rigid body B1
	worldPos.setValue(25,0,0);
	trans.setOrigin(worldPos);
	btRigidBody* pRbB3 = localCreateRigidBody(mass, trans, shape);
	pRbB3->setActivationState(DISABLE_DEACTIVATION);

	btVector3 pivA( 2.5, 0., 0.);
	btVector3 pivB(-2.5, 0., 0.);
	spP2PConst = new btPoint2PointConstraint(*pRbA3, *pRbB3, pivA, pivB);
	m_dynamicsWorld->addConstraint(spP2PConst, true);
	spP2PConst->setDbgDrawSize(btScalar(5.f));

}
#endif

#if 0 // SLIDER_ENABLE_ALL_DEMOS
	// add dynamic rigid body A4
	trans.setIdentity();
	worldPos.setValue(20,10,0);
	trans.setOrigin(worldPos);
	btRigidBody* pRbA4 = localCreateRigidBody(0.0F, trans, shape);
	pRbA4->setActivationState(DISABLE_DEACTIVATION);

	// add dynamic rigid body B1
	worldPos.setValue(27,10,0);
	trans.setOrigin(worldPos);
	btRigidBody* pRbB4 = localCreateRigidBody(mass, trans, shape);
	pRbB1->setActivationState(DISABLE_DEACTIVATION);


	btVector3 pivA( 2., 0., 0.);
	btVector3 pivB(-5., 0., 0.);
	btVector3 axisA(0., 0., 1.);
	btVector3 axisB(0., 0., 1.);

	spHingeConst = new btHingeConstraint(*pRbA4, *pRbB4, pivA, pivB, axisA, axisB);
//	spHingeConst->setLimit(-1.57, 1.57);
	spHingeConst->setLimit(1.57, -1.57);
	spHingeConst->enableAngularMotor(true, 10.0, 0.19);

	m_dynamicsWorld->addConstraint(spHingeConst, true);
	spHingeConst->setDbgDrawSize(btScalar(5.f));

#endif

} // SliderConstraintDemo::initPhysics()
Ejemplo n.º 15
0
//------------------------------------------------------------------------
void CVehicleMovementStdBoat::UpdateSurfaceEffects(const float deltaTime)
{
  FUNCTION_PROFILER( GetISystem(), PROFILE_GAME );

  if (0 == g_pGameCVars->v_pa_surface)
  {
    ResetParticles();
    return;
  }
  
  IEntity* pEntity = m_pVehicle->GetEntity();
  const Matrix34& worldTM = pEntity->GetWorldTM();
  
  float distSq = worldTM.GetTranslation().GetSquaredDistance(gEnv->pRenderer->GetCamera().GetPosition());
  if (distSq > sqr(300.f) || (distSq > sqr(50.f) && !m_pVehicle->GetGameObject()->IsProbablyVisible()))
    return;

  Matrix34 worldTMInv = worldTM.GetInverted();
  const SVehicleStatus& status = m_pVehicle->GetStatus();    
  float velDot = status.vel * worldTM.GetColumn1();  
  float powerNorm = min(abs(m_movementAction.power), 1.f);

  SEnvironmentParticles* envParams = m_pPaParams->GetEnvironmentParticles();

  SEnvParticleStatus::TEnvEmitters::iterator end = m_paStats.envStats.emitters.end();
  for (SEnvParticleStatus::TEnvEmitters::iterator emitterIt = m_paStats.envStats.emitters.begin(); emitterIt!=end; ++emitterIt)  
  { 
    if (emitterIt->layer < 0)
    {
      assert(0);
      continue;
    }

    const SEnvironmentLayer& layer = envParams->GetLayer(emitterIt->layer);
    
    SEntitySlotInfo info;        
    info.pParticleEmitter = 0;
    pEntity->GetSlotInfo(emitterIt->slot, info);        

    float countScale = 1.f;
    float sizeScale = 1.f;
		float speedScale = 1.f;
    float speed = 0.f;

    // check if helper position is beneath water level      
                
    Vec3 emitterWorldPos = worldTM * emitterIt->quatT.t;
    float waterLevel = gEnv->p3DEngine->GetWaterLevel(&emitterWorldPos);
    int matId = 0;
    
    if (emitterWorldPos.z <= waterLevel+0.1f && m_physStatus[k_mainThread].submergedFraction<0.999f)
    {
      matId = gEnv->pPhysicalWorld->GetWaterMat();
      speed = status.speed;

      bool spray = !strcmp(layer.GetName(), "spray");        
      
      if (spray)
      {
        // slip based          
        speed -= abs(velDot);
      }

      GetParticleScale(layer, speed, powerNorm, countScale, sizeScale, speedScale);
    }
    else
    {
      countScale = 0.f;
    }
    
    if (matId && matId != emitterIt->matId)
    {
      // change effect       
      IParticleEffect* pEff = 0;                
      const char* effect = GetEffectByIndex( matId, layer.GetName() );

      if (effect && (pEff = gEnv->pParticleManager->FindEffect(effect)))
      {  
#if ENABLE_VEHICLE_DEBUG
        if (DebugParticles())              
					CryLog("%s changes water sfx to %s (slot %i)", pEntity->GetName(), effect, emitterIt->slot);
#endif

        if (info.pParticleEmitter)
        {
          info.pParticleEmitter->Activate(false);
          pEntity->FreeSlot(emitterIt->slot);                  
        }

        emitterIt->slot = pEntity->LoadParticleEmitter(emitterIt->slot, pEff);

        if (emitterIt->slot != -1)
          pEntity->SetSlotLocalTM(emitterIt->slot, Matrix34(emitterIt->quatT));

        info.pParticleEmitter = 0;
        pEntity->GetSlotInfo(emitterIt->slot, info);
      }
      else
        countScale = 0.f;
    }

    if (matId)
      emitterIt->matId = matId;

    if (info.pParticleEmitter)
    {
      SpawnParams sp;
      sp.fSizeScale = sizeScale;
      sp.fCountScale = countScale;    
			sp.fSpeedScale = speedScale;
      info.pParticleEmitter->SetSpawnParams(sp);

      if (layer.alignToWater && countScale > 0.f)
      {          
        Vec3 worldPos(emitterWorldPos.x, emitterWorldPos.y, waterLevel+0.05f);

        Matrix34 localTM(emitterIt->quatT);
        localTM.SetTranslation(worldTMInv * worldPos);
        pEntity->SetSlotLocalTM(emitterIt->slot, localTM);           
      }
    }

#if ENABLE_VEHICLE_DEBUG
    if (DebugParticles() && m_pVehicle->IsPlayerDriving())
    {          
      float color[] = {1,1,1,1};
      ColorB red(255,0,0,255);
      IRenderAuxGeom* pAuxGeom = gEnv->pRenderer->GetIRenderAuxGeom();
      
      const char* effect = info.pParticleEmitter ? info.pParticleEmitter->GetName() : "";
      const Matrix34& slotTM = m_pEntity->GetSlotWorldTM(emitterIt->slot);
      Vec3 ppos = slotTM.GetTranslation();
      
      pAuxGeom->DrawSphere(ppos, 0.2f, red);
      pAuxGeom->DrawCone(ppos, slotTM.GetColumn1(), 0.1f, 0.5f, red);
      gEnv->pRenderer->Draw2dLabel(50.f, (float)(400+10*emitterIt->slot), 1.2f, color, false, "<%s> water fx: slot %i [%s], speed %.1f, sizeScale %.2f, countScale %.2f (pos %.0f,%0.f,%0.f)", pEntity->GetName(), emitterIt->slot, effect, speed, sizeScale, countScale, ppos.x, ppos.y, ppos.z);        
    }  
#endif
  }

  // generate water splashes
	Vec3 wakePos;
	if(m_pSplashPos)
	{
		wakePos = m_pSplashPos->GetWorldSpaceTranslation();
	}
	else
	{
		wakePos = worldTM.GetTranslation();
	}
  float wakeWaterLevel = gEnv->p3DEngine->GetWaterLevel(&wakePos);

  const Vec3& localW = m_localSpeed;
  if (localW.x >= 0.f)
    m_diving = false;
      
  if (!m_diving && localW.x < -0.03f && status.speed > 10.f && wakePos.z < m_lastWakePos.z && wakeWaterLevel+0.1f >= wakePos.z)
  {
    float speedRatio = min(1.f, status.speed/(m_maxSpeed*m_factorMaxSpeed)); 
    m_diving = true;              
    
    if (m_pWaveEffect)
    {
      if (IParticleEmitter* pEmitter = pEntity->GetParticleEmitter(m_wakeSlot))
      {
        pEmitter->Activate(false);
        pEntity->FreeSlot(m_wakeSlot);
        m_wakeSlot = -1;
      }

      SpawnParams spawnParams;
      spawnParams.fSizeScale = spawnParams.fCountScale = 0.5f + 0.25f*speedRatio;
      spawnParams.fSizeScale  += 0.4f*m_waveRandomMult;
      spawnParams.fCountScale += cry_random(0.0f, 0.4f);

      m_wakeSlot = pEntity->LoadParticleEmitter(m_wakeSlot, m_pWaveEffect, &spawnParams);        
    }

    // handle splash sound  
    ExecuteTrigger(eSID_Splash);
    SetSoundParam(eSID_Splash, "intensity", 0.2f*speedRatio + 0.5f*m_waveRandomMult);     

    if (m_rpmPitchDir == 0)
    {
      m_rpmPitchDir = -1;
      m_waveSoundPitch = 0.f;
      m_waveSoundAmount = 0.02f + m_waveRandomMult*0.08f;
    }      
  }  

  if (m_wakeSlot != -1)
  { 
    // update emitter local pos to short above waterlevel
    Matrix34 tm;
		if(m_pSplashPos)
			m_pSplashPos->GetVehicleTM(tm);
		else
			tm.SetIdentity();

    Vec3 pos = tm.GetTranslation();
    pos.z = worldTMInv.TransformPoint(Vec3(wakePos.x,wakePos.y,wakeWaterLevel)).z + 0.2f;
    tm.SetTranslation(pos);
    pEntity->SetSlotLocalTM(m_wakeSlot, tm);

#if ENABLE_VEHICLE_DEBUG
    if (IsProfilingMovement())
    {
      Vec3 wPos = worldTM * tm.GetTranslation();
      ColorB col(128, 128, 0, 200);
      gEnv->pRenderer->GetIRenderAuxGeom()->DrawSphere(wPos, 0.4f, col);
      gEnv->pRenderer->GetIRenderAuxGeom()->DrawLine(wPos, col, wPos+Vec3(0,0,1.5f), col);
    }          
#endif
  } 

  m_lastWakePos = wakePos;
}
Ejemplo n.º 16
0
	void Pipeline::worldPos(const vec3 & pos)
	{
		worldPos(pos.x, pos.y, pos.z);
	}
Ejemplo n.º 17
0
	virtual void OnIterStart(SActivationInfo *pActInfo)
	{
		const int type = GetPortInt(pActInfo, EIP_Type);
		const char* area = GetPortString(pActInfo, EIP_Area);

		// Find the entity
		IEntitySystem *pEntitySystem = gEnv->pEntitySystem;
		if (pEntitySystem)
		{
			IEntity *pArea = pEntitySystem->FindEntityByName(area);
			if (pArea)
			{
				IEntityAreaProxy *pAreaProxy = (IEntityAreaProxy*)pArea->GetProxy(ENTITY_PROXY_AREA);
				if (pAreaProxy)
				{
					Vec3 min, max, worldPos(pArea->GetWorldPos());
					min.Set(0.f,0.f,0.f);
					max.Set(0.f,0.f,0.f);
					EEntityAreaType areaType = pAreaProxy->GetAreaType();

					// Construct bounding space around area
					switch (areaType)
					{
						case ENTITY_AREA_TYPE_BOX:
						{
							pAreaProxy->GetBox(min, max);
							min += worldPos;
							max += worldPos;
						}
						break;
						case ENTITY_AREA_TYPE_SPHERE:
						{
							Vec3 center;
							float radius = 0.f;
							pAreaProxy->GetSphere(center, radius);
							
							min.Set(center.x-radius, center.y-radius, center.z-radius);
							max.Set(center.x+radius, center.y+radius, center.z+radius);
						}
						break;
						case ENTITY_AREA_TYPE_SHAPE:
						{
							const Vec3 *points = pAreaProxy->GetPoints();
							const int count = pAreaProxy->GetPointsCount();
							if (count > 0)
							{
								Vec3 p = worldPos + points[0];
								min = p;
								max = p;
								for (int i = 1; i < count; ++i)
								{
									p = worldPos + points[i];
									if (p.x < min.x) min.x = p.x;
									if (p.y < min.y) min.y = p.y;
									if (p.z < min.z) min.z = p.z;
									if (p.x > max.x) max.x = p.x;
									if (p.y > max.y) max.y = p.y;
									if (p.z > max.z) max.z = p.z;
								}
							}
						}
						break;
					}

					IPhysicalWorld *pWorld = gEnv->pPhysicalWorld;
					IPhysicalEntity **ppList = NULL;
					int	numEnts = pWorld->GetEntitiesInBox(min,max,ppList,ent_all);
					for (int i = 0; i < numEnts; ++i)
					{
						const EntityId id = pWorld->GetPhysicalEntityId(ppList[i]);
						const EEntityType entityType = GetEntityType(id);
						if (IsValidType(type, entityType))
						{
							// Sanity check - Test entity's position
							IEntity *pEntity = pEntitySystem->GetEntity(id);
							if (pEntity && pAreaProxy->CalcPointWithin(id, pEntity->GetWorldPos(), pAreaProxy->GetHeight()==0))
							{
								AddEntity(id);
							}
						}
					}
				}
			}
		}
	}