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); }
CameraMaster::CameraMaster( Context *context, MasterControl *masterControl ) : Object(context), masterControl_{masterControl} { SubscribeToEvent(E_SCENEUPDATE, HANDLER(CameraMaster, HandleSceneUpdate)); //Create the camera. Limit far clip distance to match the fog translationNode_ = masterControl_->world_.scene->CreateChild("CamTrans"); rotationNode_ = translationNode_->CreateChild("CamRot"); camera_ = rotationNode_->CreateComponent<Camera>(); camera_->SetFarClip(1024.0f); //Set an initial position for the camera scene node above the origin //translationNode_->SetPosition(Vector3(0.0f, 3.0f, 0.0f)); translationNode_->SetPosition(Vector3(0.0, 3.0,-20.0)); rotationNode_->SetRotation(Quaternion(0.0f, 90.0f, 0.0f)); rigidBody_ = translationNode_->CreateComponent<RigidBody>(); rigidBody_->SetAngularDamping(10.0f); CollisionShape* collisionShape = translationNode_->CreateComponent<CollisionShape>(); collisionShape->SetSphere(0.1f); rigidBody_->SetMass(1.0f); Node* lightNode = translationNode_->CreateChild("DirectionalLight"); lightNode->SetDirection(Vector3(0.0f, -1.0f, 0.0f)); Light* light = lightNode->CreateComponent<Light>(); light->SetLightType(LIGHT_POINT); light->SetBrightness(0.5f); light->SetColor(Color(0.7f, 0.9f, 0.6f)); light->SetCastShadows(false); SetupViewport(); }
Node* SceneReplication::CreateControllableObject() { ResourceCache* cache = GetSubsystem<ResourceCache>(); // Create the scene node & visual representation. This will be a replicated object Node* ballNode = scene_->CreateChild("Ball"); ballNode->SetPosition(Vector3(Random(40.0f) - 20.0f, 5.0f, Random(40.0f) - 20.0f)); ballNode->SetScale(0.5f); StaticModel* ballObject = ballNode->CreateComponent<StaticModel>(); ballObject->SetModel(cache->GetResource<Model>("Models/Sphere.mdl")); ballObject->SetMaterial(cache->GetResource<Material>("Materials/StoneSmall.xml")); // Create the physics components RigidBody* body = ballNode->CreateComponent<RigidBody>(); body->SetMass(1.0f); body->SetFriction(1.0f); // In addition to friction, use motion damping so that the ball can not accelerate limitlessly body->SetLinearDamping(0.5f); body->SetAngularDamping(0.5f); CollisionShape* shape = ballNode->CreateComponent<CollisionShape>(); shape->SetSphere(1.0f); // Create a random colored point light at the ball so that can see better where is going Light* light = ballNode->CreateComponent<Light>(); light->SetRange(3.0f); light->SetColor(Color(0.5f + (Rand() & 1) * 0.5f, 0.5f + (Rand() & 1) * 0.5f, 0.5f + (Rand() & 1) * 0.5f)); return ballNode; }
heXoCam::heXoCam(Context *context, MasterControl *masterControl): Object(context), yaw_{0.0f}, pitch_{0.0f}, yawDelta_{0.0f}, pitchDelta_{0.0f}, forceMultiplier{1.0f} { masterControl_ = masterControl; SubscribeToEvent(E_SCENEUPDATE, URHO3D_HANDLER(heXoCam, HandleSceneUpdate)); rootNode_ = masterControl_->world.scene->CreateChild("Camera"); Node* leftEye = rootNode_->CreateChild("Left Eye"); leftEye->SetPosition(Vector3::LEFT); stereoCam_.first_ = leftEye->CreateComponent<Camera>(); Node* rightEye = rootNode_->CreateChild("Right Eye"); rightEye->SetPosition(Vector3::RIGHT); stereoCam_.second_ = rightEye->CreateComponent<Camera>(); camera_ = rootNode_->CreateComponent<Camera>(); camera_->SetFarClip(128.0f); rootNode_->SetPosition(Vector3(0.0f, 42.0f, -23.0f)); rootNode_->SetRotation(Quaternion(65.0f, 0.0f, 0.0f)); rigidBody_ = rootNode_->CreateComponent<RigidBody>(); rigidBody_->SetAngularDamping(10.0f); CollisionShape* collisionShape = rootNode_->CreateComponent<CollisionShape>(); collisionShape->SetSphere(0.1f); rigidBody_->SetMass(1.0f); SetupViewport(); }
Pickup::Pickup(Context *context, MasterControl *masterControl): SceneObject(context, masterControl), sinceLastPickup_{0.0f}, chaoInterval_{Random(23.0f, 100.0f)} { rootNode_->SetName("Pickup"); graphicsNode_ = rootNode_->CreateChild("Graphics"); model_ = graphicsNode_->CreateComponent<StaticModel>(); rootNode_->SetScale(0.8f); rootNode_->SetEnabledRecursive(false); rigidBody_ = rootNode_->CreateComponent<RigidBody>(); rigidBody_->SetRestitution(0.666f); rigidBody_->SetMass(0.666f); rigidBody_->SetLinearFactor(Vector3::ONE - Vector3::UP); rigidBody_->SetLinearDamping(0.5f); rigidBody_->SetFriction(0.0f); rigidBody_->SetAngularFactor(Vector3::UP); rigidBody_->SetAngularDamping(0.0f); rigidBody_->ApplyTorque(Vector3::UP * 32.0f); rigidBody_->SetLinearRestThreshold(0.0f); rigidBody_->SetAngularRestThreshold(0.0f); CollisionShape* collisionShape = rootNode_->CreateComponent<CollisionShape>(); collisionShape->SetSphere(1.5f); masterControl_->tileMaster_->AddToAffectors(WeakPtr<Node>(rootNode_), WeakPtr<RigidBody>(rigidBody_)); triggerNode_ = masterControl_->world.scene->CreateChild("PickupTrigger"); triggerBody_ = triggerNode_->CreateComponent<RigidBody>(); triggerBody_->SetTrigger(true); triggerBody_->SetMass(0.0f); triggerBody_->SetLinearFactor(Vector3::ZERO); CollisionShape* triggerShape = triggerNode_->CreateComponent<CollisionShape>(); triggerShape->SetSphere(2.5f); particleEmitter_ = graphicsNode_->CreateComponent<ParticleEmitter>(); particleEmitter_->SetEffect(masterControl_->cache_->GetTempResource<ParticleEffect>("Resources/Particles/Shine.xml")); SubscribeToEvent(triggerNode_, E_NODECOLLISIONSTART, URHO3D_HANDLER(Pickup, HandleTriggerStart)); SubscribeToEvent(E_SCENEUPDATE, URHO3D_HANDLER(Pickup, HandleSceneUpdate)); }
void Vehicle::InitWheel(const String& name, const Vector3& offset, WeakPtr<Node>& wheelNode, unsigned& wheelNodeID) { ResourceCache* cache = GetSubsystem<ResourceCache>(); // Note: do not parent the wheel to the hull scene node. Instead create it on the root level and let the physics // constraint keep it together wheelNode = GetScene()->CreateChild(name); wheelNode->SetPosition(node_->LocalToWorld(offset)); wheelNode->SetRotation(node_->GetRotation() * (offset.x_ >= 0.0 ? Quaternion(0.0f, 0.0f, -90.0f) : Quaternion(0.0f, 0.0f, 90.0f))); wheelNode->SetScale(Vector3(0.8f, 0.5f, 0.8f)); // Remember the ID for serialization wheelNodeID = wheelNode->GetID(); StaticModel* wheelObject = wheelNode->CreateComponent<StaticModel>(); RigidBody* wheelBody = wheelNode->CreateComponent<RigidBody>(); CollisionShape* wheelShape = wheelNode->CreateComponent<CollisionShape>(); Constraint* wheelConstraint = wheelNode->CreateComponent<Constraint>(); wheelObject->SetModel(cache->GetResource<Model>("Models/Cylinder.mdl")); wheelObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml")); wheelObject->SetCastShadows(true); wheelShape->SetSphere(1.0f); wheelBody->SetFriction(1.0f); wheelBody->SetMass(1.0f); wheelBody->SetLinearDamping(0.2f); // Some air resistance wheelBody->SetAngularDamping(0.75f); // Could also use rolling friction wheelBody->SetCollisionLayer(1); wheelConstraint->SetConstraintType(CONSTRAINT_HINGE); wheelConstraint->SetOtherBody(GetComponent<RigidBody>()); // Connect to the hull body wheelConstraint->SetWorldPosition(wheelNode->GetPosition()); // Set constraint's both ends at wheel's location wheelConstraint->SetAxis(Vector3::UP); // Wheel rotates around its local Y-axis wheelConstraint->SetOtherAxis(offset.x_ >= 0.0 ? Vector3::RIGHT : Vector3::LEFT); // Wheel's hull axis points either left or right wheelConstraint->SetLowLimit(Vector2(-180.0f, 0.0f)); // Let the wheel rotate freely around the axis wheelConstraint->SetHighLimit(Vector2(180.0f, 0.0f)); wheelConstraint->SetDisableCollision(true); // Let the wheel intersect the vehicle hull }