Created
July 23, 2014 21:49
-
-
Save lukebitts/1a09b0424be8cb14a8ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <anax/World.hpp> | |
#include <anax/Entity.hpp> | |
#include <anax/Component.hpp> | |
#include <anax/System.hpp> | |
struct BaseShapeComponent : anax::Component<BaseShapeComponent> | |
{ | |
protected: | |
btCollisionShape* _shape = nullptr; | |
BaseShapeComponent() {} | |
virtual ~BaseShapeComponent() | |
{ | |
delete _shape; | |
} | |
public: | |
btCollisionShape* shape() const | |
{ | |
return _shape; | |
} | |
}; | |
struct SphereShapeComponent : BaseShapeComponent, anax::Component<BaseShapeComponent> | |
{ | |
SphereShapeComponent(float radius = 1.f) | |
{ | |
this->_shape = new btSphereShape(radius); | |
} | |
}; | |
struct BoxShapeComponent : BaseShapeComponent, anax::Component<BaseShapeComponent> | |
{ | |
BoxShapeComponent(glm::vec3 halfExtents = glm::vec3(1.f,1.f,1.f)) : BaseShapeComponent(new btBoxShape(btVector3(halfExtents.x,halfExtents.y,halfExtents.z))) {} | |
}; | |
class MotionState : public btMotionState | |
{ | |
private: | |
anax::Entity _e; | |
btTransform _initial_position; | |
public: | |
MotionState(const btTransform &initialPosition, anax::Entity e) | |
{ | |
_e = e; | |
_initial_position = initialPosition; | |
} | |
virtual ~MyMotionState() | |
{ | |
} | |
void setNode(anax::Entity e) | |
{ | |
_e = e; | |
} | |
virtual void getWorldTransform(btTransform &worldTrans) const override | |
{ | |
worldTrans = initial_position; | |
} | |
virtual void setWorldTransform(const btTransform &worldTrans) override | |
{ | |
if(!_e.isValid()) | |
return; | |
btQuaternion rot = worldTrans.getRotation(); | |
_e.getComponent<SpatialComponent>().rotation = glm::vec3(rot.x(), rot.y(), rot.z(), rot.w()); | |
btVector3 pos = worldTrans.getOrigin(); | |
_e.getComponent<SpatialComponent>().position = glm::vec3(pos.x(), pos.y(), pos.z()); | |
} | |
}; | |
struct RigidBodyComponent : anax::Component<RigidBodyComponent> | |
{ | |
std::unique_ptr<MotionState> motion_state = nullptr; | |
std::unique_ptr<btRigidBody> rigid_body = nullptr; | |
btDiscreteWorld* world = nullptr; | |
float mass = 0.f; | |
~RigidBodyComponent() | |
{ | |
world->removeRigidBody(rigid_body.get()); | |
} | |
}; | |
class BulletSystem : public anax::System<BulletSystem> | |
{ | |
private: | |
btDbvtBroadphase _broadphase{}; | |
btDefaultCollisionConfiguration _collision_configuration{}; | |
btCollisionDispatcher _collision_dispatcher{}; | |
btSequentialImpulseConstraintSolver _solver{}; | |
btDiscreteDynamicsWorld _world | |
public: | |
BulletSystem() : Base(anax::ComponentFilter().requires<BaseShapeComponent,RigidBodyComponent>()), | |
_world(&dispatcher, &broadphase, &solver, &collisionConfiguration) | |
{ | |
_world.setGravity(btVector3(0,-10,0)); | |
} | |
virtual void onEntityAdded(anax::Entity& e) override | |
{ | |
SpatialComponent& e_spatial = e.getComponent<SpatialComponent>(); | |
RigidBodyComponent& e_rigidbody = e.getComponent<RigidBodyComponent>(); | |
BaseShapeComponent& e_shape = e.getComponent<BaseShapeComponent>(); | |
e_rigidbody.world = &_world; | |
e_rigidbody.motion_state = new MotionState( | |
btTransform(btQuaternion(e_spatial.rotation.x,e_spatial.rotation.y,e_spatial.rotation.z,e_spatial.rotation.w), | |
btVector3(e_spatial.position.x,e_spatial.position.y,e_spatial.position.z)), e); | |
btRigidBody::btRigidBodyConstructionInfo info{e_rigidbody.mass, e_rigidbody.motion_state, e_shape.shape(), btVector3(0.f,0.f,0.f)}; | |
e_rigidbody.rigid_body = new btRigidBody(info); | |
_world.addRigidBody(e_rigidbody.rigid_body); | |
} | |
virtual void onEntityRemoved(anax::Entity& e) override | |
{ | |
} | |
}; | |
int main() | |
{ | |
anax::World w; | |
BulletSystem bs; | |
w.addSystem<BulletSystem>(bs); | |
auto e1 = w.createEntity(); | |
e1.addComponent<SphereShapeComponent>(); | |
e1.addComponent<RigidBodyComponent>(); | |
e1.activate(); | |
e1.getComponent<BaseShapeComponent>().test(); | |
std::cout<<e1.getComponent<SphereShapeComponent>().l<<"\n"; | |
w.refresh(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment