Collisions kind of working

This commit is contained in:
viktorljung
2015-09-17 14:11:09 +01:00
parent 8963e94cd6
commit 442554812d
15 changed files with 47010 additions and 15 deletions
+14
View File
@@ -0,0 +1,14 @@
# Blender MTL File: 'Brick.blend'
# Material Count: 1
newmtl Brick
Ns 17.647059
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.154050 0.039202
Ks 0.000000 0.000000 0.000000
Ni 1.000000
d 1.000000
illum 1
map_Kd Brick_d.jpg
map_Bump Brick_n.jpg
map_Ks Brick_s.jpg
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1010 KiB

+14
View File
@@ -0,0 +1,14 @@
# Blender MTL File: 'None'
# Material Count: 1
newmtl Material.001
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.500000 0.500000 0.500000
Ni 1.000000
d 1.000000
illum 2
map_Kd Test/Brick_Diffuse.png
map_Bump Test/Brick_Normal.png
map_Ks Test/Brick_Specular.png
+124
View File
@@ -0,0 +1,124 @@
# Blender v2.75 (sub 0) OBJ File: ''
# www.blender.org
mtllib Halfpipe.mtl
o Cylinder
v 0.000000 -2.908792 -1.000000
v 0.000000 2.908792 -1.000000
v 0.195090 -2.908792 -0.980785
v 0.195090 2.908792 -0.980785
v 0.382683 -2.908792 -0.923880
v 0.382683 2.908792 -0.923880
v 0.555570 -2.908792 -0.831470
v 0.555570 2.908792 -0.831470
v 0.707107 -2.908792 -0.707107
v 0.707107 2.908792 -0.707107
v 0.831470 -2.908792 -0.555570
v 0.831470 2.908792 -0.555570
v 0.923880 -2.908792 -0.382683
v 0.923880 2.908792 -0.382683
v 0.980785 -2.908792 -0.195090
v 0.980785 2.908792 -0.195090
v 1.000000 -2.908792 -0.000000
v 1.000000 2.908792 -0.000000
v -1.000000 -2.908792 -0.000001
v -1.000000 2.908792 -0.000001
v -0.980785 -2.908792 -0.195091
v -0.980785 2.908792 -0.195091
v -0.923879 -2.908792 -0.382684
v -0.923879 2.908792 -0.382684
v -0.831469 -2.908792 -0.555571
v -0.831469 2.908792 -0.555571
v -0.707106 -2.908792 -0.707108
v -0.707106 2.908792 -0.707108
v -0.555569 -2.908792 -0.831470
v -0.555569 2.908792 -0.831470
v -0.382682 -2.908792 -0.923880
v -0.382682 2.908792 -0.923880
v -0.195089 -2.908792 -0.980786
v -0.195089 2.908792 -0.980786
vt -1.156714 -1.419990
vt -1.158346 2.336063
vt -1.031779 2.336118
vt -1.283288 -1.420045
vt -1.284913 2.336008
vt -1.409862 -1.420100
vt -1.411480 2.335954
vt -1.536436 -1.420155
vt -1.538048 2.335899
vt 2.387354 -1.418355
vt 2.385534 2.337692
vt 2.512101 2.337753
vt 2.260782 -1.418416
vt 2.258967 2.337631
vt 2.134208 -1.418478
vt 2.132400 2.337570
vt 2.007634 -1.418538
vt 2.005832 2.337510
vt -0.144122 -1.419541
vt -0.145809 2.336510
vt -0.019241 2.336567
vt -0.270696 -1.419598
vt -0.272376 2.336454
vt -0.397270 -1.419654
vt -0.398943 2.336397
vt -0.523844 -1.419711
vt -0.525511 2.336341
vt -0.650418 -1.419767
vt -0.652078 2.336285
vt -0.776992 -1.419823
vt -0.778645 2.336229
vt -1.030140 -1.419935
vt -0.905212 2.336174
vt -0.903566 -1.419879
vt 2.513929 -1.418293
vt -0.017548 -1.419484
vn -0.098000 0.000000 0.995200
vn -0.290300 0.000000 0.956900
vn -0.471400 0.000000 0.881900
vn -0.634400 0.000000 0.773000
vn -0.773000 0.000000 0.634400
vn -0.881900 0.000000 0.471400
vn -0.956900 0.000000 0.290300
vn -0.995200 0.000000 0.098000
vn 0.995200 0.000000 0.098000
vn 0.956900 0.000000 0.290300
vn 0.881900 0.000000 0.471400
vn 0.773000 0.000000 0.634400
vn 0.634400 0.000000 0.773000
vn 0.471400 0.000000 0.881900
vn 0.098000 0.000000 0.995200
vn 0.290300 0.000000 0.956900
usemtl Material.001
s off
f 3/1/1 4/2/1 2/3/1
f 5/4/2 6/5/2 4/2/2
f 7/6/3 8/7/3 6/5/3
f 9/8/4 10/9/4 8/7/4
f 11/10/5 12/11/5 10/12/5
f 13/13/6 14/14/6 12/11/6
f 15/15/7 16/16/7 14/14/7
f 17/17/8 18/18/8 16/16/8
f 21/19/9 22/20/9 20/21/9
f 23/22/10 24/23/10 22/20/10
f 25/24/11 26/25/11 24/23/11
f 27/26/12 28/27/12 26/25/12
f 29/28/13 30/29/13 28/27/13
f 31/30/14 32/31/14 30/29/14
f 1/32/15 2/3/15 34/33/15
f 33/34/16 34/33/16 32/31/16
f 1/32/1 3/1/1 2/3/1
f 3/1/2 5/4/2 4/2/2
f 5/4/3 7/6/3 6/5/3
f 7/6/4 9/8/4 8/7/4
f 9/35/5 11/10/5 10/12/5
f 11/10/6 13/13/6 12/11/6
f 13/13/7 15/15/7 14/14/7
f 15/15/8 17/17/8 16/16/8
f 19/36/9 21/19/9 20/21/9
f 21/19/10 23/22/10 22/20/10
f 23/22/11 25/24/11 24/23/11
f 25/24/12 27/26/12 26/25/12
f 27/26/13 29/28/13 28/27/13
f 29/28/14 31/30/14 30/29/14
f 33/34/15 1/32/15 34/33/15
f 31/30/16 33/34/16 32/31/16
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 MiB

