Merge remote-tracking branch 'origin/physics' into water
# Conflicts: # include/Core/Engine.h # include/Physics/PhysicsSystem.h # src/game/Game/PadSystem.cpp # src/game/Physics/PhysicsSystem.cpp
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
#include "PrecompiledHeader.h"
|
||||
#include "Game/BallSystem.h"
|
||||
|
||||
dd::Systems::BallSystem::~BallSystem() {
|
||||
|
||||
}
|
||||
|
||||
void dd::Systems::BallSystem::RegisterComponents(ComponentFactory *cf)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void dd::Systems::BallSystem::Initialize()
|
||||
{
|
||||
|
||||
EVENT_SUBSCRIBE_MEMBER(m_Contact, BallSystem::Contact);
|
||||
}
|
||||
|
||||
void dd::Systems::BallSystem::Update(double dt)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void dd::Systems::BallSystem::UpdateEntity(double dt, EntityID entity, EntityID parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void dd::Systems::BallSystem::OnEntityCommit(EntityID entity)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void dd::Systems::BallSystem::OnEntityRemoved(EntityID entity)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
bool dd::Systems::BallSystem::Contact(const Events::Contact &event)
|
||||
{
|
||||
auto ballComponent1 = m_World->GetComponent<Components::Ball>(event.Entity1);
|
||||
auto ballComponent2 = m_World->GetComponent<Components::Ball>(event.Entity2);
|
||||
|
||||
//Which is the ball?
|
||||
EntityID ballEntity, otherEntitiy;
|
||||
glm::vec2 normal;
|
||||
if (ballComponent1 != nullptr) {
|
||||
ballEntity = event.Entity1;
|
||||
otherEntitiy = event.Entity2;
|
||||
|
||||
//Make normal point from the other entity to the ball
|
||||
normal = event.Normal * -1.f;
|
||||
}
|
||||
else if (ballComponent2 != nullptr) {
|
||||
ballEntity = event.Entity2;
|
||||
otherEntitiy = event.Entity1;
|
||||
|
||||
//Make normal point from the other entity to the ball
|
||||
normal = event.Normal * 1.f;
|
||||
}
|
||||
|
||||
auto ballTransform = m_World->GetComponent<Components::Transform>(ballEntity);
|
||||
auto otherTransform = m_World->GetComponent<Components::Transform>(otherEntitiy);
|
||||
|
||||
glm::vec2 ballVelocity = glm::vec2(ballTransform->Velocity.x, ballTransform->Velocity.y);
|
||||
glm::vec2 reflectedVelocity = ballVelocity - 2.f * glm::dot(ballVelocity, normal)*normal;
|
||||
ballTransform->Velocity = glm::vec3(reflectedVelocity, 0.f);
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
+13
-24
@@ -100,11 +100,11 @@ void dd::Systems::PadSystem::Update(double dt)
|
||||
transform->Velocity.x = pad->MaxSpeed;
|
||||
}
|
||||
transform->Position += transform->Velocity * (float)dt;
|
||||
transform->Velocity += acceleration * (float)dt;
|
||||
transform->Velocity += acceleration * (float)dt;
|
||||
transform->Velocity -= transform->Velocity * pad->SlowdownModifier * (float)dt;
|
||||
|
||||
if (Left()) {
|
||||
acceleration.x = -pad->AccelerationSpeed;
|
||||
|
||||
|
||||
}
|
||||
else if (Right()) {
|
||||
acceleration.x = pad->AccelerationSpeed;
|
||||
@@ -158,6 +158,7 @@ bool dd::Systems::PadSystem::OnKeyUp(const dd::Events::KeyUp &event)
|
||||
|
||||
bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
|
||||
{
|
||||
/*
|
||||
EntityID entityBall = event.Entity2;
|
||||
auto ball = m_World->GetComponent<Components::Ball>(entityBall);
|
||||
if (ball == NULL) {
|
||||
@@ -173,8 +174,8 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
|
||||
|
||||
float movementMultiplier = 4.f;
|
||||
|
||||
float whatX = transformBall->Position.x - transformPad->Position.x;
|
||||
float movementX;
|
||||
//float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
|
||||
//float movementY = glm::cos((abs(movementX) / (3.2f * movementMultiplier)) * 3.14159265359f / 2) * 2.f;
|
||||
if (whatX > 0) {
|
||||
movementX = movementMultiplier * whatX;
|
||||
//std::cout << "Right!" << std::endl;
|
||||
@@ -183,34 +184,22 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
|
||||
//std::cout << "Left!" << std::endl;
|
||||
}
|
||||
|
||||
//float movementY = 1;
|
||||
// std::cout << movementX << " " << movementY << std::endl;
|
||||
|
||||
//float movementX = (event.ContactPoint.x - transformPad->Position.x) * movementMultiplier;
|
||||
float movementY = glm::cos((abs(movementX) / ((1.6f) * movementMultiplier)) * 3.14159265359f / 2) + 1;
|
||||
|
||||
//std::cout << movementX << " " << movementY << std::endl;
|
||||
|
||||
//Temporary solution. Add a new ball and delete the old one.
|
||||
auto ent = m_World->CreateEntity();
|
||||
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
|
||||
transform->Position = glm::vec3(transformBall->Position.x, transformBall->Position.y + 0.01f, -10.f);
|
||||
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
|
||||
auto model = m_World->AddComponent<Components::Model>(ent);
|
||||
model->ModelFile = "Models/Test/Ball/Ballopus.obj";
|
||||
float len = glm::length<float>(transformBall->Velocity);
|
||||
//auto pointlight = m_World->AddComponent<Components::PointLight>(ent);
|
||||
std::shared_ptr<Components::CircleShape> circleShape = m_World->AddComponent<Components::CircleShape>(ent);
|
||||
std::shared_ptr<Components::Ball> cball = m_World->AddComponent<Components::Ball>(ent);
|
||||
std::shared_ptr<Components::Physics> physics = m_World->AddComponent<Components::Physics>(ent);
|
||||
physics->Static = false;
|
||||
|
||||
m_World->RemoveEntity(entityBall);
|
||||
m_World->CommitEntity(ent);
|
||||
transformBall->Velocity += glm::vec3(transformPad->Velocity.x, 0, 0);
|
||||
transformBall->Velocity = glm::normalize(transformBall->Velocity) * len;
|
||||
|
||||
Events::SetImpulse e;
|
||||
e.Entity = ent;
|
||||
e.Impulse = glm::vec2(movementX, movementY);
|
||||
e.Point = glm::vec2(transform->Position.x, transform->Position.y);
|
||||
EventBroker->Publish(e);
|
||||
|
||||
//transform->Velocity = glm::vec3(movementX, movementY, 0.f);
|
||||
*/
|
||||
}
|
||||
|
||||
bool dd::Systems::PadSystem::ResetBall(const dd::Events::ResetBall &event)
|
||||
|
||||
@@ -70,6 +70,9 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
float angle = -glm::eulerAngles(transformComponent->Orientation).z;
|
||||
|
||||
body->SetTransform(position, angle);
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +95,6 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
|
||||
if (! transformComponent)
|
||||
continue;
|
||||
|
||||
if (m_World->GetEntityParent(entity) == 0) {
|
||||
b2Vec2 position = body->GetPosition();
|
||||
transformComponent->Position.x = position.x;
|
||||
@@ -102,6 +104,7 @@ void dd::Systems::PhysicsSystem::Update(double dt)
|
||||
|
||||
//TODO: CHECK IF THIS IS CORRECT
|
||||
transformComponent->Orientation = glm::quat(glm::vec3(0, 0, -angle));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -166,6 +169,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
bodyDef.type = b2_staticBody;
|
||||
} else {
|
||||
bodyDef.type = b2_dynamicBody;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -202,7 +206,7 @@ void dd::Systems::PhysicsSystem::CreateBody(EntityID entity)
|
||||
fixtureDef.shape = pShape;
|
||||
fixtureDef.density = 1.f;
|
||||
fixtureDef.restitution = 1.0f;
|
||||
fixtureDef.friction = 0.3f;
|
||||
fixtureDef.friction = 0.0f;
|
||||
body->CreateFixture(&fixtureDef);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user