Beispiel #1
0
void Ragdolls::SpawnObject()
{
    ResourceCache* cache = GetContext()->m_ResourceCache.get();

    Node* boxNode = scene_->CreateChild("Sphere");
    boxNode->SetPosition(cameraNode_->GetPosition());
    boxNode->SetRotation(cameraNode_->GetRotation());
    boxNode->SetScale(0.25f);
    StaticModel* boxObject = boxNode->CreateComponent<StaticModel>();
    boxObject->SetModel(cache->GetResource<Model>("Models/Sphere.mdl"));
    boxObject->SetMaterial(cache->GetResource<Material>("Materials/StoneSmall.xml"));
    boxObject->SetCastShadows(true);

    RigidBody* body = boxNode->CreateComponent<RigidBody>();
    body->SetMass(1.0f);
    body->SetRollingFriction(0.15f);
    CollisionShape* shape = boxNode->CreateComponent<CollisionShape>();
    shape->SetSphere(1.0f);

    const float OBJECT_VELOCITY = 10.0f;

    // Set initial velocity for the RigidBody based on camera forward vector. Add also a slight up component
    // to overcome gravity better
    body->SetLinearVelocity(cameraNode_->GetRotation() * Vector3(0.0f, 0.25f, 1.0f) * OBJECT_VELOCITY);
}
Beispiel #2
0
void Ragdolls::CreateScene()
{
    ResourceCache* cache = GetContext()->m_ResourceCache.get();

    scene_ = new Scene(GetContext());

    // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
    // Create a physics simulation world with default parameters, which will update at 60fps. Like the Octree must
    // exist before creating drawable components, the PhysicsWorld must exist before creating physics components.
    // Finally, create a DebugRenderer component so that we can draw physics debug geometry
    scene_->CreateComponent<Octree>();
    scene_->CreateComponent<PhysicsWorld>();
    scene_->CreateComponent<DebugRenderer>();

    // Create a Zone component for ambient lighting & fog control
    Node* zoneNode = scene_->CreateChild("Zone");
    Zone* zone = zoneNode->CreateComponent<Zone>();
    zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
    zone->SetAmbientColor(Color(0.15f, 0.15f, 0.15f));
    zone->SetFogColor(Color(0.5f, 0.5f, 0.7f));
    zone->SetFogStart(100.0f);
    zone->SetFogEnd(300.0f);

    // Create a directional light to the world. Enable cascaded shadows on it
    Node* lightNode = scene_->CreateChild("DirectionalLight");
    lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f));
    Light* light = lightNode->CreateComponent<Light>();
    light->SetLightType(LIGHT_DIRECTIONAL);
    light->SetCastShadows(true);
    light->SetShadowBias(BiasParameters(0.00025f, 0.5f));
    // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
    light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f));

    {
        // Create a floor object, 500 x 500 world units. Adjust position so that the ground is at zero Y
        Node* floorNode = scene_->CreateChild("Floor");
        floorNode->SetPosition(Vector3(0.0f, -0.5f, 0.0f));
        floorNode->SetScale(Vector3(500.0f, 1.0f, 500.0f));
        StaticModel* floorObject = floorNode->CreateComponent<StaticModel>();
        floorObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
        floorObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml"));

        // Make the floor physical by adding RigidBody and CollisionShape components
        RigidBody* body = floorNode->CreateComponent<RigidBody>();
        // We will be spawning spherical objects in this sample. The ground also needs non-zero rolling friction so that
        // the spheres will eventually come to rest
        body->SetRollingFriction(0.15f);
        CollisionShape* shape = floorNode->CreateComponent<CollisionShape>();
        // Set a box shape of size 1 x 1 x 1 for collision. The shape will be scaled with the scene node scale, so the
        // rendering and physics representation sizes should match (the box model is also 1 x 1 x 1.)
        shape->SetBox(Vector3::ONE);
    }

    // Create animated models
    for (int z = -1; z <= 1; ++z)
    {
        for (int x = -4; x <= 4; ++x)
        {
            Node* modelNode = scene_->CreateChild("Jack");
            modelNode->SetPosition(Vector3(x * 5.0f, 0.0f, z * 5.0f));
            modelNode->SetRotation(Quaternion(0.0f, 180.0f, 0.0f));
            AnimatedModel* modelObject = modelNode->CreateComponent<AnimatedModel>();
            modelObject->SetModel(cache->GetResource<Model>("Models/Jack.mdl"));
            modelObject->SetMaterial(cache->GetResource<Material>("Materials/Jack.xml"));
            modelObject->SetCastShadows(true);
            // Set the model to also update when invisible to avoid staying invisible when the model should come into
            // view, but does not as the bounding box is not updated
            modelObject->SetUpdateInvisible(true);

            // Create a rigid body and a collision shape. These will act as a trigger for transforming the
            // model into a ragdoll when hit by a moving object
            RigidBody* body = modelNode->CreateComponent<RigidBody>();
            // The Trigger mode makes the rigid body only detect collisions, but impart no forces on the
            // colliding objects
            body->SetTrigger(true);
            CollisionShape* shape = modelNode->CreateComponent<CollisionShape>();
            // Create the capsule shape with an offset so that it is correctly aligned with the model, which
            // has its origin at the feet
            shape->SetCapsule(0.7f, 2.0f, Vector3(0.0f, 1.0f, 0.0f));

            // Create a custom component that reacts to collisions and creates the ragdoll
            modelNode->CreateComponent<CreateRagdoll>();
        }
    }

    // Create the camera. Limit far clip distance to match the fog. Note: now we actually create the camera node outside
    // the scene, because we want it to be unaffected by scene load / save
    cameraNode_ = new Node(GetContext());
    Camera* camera = cameraNode_->CreateComponent<Camera>();
    camera->setFarClipDistance(300.0f);

    // Set an initial position for the camera scene node above the floor
    cameraNode_->SetPosition(Vector3(0.0f, 3.0f, -20.0f));
}
Void EngineTests::_CreateWorld()
{
    // Floor
    RigidBody * pFloorBody = PhysicsFn->CreateRigidBody( true, m_pFloorShape, 1.0f, Vertex3(0.0f,0.0f,-10.0f) );
    pFloorBody->SetRestitution( 0.0f );
    pFloorBody->SetFriction( 0.0f );
    pFloorBody->SetRollingFriction( 0.0f );
    pFloorBody->SetCollisionGroup( 0x01 );
    pFloorBody->SetCollisionMask( 0x7f );

    m_pFloor = WorldFn->CreateLeaf( TEXT("Floor") );
    m_pFloor->SetMesh( m_pFloorGeometry );
    m_pFloor->SetEffectInstance( m_pEffectInstanceFloor );
    m_pFloor->SetBody( pFloorBody );
    WorldFn->AddChild( m_pFloor );

    // Character
    Vertex3 vInitialPosition( 0.0f, 0.0f, 2.0f );

    KinematicBody * pCharacterBody = PhysicsFn->CreateKinematicBody( false, m_pCharacterShape, 1.0f );
    pCharacterBody->SetRestitution( 0.0f );
    pCharacterBody->SetFriction( 0.0f );
    pCharacterBody->SetRollingFriction( 0.0f );
    pCharacterBody->SetCollisionGroup( 0x01 );
    pCharacterBody->SetCollisionMask( 0x07 );

    m_pCharacterController = PhysicsFn->CreateCharacterController( TEXT("CharacterController"), vInitialPosition, Quaternion::Identity, Vector3::Null, Vector3::Null );
    m_pCharacterController->Enabled = true;
    m_pCharacterController->EnableForces( false );
    m_pCharacterController->SetMovementSpeed( 5.0f );
    pCharacterBody->AttachController( m_pCharacterController );

    m_pCharacter = WorldFn->CreateLeaf( TEXT("Character") );
    m_pCharacter->SetMesh( m_pCharacterGeometry );
    m_pCharacter->SetEffectInstance( m_pEffectInstanceCharacter );
    m_pCharacter->SetBody( pCharacterBody );
    WorldFn->AddChild( m_pCharacter );

    // Sphere/Box stack
    Vertex3 vBallStackPosition( -10.0f, 10.0f, 0.0f );
    Vertex3 vBoxStackPosition( -10.0f, -10.0f, 0.0f );
    GChar strName[64];

    for( UInt i = 0; i < ENGINE_TEST_STACK_SIZE; ++i ) {
        vBallStackPosition.Z = 5.0f * (Scalar)(i+1);
        vBoxStackPosition.Z = 5.0f * (Scalar)(i+1);

        //StringFn->Format( strName, TEXT("Ball_%d"), i );

        //RigidBody * pBallBody = PhysicsFn->CreateRigidBody( false, m_pSphereShape, 1.0f, vBallStackPosition );
        //pBallBody->SetRestitution( 0.0f );
        //pBallBody->SetFriction( 0.0f );
        //pBallBody->SetRollingFriction( 0.0f );

        //m_arrBallStack[i] = WorldFn->CreateLeaf( strName );
        //m_arrBallStack[i]->SetMesh( m_pSphereGeometry );
        //m_arrBallStack[i]->SetEffectInstance( m_pEffectInstanceA );
        //m_arrBallStack[i]->SetBody( pBallBody );
        //WorldFn->AddChild( m_arrBallStack[i] );

        StringFn->Format( strName, TEXT("Box_%d"), i );

        RigidBody * pBoxBody = PhysicsFn->CreateRigidBody( false, m_pBoxShape, 1.0f, vBoxStackPosition );
        pBoxBody->SetRestitution( 0.0f );
        pBoxBody->SetFriction( 0.0f );
        pBoxBody->SetRollingFriction( 0.0f );

        m_arrBoxStack[i] = WorldFn->CreateLeaf( strName );
        m_arrBoxStack[i]->SetMesh( m_pBoxGeometry );
        m_arrBoxStack[i]->SetEffectInstance( m_pEffectInstanceA );
        m_arrBoxStack[i]->SetBody( pBoxBody );
        WorldFn->AddChild( m_arrBoxStack[i] );
    }

    // Joint systems
    //Matrix3 matRotation;
    //matRotation.MakeRotate( 0.0f, SCALAR_PI_4, SCALAR_PI_4, EULER_ANGLES_XYZ );

    //Vertex3 vFixedObjectPosition( 10.0f, 10.0f, 10.0f );
    //Vertex3 vObjectAPosition( 12.0f, 10.0f, 10.0f );
    //Quaternion qObjectAOrientation( matRotation );
    //Vertex3 vObjectBPosition( 7.0f, 10.0f, 10.0f );

    //RigidBody * pFixedBody = PhysicsFn->CreateRigidBody( true, m_pSphereShape, 1.0f, vFixedObjectPosition );
    //pFixedBody->SetRestitution( 0.0f );
    //pFixedBody->SetFriction( 0.0f );
    //pFixedBody->SetRollingFriction( 0.0f );
    //pFixedBody->SetCollisionGroup( 0x80 );
    //pFixedBody->SetCollisionMask( 0 );

    //m_pFixedObject = WorldFn->CreateLeaf( TEXT("FixedObject") );
    //m_pFixedObject->SetMesh( m_pSphereGeometry );
    //m_pFixedObject->SetEffectInstance( m_pEffectInstanceC );
    //m_pFixedObject->SetBody( pFixedBody );
    //WorldFn->AddChild( m_pFixedObject );

    //RigidBody * pObjectABody = PhysicsFn->CreateRigidBody( false, m_pBoxShape, 1.0f, vObjectAPosition ); //, qObjectAOrientation );
    //pObjectABody->SetRestitution( 0.0f );
    //pObjectABody->SetFriction( 0.0f );
    //pObjectABody->SetRollingFriction( 0.0f );
    //pObjectABody->SetCollisionGroup( 0x02 );
    //pObjectABody->SetCollisionMask( 0x03 );

    //m_pObjectA = WorldFn->CreateLeaf( TEXT("ObjectA") );
    //m_pObjectA->SetMesh( m_pBoxGeometry );
    //m_pObjectA->SetEffectInstance( m_pEffectInstanceA );
    //m_pObjectA->SetBody( pObjectABody );
    //WorldFn->AddChild( m_pObjectA );

    //RigidBody * pObjectBBody = PhysicsFn->CreateRigidBody( false, m_pBoxShape, 1.0f, vObjectBPosition );
    //pObjectBBody->SetRestitution( 0.0f );
    //pObjectBBody->SetFriction( 0.0f );
    //pObjectBBody->SetRollingFriction( 0.0f );
    //pObjectBBody->SetCollisionGroup( 0x04 );
    //pObjectBBody->SetCollisionMask( 0x05 );

    //m_pObjectB = WorldFn->CreateLeaf( TEXT("ObjectB") );
    //m_pObjectB->SetMesh( m_pBoxGeometry );
    //m_pObjectB->SetEffectInstance( m_pEffectInstanceB );
    //m_pObjectB->SetBody( pObjectBBody );
    //WorldFn->AddChild( m_pObjectB );

    //Transform3 vJointFrame;

    //Matrix3 matJointFrame;
    //matJointFrame.SetColumn( 0, Vector3::eJ );
    //matJointFrame.SetColumn( 1, Vector3::eK );
    //matJointFrame.SetColumn( 2, Vector3::eI );
    //vJointFrame.SetRotate( matJointFrame );
    //vJointFrame.SetTranslate( Vector3(11.0f, 10.0f, 10.0f) );

    //m_pJointAF = PhysicsFn->CreateJoint( JOINT_CONETWIST, pObjectABody, pFixedBody, vJointFrame );
    //( (JointConeTwist*)(m_pJointAF->GetJoint()) )->EnableTwistLimits( -SCALAR_PI_4, SCALAR_PI_4 );
    //( (JointHinge*)(m_pJointAF->GetJoint()) )->EnableSpring( 0.5f, SCALAR_PI_4 );

    //vJointFrame.SetTranslate( Vector3(9.0f, 10.0f, 10.0f) );

    //m_pJointBF = PhysicsFn->CreateJoint( JOINT_SLIDER, pObjectBBody, pFixedBody, vJointFrame );
    //( (JointSlider*)(m_pJointBF->GetJoint()) )->EnableLimits( -2.0f, +2.0f );
    //( (JointSlider*)(m_pJointBF->GetJoint()) )->EnableSpring( 0.5f, -1.0f );

    // World camera
    m_pRenderCamera = New Camera( true );
    m_pWorldCamera = New WorldCamera3rdPerson( m_pRenderCamera, m_pCharacter, NULL, 3.0f );
    WorldFn->SetWorldCamera( m_pWorldCamera );
}