Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
526
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.34 KB | None | 0 0
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22.  
  23. #pragma once
  24.  
  25. #include "../IO/VectorBuffer.h"
  26. #include "../Scene/Component.h"
  27.  
  28. #include <Bullet/LinearMath/btMotionState.h>
  29.  
  30. class btCompoundShape;
  31. class btSoftBody;
  32. class RigidBody;
  33.  
  34. namespace Urho3D
  35. {
  36.  
  37. class CollisionShape;
  38. class Constraint;
  39. class PhysicsWorld;
  40. class SmoothedTransform;
  41. class Model;
  42. class VertexBuffer;
  43. class IndexBuffer;
  44. class Drawable;
  45.  
  46. /// Rigid body collision event signaling mode.
  47.  
  48.  
  49. /// Physics rigid body component.
  50. class URHO3D_API SoftBody : public Component, public btMotionState
  51. {
  52.     OBJECT(SoftBody);
  53.  
  54. public:
  55.     /// Construct.
  56.     SoftBody(Context* context);
  57.     /// Destruct. Free the rigid body and geometries.
  58.     virtual ~SoftBody();
  59.     /// Register object factory.
  60.     static void RegisterObject(Context* context);
  61.  
  62.     /// Handle attribute write access.
  63.     virtual void OnSetAttribute(const AttributeInfo& attr, const Variant& src);
  64.     /// Apply attribute changes that can not be applied immediately. Called after scene load or a network update.
  65.     virtual void ApplyAttributes();
  66.     /// Handle enabled/disabled state change.
  67.     virtual void OnSetEnabled();
  68.     /// Return initial world transform to Bullet.
  69.     virtual void getWorldTransform(btTransform& worldTrans) const;
  70.     /// Update world transform from Bullet.
  71.     virtual void setWorldTransform(const btTransform& worldTrans);
  72.     /// Visualize the component as debug geometry.
  73.     virtual void DrawDebugGeometry(DebugRenderer* debug, bool depthTest);
  74.  
  75.     /// Set mass. Zero mass makes the body static.
  76.     void SetMass(float mass);
  77.     /// Set rigid body position in world space.
  78.     void SetPosition(const Vector3& position);
  79.     /// Set rigid body rotation in world space.
  80.     void SetRotation(const Quaternion& rotation);
  81.     /// Set rigid body position and rotation in world space as an atomic operation.
  82.     void SetTransform(const Vector3& position, const Quaternion& rotation);
  83.  
  84.     void SetUseGravity(bool enable);
  85.     /// Set gravity override. If zero, uses physics world's gravity.
  86.     void SetGravityOverride(const Vector3& gravity);
  87.     /// Set rigid body kinematic mode. In kinematic mode forces are not applied to the rigid body.
  88.     void SetCollisionLayer(unsigned layer);
  89.     /// Set collision mask.
  90.     void SetCollisionMask(unsigned mask);
  91.     /// Set collision group and mask.
  92.     void SetCollisionLayerAndMask(unsigned layer, unsigned mask);
  93.     /// Set collision event signaling mode. Default is to signal when rigid bodies are active.
  94.     void SetCollisionEventMode(CollisionEventMode mode);
  95.    
  96.     /// Activate rigid body if it was resting.
  97.     void Activate();
  98.     /// Readd rigid body to the physics world to clean up internal state like stale contacts.
  99.     void ReAddBodyToWorld();
  100.     /// Disable mass update. Call this to optimize performance when adding or editing multiple collision shapes in the same node.
  101.     void DisableMassUpdate();
  102.     /// Re-enable mass update and recalculate the mass/inertia by calling UpdateMass(). Call when collision shape changes are finished.
  103.     void EnableMassUpdate();
  104.  
  105.     /// Return physics world.
  106.     PhysicsWorld* GetPhysicsWorld() const { return physicsWorld_; }
  107.  
  108.     /// Return Bullet soft body.
  109.     btSoftBody* GetBody() const { return body_; }
  110.  
  111.     /// Return mass.
  112.     float GetMass() const { return mass_; }
  113.  
  114.     /// Return rigid body position in world space.
  115.     Vector3 GetPosition() const;
  116.     /// Return rigid body rotation in world space.
  117.     Quaternion GetRotation() const;
  118.  
  119.     /// Return whether rigid body uses gravity.
  120.     bool GetUseGravity() const { return useGravity_; }
  121.  
  122.     /// Return gravity override. If zero (default), uses the physics world's gravity.
  123.     const Vector3& GetGravityOverride() const { return gravityOverride_; }
  124.  
  125.     /// Return center of mass offset.
  126.     const Vector3& GetCenterOfMass() const { return centerOfMass_; }
  127.  
  128.     /// Return whether rigid body is active (not sleeping.)
  129.     bool IsActive() const;
  130.  
  131.     /// Return collision layer.
  132.     unsigned GetCollisionLayer() const { return collisionLayer_; }
  133.  
  134.     /// Return collision mask.
  135.     unsigned GetCollisionMask() const { return collisionMask_; }
  136.  
  137.     /// Apply new world transform after a simulation step. Called internally.
  138.     void ApplyWorldTransform(const Vector3& newWorldPosition, const Quaternion& newWorldRotation);
  139.     /// Update mass and inertia to the Bullet rigid body.
  140.     void UpdateMass();
  141.     /// Update gravity parameters to the Bullet rigid body.
  142.     void UpdateGravity();
  143.  
  144.     void ReleaseBody();
  145.  
  146.     void SetModel(Model* model);
  147.  
  148. protected:
  149.     /// Handle node being assigned.
  150.     virtual void OnNodeSet(Node* node);
  151.     /// Handle scene being assigned.
  152.     virtual void OnSceneSet(Scene* scene);
  153.     /// Handle node transform being dirtied.
  154.     virtual void OnMarkedDirty(Node* node);
  155.  
  156. private:
  157.     /// Create the soft body, or re-add to the physics world with changed flags. Calls UpdateMass().
  158.     void AddBodyToWorld();
  159.     /// Remove the soft body from the physics world.
  160.     void RemoveBodyFromWorld();
  161.  
  162.     /// Bullet soft body.
  163.     btSoftBody* body_;
  164.     /// Physics world.
  165.     WeakPtr<PhysicsWorld> physicsWorld_;
  166.     /// Smoothed transform, if has one.
  167.     WeakPtr<SmoothedTransform> smoothedTransform_;
  168.     /// Gravity override vector.
  169.     Vector3 gravityOverride_;
  170.     /// Center of mass offset.
  171.     Vector3 centerOfMass_;
  172.     /// Mass.
  173.     float mass_;
  174.     /// Attribute buffer for network replication.
  175.     mutable VectorBuffer attrBuffer_;
  176.     /// Collision layer.
  177.     unsigned collisionLayer_;
  178.     /// Collision mask.
  179.     unsigned collisionMask_;
  180.     /// Last interpolated position from the simulation.
  181.     mutable Vector3 lastPosition_;
  182.     /// Last interpolated rotation from the simulation.
  183.     mutable Quaternion lastRotation_;
  184.     /// Use gravity flag.
  185.     bool useGravity_;
  186.     /// Readd body to world flag.
  187.     bool readdBody_;
  188.     /// Body exists in world flag.
  189.     bool inWorld_;
  190.     /// Mass update enable flag.
  191.     bool enableMassUpdate_;
  192.  
  193.     SharedPtr<Model> model_;
  194.     void UpdateSoftBodyFromModel();
  195.     bool CreateBodyFromTriMesh(VertexBuffer* vb, IndexBuffer* ib, bool randomizeConstraints);
  196.     void UpdateDrawable(Drawable* drawable);
  197.     void InitDrawable(Drawable* drawable);
  198. };
  199.  
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement