Browse Source

Make improvements to coordinate system code.

Nicholas Farshidmehr 7 years ago
parent
commit
38e008ea5a
2 changed files with 34 additions and 8 deletions
  1. 24 4
      Source/Urho3D/Physics/RaycastVehicle.cpp
  2. 10 4
      Source/Urho3D/Physics/RaycastVehicle.h

+ 24 - 4
Source/Urho3D/Physics/RaycastVehicle.cpp

@@ -34,6 +34,8 @@
 namespace Urho3D
 {
 
+const IntVector3 RaycastVehicle::DEFAULT_COORDINATE_SYSTEM(0, 1, 2);
+
 struct RaycastVehicleData
 {
     RaycastVehicleData()
@@ -92,10 +94,16 @@ struct RaycastVehicleData
             added_ = true;
         }
 
-        vehicle_->setCoordinateSystem(coordinateSystem.x_, coordinateSystem.y_, coordinateSystem.z_);
+        SetCoordinateSystem(coordinateSystem);
         physWorld_ = pPhysWorld;
     }
 
+    void SetCoordinateSystem(const IntVector3& coordinateSystem)
+    {
+        if (vehicle_)
+            vehicle_->setCoordinateSystem(coordinateSystem.x_, coordinateSystem.y_, coordinateSystem.z_);
+    }
+
     void SetEnabled(bool enabled)
     {
         if (!physWorld_ || !vehicle_)
@@ -129,7 +137,7 @@ RaycastVehicle::RaycastVehicle(Context* context) :
     // fixed update() for inputs and post update() to sync wheels for rendering
     SetUpdateEventMask(USE_FIXEDUPDATE | USE_FIXEDPOSTUPDATE | USE_POSTUPDATE);
     vehicleData_ = new RaycastVehicleData();
-    coordinateSystem_ = IntVector3(0, 1, 2);
+    coordinateSystem_ = DEFAULT_COORDINATE_SYSTEM;
     wheelNodes_.Clear();
     activate_ = false;
     inAirRPM_ = 0.0f;
@@ -175,6 +183,7 @@ void RaycastVehicle::RegisterObject(Context* context)
         .SetMetadata(AttributeMetadata::P_VECTOR_STRUCT_ELEMENTS, wheelElementNames);
     URHO3D_ATTRIBUTE("Maximum side slip threshold", float, maxSideSlipSpeed_, 4.0f, AM_DEFAULT);
     URHO3D_ATTRIBUTE("RPM for wheel motors in air (0=calculate)", float, inAirRPM_, 0.0f, AM_DEFAULT);
+    URHO3D_ATTRIBUTE("Coordinate system", IntVector3, coordinateSystem_, DEFAULT_COORDINATE_SYSTEM, AM_DEFAULT);
 }
 
 void RaycastVehicle::OnSetEnabled()
@@ -262,11 +271,11 @@ void RaycastVehicle::ApplyAttributes()
     URHO3D_LOGDEBUG("loaded wheels: " + String(GetNumWheels()));
 }
 
-void RaycastVehicle::Init(const IntVector3& coordinateSystem)
+void RaycastVehicle::Init()
 {
     hullBody_ = node_->GetOrCreateComponent<RigidBody>();
     Scene* scene = GetScene();
-    vehicleData_->Init(scene, hullBody_, IsEnabledEffective(), coordinateSystem);
+    vehicleData_->Init(scene, hullBody_, IsEnabledEffective(), coordinateSystem_);
 }
 
 void RaycastVehicle::FixedUpdate(float timeStep)
@@ -668,6 +677,17 @@ float RaycastVehicle::GetInAirRPM() const
     return inAirRPM_;
 }
 
+void RaycastVehicle::SetCoordinateSystem(const IntVector3& coordinateSystem)
+{
+    coordinateSystem_ = coordinateSystem;
+    vehicleData_->SetCoordinateSystem(coordinateSystem_);
+}
+
+IntVector3 RaycastVehicle::GetCoordinateSystem()
+{
+    return coordinateSystem_;
+}
+
 void RaycastVehicle::ResetWheels()
 {
     ResetSuspension();

+ 10 - 4
Source/Urho3D/Physics/RaycastVehicle.h

@@ -28,7 +28,6 @@
 
 namespace Urho3D
 {
-
 struct RaycastVehicleData;
 
 class URHO3D_API RaycastVehicle : public LogicComponent
@@ -92,8 +91,10 @@ public:
     void SetWheelSkidInfoCumulative(int wheel, float skid);
     /// Set revolution per minute value for when wheel doesn't touch ground. If set to 0 (or not set), calculated from engine force (probably not what you want).
     void SetInAirRPM(float rpm);
-    /// Init the vehicle component after creation
-    void Init(const IntVector3& coordinateSystem = IntVector3(0, 1, 2));
+    /// Set the coordinate system. The default is (0, 1, 2).
+    void SetCoordinateSystem(const IntVector3& coordinateSystem = DEFAULT_COORDINATE_SYSTEM);
+    /// Init the vehicle component after creation.
+    void Init();
     /// Perform fixed step pre-update.
     void FixedUpdate(float timeStep) override;
     /// Perform fixed step post-update.
@@ -155,12 +156,17 @@ public:
     Vector3 GetContactNormal(int wheel) const;
     /// Get revolution per minute value for when wheel doesn't touch ground.
     float GetInAirRPM() const;
+    /// Get the coordinate system.
+    IntVector3 GetCoordinateSystem();
 
     /// Get wheel data attribute for serialization.
     VariantVector GetWheelDataAttr() const;
     /// Set wheel data attribute during loading.
     void SetWheelDataAttr(const VariantVector& value);
 
+    /// The default coordinate system (0, 1, 2).
+    static const IntVector3 DEFAULT_COORDINATE_SYSTEM;
+
 private:
     /// If the RigidBody should be activated.
     bool activate_;
@@ -168,7 +174,7 @@ private:
     WeakPtr<RigidBody> hullBody_;
     /// Opaque Bullet data hidden from public
     RaycastVehicleData* vehicleData_;
-    /// Coordinate system
+    /// Coordinate system.
     IntVector3 coordinateSystem_;
     /// Nodes of all wheels
     Vector<Node*> wheelNodes_;