| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- //
- // Copyright (c) 2008-2017 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #pragma once
- #include <Urho3D/Input/Controls.h>
- #include <Urho3D/Physics/PhysicsUtils.h>
- #include <Urho3D/Scene/LogicComponent.h>
- namespace Urho3D
- {
- class Constraint;
- class Node;
- class RigidBody;
- }
- using namespace Urho3D;
- const int CTRL_FORWARD = (1 << 0);
- const int CTRL_BACK = (1 << 1);
- const int CTRL_LEFT = (1 << 2);
- const int CTRL_RIGHT = (1 << 3);
- const int CTRL_BRAKE = (1 << 4);
- const float YAW_SENSITIVITY = 0.1f;
- const float ENGINE_POWER = 10.0f;
- const float MAX_WHEEL_ANGLE = 22.5f;
- // Vehicle component, responsible for physical movement according to controls.
- // Encapsulates RaycastVehicle
- class Vehicle : public LogicComponent
- {
- URHO3D_OBJECT(Vehicle, LogicComponent)
- public :
- /// Construct.
- Vehicle(Context* context);
- /// Destruct.
- ~Vehicle();
- /// Register object factory and attributes.
- static void RegisterObject(Context* context);
- /// Perform post-load after deserialization. Acquire the components from the scene nodes.
- virtual void ApplyAttributes();
- /// Initialize the vehicle. Create rendering and physics components. Called by the application.
- void Init();
- /// Handle physics world update. Called by LogicComponent base class.
- virtual void FixedUpdate(float timeStep);
- /// Updating wheel effects here.
- virtual void PostUpdate(float timeStep);
- /// Movement controls.
- Controls controls_;
- /// Get steering value.
- float GetSteering() { return steering_; }
- /// Set steering value.
- void SetSteering(float steering) { steering_ = steering; }
- /// Get wheel radius.
- float GetWheelRadius() { return wheelRadius_; }
- /// Get wheel width.
- float GetWheelWidth() { return wheelWidth_; }
- private:
- /// Creates particle emitter.
- void CreateEmitter(Vector3 place);
- /// Current left/right steering amount (-1 to 1.)
- float steering_;
- /// Tmp storage for steering
- float vehicleSteering_;
- /// Linear momentum supplied by engine to RigidBody
- float engineForce_;
- /// Rotational momentum preventing (dampening) wheels rotation
- float brakingForce_;
- /// Maximum linear momentum supplied by engine to RigidBody
- float maxEngineForce_;
- /// Stored wheel radius
- float wheelRadius_;
- /// Suspension rest length (in meters)
- float suspensionRestLength_;
- /// Width of wheel (used only in calculation of wheel placement)
- float wheelWidth_;
- /// Suspension stiffness
- float suspensionStiffness_;
- /// Suspension damping
- float suspensionDamping_;
- /// Suspension compression
- float suspensionCompression_;
- /// Wheel friction
- float wheelFriction_;
- /// Wheel roll influence (how much car will turn sidewise)
- float rollInfluence_;
- /// Emitter data for saving.
- Vector<Node*> particleEmitterNodeList_;
- /// Value to calculate acceleration.
- Vector3 prevVelocity_;
- /// Storing points for emitters
- Vector3 connectionPoints_[4];
- /// Do not recreate emitters if they are already created.
- bool emittersCreated;
- };
|