Added more collision shapes and rigidbody driving
This commit is contained in:
17
core/CollisionShape.cpp
Normal file
17
core/CollisionShape.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "CollisionShape.h"
|
||||
|
||||
CollisionShape::CollisionShape() {
|
||||
shape = nullptr;
|
||||
}
|
||||
|
||||
SphereCollisionShape::SphereCollisionShape(cfloat radius) {
|
||||
shape = new btSphereShape(radius);
|
||||
}
|
||||
|
||||
BoxCollisionShape::BoxCollisionShape(cfloat width, cfloat height, cfloat depth) {
|
||||
shape = new btBoxShape(btVector3(width / 2.0f, height / 2.0f, depth / 2.0f));
|
||||
}
|
||||
|
||||
CapsuleCollisionShape::CapsuleCollisionShape(cfloat radius, cfloat height) {
|
||||
shape = new btCapsuleShape(radius, height);
|
||||
}
|
||||
32
core/CollisionShape.h
Normal file
32
core/CollisionShape.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef COLLISIONSHAPE_H
|
||||
#define COLLISIONSHAPE_H
|
||||
|
||||
#include <btBulletDynamicsCommon.h>
|
||||
|
||||
#include "types.h"
|
||||
|
||||
class CollisionShape {
|
||||
public:
|
||||
CollisionShape();
|
||||
protected:
|
||||
btCollisionShape *shape;
|
||||
private:
|
||||
friend class Rigidbody;
|
||||
};
|
||||
|
||||
class SphereCollisionShape : public CollisionShape {
|
||||
public:
|
||||
SphereCollisionShape(cfloat radius);
|
||||
};
|
||||
|
||||
class BoxCollisionShape : public CollisionShape {
|
||||
public:
|
||||
BoxCollisionShape(cfloat width, cfloat height, cfloat depth);
|
||||
};
|
||||
|
||||
class CapsuleCollisionShape: public CollisionShape {
|
||||
public:
|
||||
CapsuleCollisionShape(cfloat radius, cfloat height);
|
||||
};
|
||||
|
||||
#endif /* COLLISIONSHAPE_H */
|
||||
@@ -1,17 +1,5 @@
|
||||
#include "Material.h"
|
||||
|
||||
Color::Color() {
|
||||
this->r = 0.0f;
|
||||
this->g = 0.0f;
|
||||
this->b = 0.0f;
|
||||
}
|
||||
|
||||
Color::Color(cfloat r, cfloat g, cfloat b) {
|
||||
this->r = r;
|
||||
this->g = g;
|
||||
this->b = b;
|
||||
}
|
||||
|
||||
Texture::Texture() {}
|
||||
|
||||
Texture Texture::FromFile(const char *filename) {
|
||||
|
||||
@@ -7,11 +7,7 @@
|
||||
#include "types.h"
|
||||
#include "Util.h"
|
||||
|
||||
struct Color {
|
||||
cfloat r, g, b;
|
||||
Color();
|
||||
Color(cfloat r, cfloat g, cfloat b);
|
||||
};
|
||||
typedef Vector3 Color;
|
||||
|
||||
class Texture {
|
||||
public:
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#include "Rigidbody.h"
|
||||
#include "World.h"
|
||||
|
||||
Rigidbody::Rigidbody() {}
|
||||
Rigidbody::Rigidbody() {
|
||||
collisionShape = new btSphereShape(1.0f);
|
||||
}
|
||||
|
||||
Rigidbody *Rigidbody::Create() {
|
||||
return new Rigidbody;
|
||||
@@ -19,9 +21,9 @@ Rigidbody *Rigidbody::Duplicate() {
|
||||
Rigidbody *Rigidbody::Instance() {
|
||||
Rigidbody *rigidbody = static_cast<Rigidbody*>(Node::Instance());
|
||||
|
||||
rigidbody->collisionShape = new btSphereShape(1.0f);
|
||||
rigidbody->collisionShape = collisionShape;
|
||||
rigidbody->btBody = new btRigidBody(rigidbody->mass, new RigidbodyMotionState(rigidbody), rigidbody->collisionShape);
|
||||
assert(rigidbody);
|
||||
rigidbody->btBody->setAngularFactor(character ? 0.0f : 1.0f);
|
||||
|
||||
World *world = World::GetWorld();
|
||||
world->AddRigidbody(rigidbody);
|
||||
@@ -29,6 +31,26 @@ Rigidbody *Rigidbody::Instance() {
|
||||
return rigidbody;
|
||||
}
|
||||
|
||||
void Rigidbody::SetCollisionShape(CollisionShape collisionShape) {
|
||||
if (this->collisionShape) {
|
||||
delete this->collisionShape;
|
||||
}
|
||||
this->collisionShape = collisionShape.shape;
|
||||
}
|
||||
|
||||
void Rigidbody::ApplyImpulse(Vector3 impulse) {
|
||||
btBody->applyCentralImpulse(btVector3(impulse.x, impulse.y, impulse.z));
|
||||
}
|
||||
|
||||
void Rigidbody::ApplyForce(Vector3 force) {
|
||||
btBody->applyCentralForce(btVector3(force.x, force.y, force.z));
|
||||
}
|
||||
|
||||
void Rigidbody::SetCharacter(bool character) {
|
||||
this->character = character;
|
||||
}
|
||||
|
||||
|
||||
RigidbodyMotionState::RigidbodyMotionState(Rigidbody *rigidbody) {
|
||||
this->rigidbody = rigidbody;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "types.h"
|
||||
#include "Spatial.h"
|
||||
#include "CollisionShape.h"
|
||||
|
||||
class Rigidbody : public Spatial {
|
||||
public:
|
||||
@@ -12,8 +13,16 @@ public:
|
||||
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;
|
||||
friend class World;
|
||||
|
||||
Reference in New Issue
Block a user