-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhysicsComponent.cpp
More file actions
53 lines (44 loc) · 1.81 KB
/
PhysicsComponent.cpp
File metadata and controls
53 lines (44 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include "PhysicsComponent.h"
#include <iostream>
#include "BaseComponentSystem.h"
#include "PhysicsSystem.h"
#include "AGameObject.h"
#include "Vector3D.h"
PhysicsComponent::PhysicsComponent(String name, AGameObject* owner) : AComponent(name, AComponent::ComponentType::Physics, owner)
{
//whenever a new physics component is initialized. Register to physics system
PhysicsSystem* physicsSystem = BaseComponentSystem::getInstance()->getPhysicsSystem();
physicsSystem->registerComponent(this);
PhysicsCommon* physicsCommon = physicsSystem->getPhysicsCommon();
PhysicsWorld* physicsWorld = physicsSystem->getPhysicsWorld();
// Create a rigid body in the world
Vector3D scale = this->getOwner()->getLocalScale();
Transform transform; transform.setFromOpenGL(this->getOwner()->getPhysicsLocalMatrix());
BoxShape* boxShape = physicsCommon->createBoxShape(Vector3(scale.getX() / 2, scale.getY() / 2, scale.getZ() / 2)); //half extent
this->rigidBody = physicsWorld->createRigidBody(transform);
this->rigidBody->addCollider(boxShape, transform);
this->rigidBody->updateMassPropertiesFromColliders();
this->rigidBody->setMass(this->mass);
this->rigidBody->setType(BodyType::DYNAMIC);
transform = this->rigidBody->getTransform();
float matrix[16];
transform.getOpenGLMatrix(matrix);
this->getOwner()->recomputeMatrix(matrix);
}
PhysicsComponent::~PhysicsComponent()
{
BaseComponentSystem::getInstance()->getPhysicsSystem()->unregisterComponent(this);
AComponent::~AComponent();
}
void PhysicsComponent::perform(float deltaTime)
{
const Transform transform = this->rigidBody->getTransform();
float matrix[16];
transform.getOpenGLMatrix(matrix);
this->getOwner()->recomputeMatrix(matrix);
//std::cout << "My component is updating: " << this->name << "\n";
}
RigidBody* PhysicsComponent::getRigidBody()
{
return this->rigidBody;
}