+5 -1
View File
@@ -41,12 +41,14 @@
#include "Game/CBall.h"
#include "Game/CBrick.h"
#include "Game/CPad.h"
#include "Game/BallSystem.h"
#include "Physics/PhysicsSystem.h"
#include "Physics/CPhysics.h"
#include "Physics/CBoxShape.h"
#include "Physics/ESetImpulse.h"
namespace dd
{
@@ -89,6 +91,8 @@ public:
m_World->AddSystem<Systems::LevelSystem>();
m_World->SystemFactory.Register<Systems::PadSystem>([this]() { return new Systems::PadSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::PadSystem>();
m_World->SystemFactory.Register<Systems::BallSystem>([this]() { return new Systems::BallSystem(m_World.get(), m_EventBroker); });
m_World->AddSystem<Systems::BallSystem>();
m_World->ComponentFactory.Register<Components::Model>();
m_World->ComponentFactory.Register<Components::Template>();
@@ -114,7 +118,7 @@ public:
std::shared_ptr<Components::Transform> transform = m_World->AddComponent<Components::Transform>(ent);
transform->Position = glm::vec3(0.5f, 0.f, -10.f);
transform->Scale = glm::vec3(1.f, 1.f, 1.f);
transform->Velocity = glm::vec3(0.0f, -5.f, 0.f);
transform->Velocity = glm::vec3(1.0f, -5.f, 0.f);
std::shared_ptr<Components::Sprite> sprite = m_World->AddComponent<Components::Sprite>(ent);
sprite->SpriteFile = "Textures/Ball.png";
+48
View File
@@ -0,0 +1,48 @@
#ifndef DAYDREAM_BALLSYSTEM_H
#define DAYDREAM_BALLSYSTEM_H
#include "Core/System.h"
#include "Core/World.h"
#include "Core/CTransform.h"
#include "Physics/EContact.h"
#include "Core/EventBroker.h"
#include "Game/CBall.h"
namespace dd {
namespace Systems {
class BallSystem : public System {
public:
BallSystem(World *world, std::shared_ptr<dd::EventBroker> eventBroker)
: System(world, eventBroker) { }
~BallSystem();
EventRelay<BallSystem, Events::Contact> m_Contact;
bool Contact(const Events::Contact &event);
void RegisterComponents(ComponentFactory *cf) override;
void Initialize() override;
// Called once per system every tick
void Update(double dt) override;
// Called once for every entity in the world every tick
void UpdateEntity(double dt, EntityID entity, EntityID parent) override;
// Called when components are committed to an entity
void OnEntityCommit(EntityID entity) override;
// Called when an entity is removed
void OnEntityRemoved(EntityID entity) override;
private:
};
}
}
#endif
+1 -1
View File
@@ -19,7 +19,7 @@ struct Contact : Event
{
EntityID Entity1;
EntityID Entity2;
glm::vec2 ContactPoint;
glm::vec2 Normal;
};
}
+25 -8
View File
@@ -14,6 +14,7 @@
#include "Physics/ESetImpulse.h"
#include "Physics/CCircleShape.h"
#include "Core/EventBroker.h"
#include "Game/CPad.h"
namespace dd
@@ -22,7 +23,6 @@ namespace dd
namespace Systems
{
//KOLLA http://box2d.org/manual.pdf p52 Pre-Solve Event
class PhysicsSystem : public System
{
@@ -83,23 +83,40 @@ private:
void BeginContact(b2Contact* contact)
{
b2Vec2 contactPoint = contact->GetManifold()->localPoint;
b2WorldManifold worldManifold;
contact->GetWorldManifold(&worldManifold);
Events::Contact e;
e.ContactPoint = glm::vec2(contactPoint.x, contactPoint.y);
e.Entity1 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
e.Entity2 = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureB()->GetBody()];
e.Normal = glm::normalize(glm::vec2(worldManifold.normal.x, worldManifold.normal.y));
m_PhysicsSystem->EventBroker->Publish(e);
LOG_INFO("Entity1 = %i, Entity2 = %i\n", e.Entity1, e.Entity2);
}
void EndContact(b2Contact* contact)
{ /* handle end event */ }
{
}
void PreSolve(b2Contact* contact, const b2Manifold* oldManifold)
{ /* handle pre-solve event */}
{
EntityID entityA = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
EntityID entityB = m_PhysicsSystem->m_BodiesToEntities[contact->GetFixtureA()->GetBody()];
auto physicsComponentA = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityA);
auto physicsComponentB = m_PhysicsSystem->m_World->GetComponent<Components::Physics>(entityB);
if (physicsComponentA != nullptr || physicsComponentB != nullptr) {
// Turn of collisions
contact->SetEnabled(false);
}
}
void PostSolve(b2Contact* contact, const b2ContactImpulse* impulse)
{ /* handle post-solve event */
contact->GetFixtureA()->GetBody()->SetAngularVelocity(0.f);
contact->GetFixtureB()->GetBody()->SetAngularVelocity(0.f);
{
}
private:
+72
View File
@@ -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;
}
+4 -3
View File
@@ -51,9 +51,8 @@ void dd::Systems::PadSystem::Update(double dt)
transform->Position += transform->Velocity * (float)dt;
transform->Velocity += acceleration * (float)dt;
transform->Velocity -= transform->Velocity * (0.9f * (float)dt);
transform->Position.y = -5.f;
//transform->Orientation = glm::quat();
std::cout << transform->Velocity.x << ", " << transform->Velocity.y << std::endl;
if (left)
{
@@ -121,6 +120,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)
@@ -150,6 +150,7 @@ bool dd::Systems::PadSystem::OnContact(const dd::Events::Contact &event)
//transform->Velocity = glm::vec3(movementX, movementY, 0.f);
*/
}
bool dd::Systems::PadSystem::PadSteeringInputController::OnCommand(const Events::InputCommand &event)
-2
View File
@@ -93,8 +93,6 @@ void dd::Systems::PhysicsSystem::Update(double dt)
auto transformComponent = m_World->GetComponent<Components::Transform>(entity);
if (m_World->GetEntityParent(entity) == 0) {
b2Vec2 position = body->GetPosition();
transformComponent->Position.x = position.x;