From 1bf1486e0a36a198bb294a2959b9bb917cd52af2 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Wed, 27 Jan 2021 00:01:17 -0600 Subject: [PATCH] Refactor and document Rigidbody --- core/Rigidbody.cpp | 46 +++++++++++++++++++++++++ core/Rigidbody.h | 86 +++++++++++++++++++++++++++++++++++++++++----- core/Spatial.h | 2 +- demo/main.lua | 4 +-- 4 files changed, 127 insertions(+), 11 deletions(-) diff --git a/core/Rigidbody.cpp b/core/Rigidbody.cpp index 16ac284..b1ad3d0 100644 --- a/core/Rigidbody.cpp +++ b/core/Rigidbody.cpp @@ -1,3 +1,24 @@ +/* + Dane Johnson + + LICENSE + + Couch Copyright (C) 2021 Dane Johnson + + This program comes with ABSOLUTELY NO WARRANTY; without event the + implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for details at + https://www.gnu.org/licenses/gpl-3.0.html + + This is free software, and you are welcome to redistribute it + under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + DESCRIPTION + + Defines physics bodies and their interfaces with the physics engine +*/ #include "Rigidbody.h" #include "World.h" @@ -31,6 +52,15 @@ Rigidbody *Rigidbody::Instance() { return rigidbody; } +void Rigidbody::Translate(Vector3 offset) { + Spatial::Translate(offset); + if (not IsPrefab()) { + btBody->translate(btVector3(offset.x, + offset.y, + offset.z)); + } +} + void Rigidbody::SetCollisionShape(CollisionShape collisionShape) { if (this->collisionShape) { delete this->collisionShape; @@ -38,6 +68,17 @@ void Rigidbody::SetCollisionShape(CollisionShape collisionShape) { this->collisionShape = collisionShape.shape; } +cfloat Rigidbody::GetMass() { + return mass; +} + +void Rigidbody::SetMass(cfloat mass) { + this->mass = mass; + if (not IsPrefab()) { + btBody->setMassProps(mass, btVector3(0, 0, 0)); + } +} + void Rigidbody::ApplyImpulse(Vector3 impulse) { btBody->applyCentralImpulse(btVector3(impulse.x, impulse.y, impulse.z)); } @@ -46,6 +87,11 @@ void Rigidbody::ApplyForce(Vector3 force) { btBody->applyCentralForce(btVector3(force.x, force.y, force.z)); } + +bool Rigidbody::GetCharacter() { + return character; +} + void Rigidbody::SetCharacter(bool character) { this->character = character; } diff --git a/core/Rigidbody.h b/core/Rigidbody.h index 99f5618..99a3ece 100644 --- a/core/Rigidbody.h +++ b/core/Rigidbody.h @@ -1,3 +1,25 @@ +/** + @file + @author Dane Johnson + + @section LICENSE + + Couch Copyright (C) 2021 Dane Johnson + + This program comes with ABSOLUTELY NO WARRANTY; without event the + implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU General Public License for details at + https://www.gnu.org/licenses/gpl-3.0.html + + This is free software, and you are welcome to redistribute it + under the terms of the GNU General Public License as published + by the Free Software Foundation; either version 3 of the License, + or (at your option) any later version. + + @section DESCRIPTION + + Defines physics bodies and their interfaces with the physics engine +*/ #ifndef RIGIDBODY_H #define RIGIDBODY_H @@ -7,24 +29,72 @@ #include "Spatial.h" #include "CollisionShape.h" +/** + Rigidbodies are any body that interacts with the physics bodies +*/ class Rigidbody : public Spatial { public: Rigidbody(); + + /** + Directly translate this physics object. + @param offset The desired translation offset. + */ + virtual void Translate(Vector3 offset); + + + /** + Sets the collision shape of this rigidbody + @param collisionShape The collision shape + */ + void SetCollisionShape(CollisionShape collisionShape); + /** + Gets the mass of this rigidbody + @return The mass, 0.0 if this is an infinite mass object + */ + cfloat GetMass(); + /** + Sets the mass of this rigidbody. + Note that setting mass equal to 0.0 will make it an + infinite mass object + @param mass The desired mass + */ + void SetMass(cfloat mass); + + /** + Add an instantaneous impulse to the object. + Probably shouldn't be called every update. + @param impulse The impulse vector + */ + void ApplyImpulse(Vector3 impulse); + /** + Add a force to this object, scaled to the time step. + Can be called every update. + @param The (unscaled) force + */ + void ApplyForce(Vector3 force); + + /** + If this is a character object, meaning it's rotation is not + controlled by the physics engine + @return true if this is a character object + */ + bool GetCharacter(); + /** + Sets if this object's rotation is controlled by the physics engine + @param character true if this is a character object + */ + void SetCharacter(bool character); + virtual Rigidbody *Create(); virtual Rigidbody *Duplicate(); virtual Rigidbody *Instance(); - - void SetCollisionShape(CollisionShape collisionShape); - cfloat mass = 1.0f; - - void ApplyImpulse(Vector3 impulse); - void ApplyForce(Vector3 force); - - void SetCharacter(bool character); private: bool character = false; btRigidBody *btBody; btCollisionShape *collisionShape; + cfloat mass = 1.0f; + friend class World; }; diff --git a/core/Spatial.h b/core/Spatial.h index cd47382..30ff9e7 100644 --- a/core/Spatial.h +++ b/core/Spatial.h @@ -51,7 +51,7 @@ public: Directly translates the spatial by offset @param offset The offset of the transform operation */ - void Translate(Vector3 offset); + virtual void Translate(Vector3 offset); /** Rotates the Camera phi radians about the X axis @param phi The amount to rotate in radians diff --git a/demo/main.lua b/demo/main.lua index 2742e51..6a1cdc9 100644 --- a/demo/main.lua +++ b/demo/main.lua @@ -71,7 +71,7 @@ function init() material.specular = WHITE * 0.1 character_prefab:SetMaterial(0, material) local character_body = couch.Rigidbody() - character_body.mass = 1.0 + character_body:SetMass(1.0) character_body:SetCollisionShape(couch.CapsuleCollisionShape(1.0, 1.0)) character_body:SetCharacter(true) character_body:AddChild(character_prefab) @@ -202,7 +202,7 @@ function make_ground() -- Add a collisionshape local ground_shape_prefab = couch.Rigidbody() - ground_shape_prefab.mass = 0.0 + ground_shape_prefab:SetMass(0.0) ground_shape_prefab:SetCollisionShape(couch.BoxCollisionShape(180.0, 1.0, 180.0)) ground_shape_prefab:Translate(couch.Vector3(0.0, -2.5, 0.0)) ground:AddChild(ground_shape_prefab:Instance())