couch/core/Rigidbody.cpp

131 lines
3.4 KiB
C++
Raw Permalink Normal View History

2021-01-27 00:01:17 -06:00
/*
Dane Johnson <dane@danejohnson.org>
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
*/
2021-01-24 22:55:36 -06:00
#include "Rigidbody.h"
#include "World.h"
Rigidbody::Rigidbody() {
collisionShape = new btSphereShape(1.0f);
}
2021-01-24 22:55:36 -06:00
Rigidbody *Rigidbody::Create() {
return new Rigidbody;
}
Rigidbody *Rigidbody::Duplicate() {
Rigidbody *rigidbody = static_cast<Rigidbody*>(Spatial::Duplicate());
rigidbody->collisionShape = collisionShape;
rigidbody->btBody = btBody;
rigidbody->mass = mass;
return rigidbody;
}
Rigidbody *Rigidbody::Instance() {
Rigidbody *rigidbody = static_cast<Rigidbody*>(Node::Instance());
rigidbody->collisionShape = collisionShape;
2021-01-24 22:55:36 -06:00
rigidbody->btBody = new btRigidBody(rigidbody->mass, new RigidbodyMotionState(rigidbody), rigidbody->collisionShape);
rigidbody->btBody->setAngularFactor(character ? 0.0f : 1.0f);
2021-04-21 18:27:30 -05:00
rigidbody->btBody->setUserPointer(rigidbody);
2021-01-24 22:55:36 -06:00
World *world = World::GetWorld();
world->AddRigidbody(rigidbody);
return rigidbody;
}
2021-01-27 00:01:17 -06:00
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;
}
this->collisionShape = collisionShape.shape;
}
float Rigidbody::GetMass() {
2021-01-27 00:01:17 -06:00
return mass;
}
void Rigidbody::SetMass(float mass) {
2021-01-27 00:01:17 -06:00
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));
btBody->setActivationState(1);
}
void Rigidbody::ApplyForce(Vector3 force) {
btBody->applyCentralForce(btVector3(force.x, force.y, force.z));
btBody->setActivationState(1);
}
2021-01-27 00:01:17 -06:00
bool Rigidbody::GetCharacter() {
return character;
}
void Rigidbody::SetCharacter(bool character) {
this->character = character;
}
2021-01-24 22:55:36 -06:00
RigidbodyMotionState::RigidbodyMotionState(Rigidbody *rigidbody) {
this->rigidbody = rigidbody;
}
void RigidbodyMotionState::getWorldTransform(btTransform &worldTrans) const {
2021-01-26 23:28:20 -06:00
Transform transform = rigidbody->GetTransform();
2021-01-24 22:55:36 -06:00
worldTrans.setOrigin(btVector3(
2021-01-26 23:28:20 -06:00
transform.position.x,
transform.position.y,
transform.position.z));
2021-01-24 22:55:36 -06:00
btQuaternion quat;
2021-01-26 23:28:20 -06:00
quat.setEuler(transform.rotation.z,
transform.rotation.y,
transform.rotation.x);
2021-01-24 22:55:36 -06:00
worldTrans.setRotation(quat);
}
void RigidbodyMotionState::setWorldTransform(const btTransform &worldTrans) {
2021-01-26 23:28:20 -06:00
Transform transform = rigidbody->GetTransform();
transform.position = Vector3(worldTrans.getOrigin().getX(),
2021-04-21 18:27:30 -05:00
worldTrans.getOrigin().getY(),
worldTrans.getOrigin().getZ());
2021-01-24 22:55:36 -06:00
2021-01-26 23:28:20 -06:00
worldTrans.getRotation().getEulerZYX(transform.rotation.z,
transform.rotation.y,
transform.rotation.x);
rigidbody->SetTransform(transform);
2021-01-24 22:55:36 -06:00
}