Skip to content
This repository
  • 7 commits
  • 28 files changed
  • 0 comments
  • 1 contributor
2  .gitignore
@@ -14,5 +14,5 @@ Makefile
14 14
 *~
15 15
 #*#
16 16
 __hidden__
17  
-
  17
+*.mat
18 18
 
2  .gitmodules
... ...
@@ -1,3 +1,3 @@
1 1
 [submodule "yaac"]
2 2
 	path = yaac
3  
-	url = ./yaac
  3
+	url = git://github.com/deek0146/yaac.git
4  AbstractFactory/Factories/LevelGeometryFactory.cpp
@@ -6,7 +6,8 @@
6 6
 #include <Physics/PhysicsManager.h>
7 7
 #include <Physics/BodyParts/BodyPart.h>
8 8
 #include <AbstractFactory/FactoryParameters.h>
9  
-#include <Entities/CollisionDatabase.h>
  9
+#include <Entities/CollisionDatabase.h>
  10
+#include <Physics/WaterPhysicsSystem.h>
10 11
 
11 12
 LevelGeometryFactory::LevelGeometryFactory()
12 13
 {
@@ -22,7 +23,6 @@ void LevelGeometryFactory::init(FactoryLoader* loader, AbstractFactories* factor
22 23
 {
23 24
     skinFactory = loader->getFactory<Skin>("skin", "ConvexPolygonSkinFactory");
24 25
     bodyFactory = loader->getFactory<BodyPart>("body", "b2FixtureBodyPartFactory");
25  
-
26 26
 }
27 27
 
28 28
 Entity* LevelGeometryFactory::useFactory(FactoryParameters* _parameters)
2  AbstractFactory/Factories/LevelGeometryFactory.h
@@ -22,8 +22,6 @@ class LevelGeometryFactory : public AbstractFactory<Entity, LevelGeometryFactory
22 22
         }
23 23
     protected:
24 24
     private:
25  
-        AbstractFactoryBase<CollisionResponse>* collisionResponse;
26  
-
27 25
         AbstractFactoryBase<Skin>* skinFactory;
28 26
         AbstractFactoryBase<BodyPart>* bodyFactory;
29 27
 };
37  AbstractFactory/Factories/WaterVolumeFactory.cpp
... ...
@@ -0,0 +1,37 @@
  1
+#include "WaterVolumeFactory.h"
  2
+#include <Entities/WaterVolume.h>
  3
+#include <Physics/PhysicsManager.h>
  4
+#include <Physics/WaterPhysicsSystem.h>
  5
+
  6
+WaterVolumeFactory::WaterVolumeFactory()
  7
+{
  8
+    //ctor
  9
+    system = nullptr;
  10
+}
  11
+
  12
+WaterVolumeFactory::~WaterVolumeFactory()
  13
+{
  14
+    //dtor
  15
+}
  16
+
  17
+void WaterVolumeFactory::init(FactoryLoader* loader, AbstractFactories* factories)
  18
+{
  19
+    skinFactory = loader->getFactory<Skin>("skin", "ConvexPolygonSkinFactory");
  20
+    bodyFactory = loader->getFactory<BodyPart>("body", "b2BodyBodyPartFactory");
  21
+    system = factories->getWorld()->getChildOfType<WaterPhysicsSystem>();
  22
+}
  23
+
  24
+Entity* WaterVolumeFactory::useFactory(FactoryParameters* _parameters)
  25
+{
  26
+    //g_AIManager.addStaticGeometry(&points[0],points.size());
  27
+    WaterVolume* entity = new WaterVolume(system);
  28
+    Vec2f waterPoint = _parameters->get<Vec2f>("point", {0.0f,0.0f});
  29
+    waterPoint.y = 2;
  30
+    std::vector<Vec2f> points = system->getContainer(waterPoint);
  31
+    _parameters->addArray<Vec2f>("points", points);
  32
+    entity->baseInit(skinFactory->use(_parameters, entity));
  33
+    BodyPart* body = bodyFactory->use(_parameters, entity);
  34
+    entity->setRootBody(body);
  35
+
  36
+    return entity;
  37
+}
28  AbstractFactory/Factories/WaterVolumeFactory.h
... ...
@@ -0,0 +1,28 @@
  1
+#ifndef WATERVOLUMEFACTORY_H
  2
+#define WATERVOLUMEFACTORY_H
  3
+
  4
+#include <AbstractFactory/AbstractFactory.h>
  5
+#include <Entities/Entity.h>
  6
+#include <Graphics/Skins/Skin.h>
  7
+#include <Physics/BodyParts/BodyPart.h>
  8
+class WaterPhysicsSystem;
  9
+
  10
+class WaterVolumeFactory : public AbstractFactory<Entity, WaterVolumeFactory>
  11
+{
  12
+    public:
  13
+        WaterVolumeFactory();
  14
+        virtual ~WaterVolumeFactory();
  15
+        void init(FactoryLoader* loader, AbstractFactories* factories);
  16
+        Entity* useFactory(FactoryParameters* _parameters);
  17
+        static std::string name()
  18
+        {
  19
+            return "WaterVolumeFactory";
  20
+        }
  21
+    protected:
  22
+    private:
  23
+        AbstractFactoryBase<Skin>* skinFactory;
  24
+        AbstractFactoryBase<BodyPart>* bodyFactory;
  25
+        WaterPhysicsSystem* system;
  26
+};
  27
+
  28
+#endif // WATERVOLUMEFACTORY_H
11  AbstractFactory/FactoryParameters.h
@@ -15,7 +15,10 @@ class FactoryParameters
15 15
         ~FactoryParameters();
16 16
 
17 17
         template <typename Type>
18  
-        void add(const std::string& name, const Type& value);
  18
+        void add(const std::string& _name, const Type& _value);
  19
+
  20
+        template <typename Type>
  21
+        void addArray(const std::string& _name, const std::vector<Type>& _value);
19 22
 
20 23
         template <typename Type>
21 24
         Type get(const std::string& name, const Type& _default);
@@ -53,6 +56,12 @@ void FactoryParameters::add(const std::string& name, const Type& value)
53 56
 }
54 57
 
55 58
 template <typename Type>
  59
+void FactoryParameters::addArray(const std::string& _name, const std::vector<Type>& _value)
  60
+{
  61
+    table.addArray(_name, _value);
  62
+}
  63
+
  64
+template <typename Type>
56 65
 Type FactoryParameters::get(const std::string& name, const Type& _default)
57 66
 {
58 67
     return table.getValue(name, _default);
12  Entities/Entity.cpp
@@ -43,15 +43,11 @@ const Vec2f& Entity::getPosition()
43 43
 {
44 44
     return rootBody->getPosition();
45 45
 }
46  
-
47  
-void Entity::registerActions(GameObjectType* _type)
  46
+void Entity::floatationAction(CollisionObject* _object)
48 47
 {
49  
-    type.createActionHandle("buoyancy", &Entity::floatationAction);
  48
+    assert(false);
50 49
 }
51  
-
52  
-void Entity::floatationAction(CollisionObject* _object)
  50
+void Entity::registerActions(GameObjectType* _type)
53 51
 {
54  
-    BodyPart* other = _object->getOther();
55  
-    other->applyLinearImpulse(other->getLinearVelocity()*-0.1f);
56  
-    other->applyLinearImpulse(Vec2f(0, other->getAreaBelowLine(18.5f) * -1.0f * other->getDensity()));
  52
+    type.createActionHandle("buoyancy", &Entity::floatationAction);
57 53
 }
2  Entities/Entity.h
@@ -23,12 +23,12 @@ class Entity : public GameObject<Entity>
23 23
         BodyPart* getRootBody();
24 24
 
25 25
         static void registerActions(GameObjectType* _type);
26  
-        void floatationAction(CollisionObject* _object);
27 26
 
28 27
         static std::string name()
29 28
         {
30 29
             return "Entity";
31 30
         }
  31
+        virtual void floatationAction(CollisionObject* _object); /// FIXME
32 32
     protected:
33 33
         BodyPart* rootBody;
34 34
         Skin* mSkin;
31  Entities/WaterVolume.cpp
... ...
@@ -0,0 +1,31 @@
  1
+#include "WaterVolume.h"
  2
+#include <Physics/BodyParts/BodyPart.h>
  3
+#include <Physics/CollisionObject.h>
  4
+#include <Types/Vec2f.h>
  5
+
  6
+WaterVolume::WaterVolume(WaterPhysicsSystem* _system)
  7
+{
  8
+    //ctor
  9
+    system = _system;
  10
+}
  11
+
  12
+WaterVolume::~WaterVolume()
  13
+{
  14
+    //dtor
  15
+}
  16
+
  17
+void WaterVolume::update()
  18
+{
  19
+}
  20
+
  21
+/*void WaterPhysicsSystem::registerActions(GameObjectType* _type)
  22
+{
  23
+    type.createActionHandle("buoyancy", &WaterPhysicsSystem::floatationAction);
  24
+}*/
  25
+
  26
+void WaterVolume::floatationAction(CollisionObject* _object)
  27
+{
  28
+    BodyPart* other = _object->getOther();
  29
+    other->applyLinearImpulse(other->getLinearVelocity()*-0.1f);
  30
+    other->applyLinearImpulse(Vec2f(0, other->getAreaBelowLine(18.5f) * -1.0f * other->getDensity()));
  31
+}
19  Entities/WaterVolume.h
... ...
@@ -0,0 +1,19 @@
  1
+#ifndef WATERVOLUME_H
  2
+#define WATERVOLUME_H
  3
+
  4
+#include <Entities/Entity.h>
  5
+class WaterPhysicsSystem;
  6
+
  7
+class WaterVolume : public Entity
  8
+{
  9
+    public:
  10
+        WaterVolume(WaterPhysicsSystem* _system);
  11
+        virtual ~WaterVolume();
  12
+        void update();
  13
+        void floatationAction(CollisionObject* _object);
  14
+    protected:
  15
+    private:
  16
+        WaterPhysicsSystem* system;
  17
+};
  18
+
  19
+#endif // WATERVOLUME_H
6  Framework2D.cbp
@@ -216,6 +216,8 @@
216 216
 		<Unit filename="AbstractFactory/Factories/TileFactory.h" />
217 217
 		<Unit filename="AbstractFactory/Factories/TileMapFactory.cpp" />
218 218
 		<Unit filename="AbstractFactory/Factories/TileMapFactory.h" />
  219
+		<Unit filename="AbstractFactory/Factories/WaterVolumeFactory.cpp" />
  220
+		<Unit filename="AbstractFactory/Factories/WaterVolumeFactory.h" />
219 221
 		<Unit filename="AbstractFactory/FactoryLoader.cpp" />
220 222
 		<Unit filename="AbstractFactory/FactoryLoader.h" />
221 223
 		<Unit filename="AbstractFactory/FactoryLoaders/CppFactoryLoader.cpp" />
@@ -266,6 +268,8 @@
266 268
 		<Unit filename="Entities/Tile.h" />
267 269
 		<Unit filename="Entities/TileMap.cpp" />
268 270
 		<Unit filename="Entities/TileMap.h" />
  271
+		<Unit filename="Entities/WaterVolume.cpp" />
  272
+		<Unit filename="Entities/WaterVolume.h" />
269 273
 		<Unit filename="Entities/Weapons/FireProjectileAction.cpp" />
270 274
 		<Unit filename="Entities/Weapons/FireProjectileAction.h" />
271 275
 		<Unit filename="Entities/Weapons/FiringMechanism.cpp" />
@@ -581,6 +585,8 @@
581 585
 		<Unit filename="Physics/PhysicsManager.h" />
582 586
 		<Unit filename="Physics/RenderCallback.cpp" />
583 587
 		<Unit filename="Physics/RenderCallback.h" />
  588
+		<Unit filename="Physics/WaterPhysicsSystem.cpp" />
  589
+		<Unit filename="Physics/WaterPhysicsSystem.h" />
584 590
 		<Unit filename="Resources/CEGUI/layouts/DistanceJoints.layout" />
585 591
 		<Unit filename="Resources/CEGUI/layouts/DynamicEditor.layout" />
586 592
 		<Unit filename="Resources/CEGUI/layouts/EditBox.layout" />
17  GameObject.h
@@ -25,6 +25,8 @@ class GameObjectBase: public TreeNode<GameObjectBase>
25 25
         GameObjectBase* getNode(const std::string& _address);
26 26
         GameObjectType* getType();
27 27
         GameObjectBase* getIndividualNode(const std::string& _address);
  28
+        template <typename Type>
  29
+        Type* getChildOfType();
28 30
     protected:
29 31
         void setParent(TreeNode<GameObjectBase*>* _parent);
30 32
         void fireEvent(EventHandle* _eventHandle){_eventHandle->fire(this);}
@@ -104,6 +106,21 @@ GameObjectType GameObject<DerivedObject>::type(EvaluateTypeName<DerivedObject>()
104 106
 
105 107
 
106 108
 
  109
+template <typename Type>
  110
+Type* GameObjectBase::getChildOfType()
  111
+{
  112
+    Type* ret = nullptr;
  113
+    for (GameObjectBase* child = getChildren(); child; child = child->getNext())
  114
+    {
  115
+        if (dynamic_cast<Type*>(child))
  116
+        {
  117
+            ret = static_cast<Type*>(child);
  118
+            break;
  119
+        }
  120
+    }
  121
+    assert(ret);
  122
+    return ret;
  123
+}
107 124
 template <typename DerivedObject>
108 125
 AutoInstantiate<DerivedObject>::AutoInstantiate(GameObjectType* _type)
109 126
 {
4  Graphics/GraphicsManager.cpp
@@ -135,3 +135,7 @@ void GraphicsManager::deleteSprite(Sprite* _sprite)
135 135
     spriteList->deleteSprite(_sprite);
136 136
 }
137 137
 
  138
+void GraphicsManager::registerActions(GameObjectType* _type)
  139
+{
  140
+}
  141
+
7  Graphics/GraphicsManager.h
@@ -11,7 +11,7 @@ class Icon;
11 11
 class Sprite;
12 12
 class SpriteList;
13 13
 
14  
-extern class GraphicsManager
  14
+extern class GraphicsManager : public GameObject<GraphicsManager>
15 15
 {
16 16
     public:
17 17
         GraphicsManager();
@@ -29,6 +29,11 @@ extern class GraphicsManager
29 29
         TextureContext* getTexture(const char* texture){return mContentManager.getTexture(texture);}
30 30
         void addSprite(Sprite* _sprite);
31 31
         void deleteSprite(Sprite* _sprite);
  32
+        static std::string name()
  33
+        {
  34
+            return "GraphicsManager";
  35
+        }
  36
+        static void registerActions(GameObjectType* _type);
32 37
     protected:
33 38
     private:
34 39
         SpriteList* spriteList;
4  Level/LevelData.cpp
@@ -59,7 +59,7 @@ Level* LevelData::build()
59 59
         entityLists[i]->build(factories, level);
60 60
     }
61 61
 
62  
-    {
  62
+    /*{
63 63
         auto waterFactory = factories->getFactory<Entity>("WaterParticle");
64 64
         FactoryParameters params;
65 65
         for (int i = -10; i != 9; i++)
@@ -70,7 +70,7 @@ Level* LevelData::build()
70 70
                 waterFactory->use(&params, level);
71 71
             }
72 72
         }
73  
-    }
  73
+    }*/
74 74
     return level;
75 75
 }
76 76
 
4  Physics/BodyParts/b2FixtureBodyPart.cpp
@@ -19,6 +19,10 @@ b2Body* b2FixtureBodyPart::getBody()
19 19
 {
20 20
     return fixture->GetBody();
21 21
 }
  22
+b2Fixture* b2FixtureBodyPart::getFixture()
  23
+{
  24
+    return fixture;
  25
+}
22 26
 
23 27
 Entity* b2FixtureBodyPart::getEntity()
24 28
 {
7  Physics/Factories/b2FixtureBodyPartFactory.cpp
@@ -3,6 +3,7 @@
3 3
 #include <Physics/BodyParts/b2FixtureBodyPart.h>
4 4
 #include <Entities/CollisionResponse.h>
5 5
 #include <Physics/BodyParts/b2BodyBodyPart.h>
  6
+#include <Physics/WaterPhysicsSystem.h>
6 7
 
7 8
 b2FixtureBodyPartFactory::b2FixtureBodyPartFactory()
8 9
 {
@@ -50,6 +51,8 @@ void b2FixtureBodyPartFactory::init(FactoryLoader* _loader, AbstractFactories* _
50 51
             throw -1;
51 52
         }
52 53
     }
  54
+    waterSystem = _factories->getWorld()->getChildOfType<WaterPhysicsSystem>();
  55
+    addToWaterSystem = _loader->get<bool>("addToWaterSystem", type == e_Polygon);
53 56
 }
54 57
 
55 58
 BodyPart* b2FixtureBodyPartFactory::useFactory(FactoryParameters* _parameters)
@@ -75,5 +78,9 @@ BodyPart* b2FixtureBodyPartFactory::useFactory(FactoryParameters* _parameters)
75 78
     {
76 79
         delete fixtureDef.shape;
77 80
     }
  81
+    if (addToWaterSystem)
  82
+    {
  83
+        waterSystem->addFixture(bodyPart);
  84
+    }
78 85
     return bodyPart;
79 86
 }
3  Physics/Factories/b2FixtureBodyPartFactory.h
@@ -4,6 +4,7 @@
4 4
 #include <AbstractFactory/AbstractFactory.h>
5 5
 #include <Physics/BodyParts/BodyPart.h>
6 6
 #include <Box2D/Box2D.h>
  7
+class WaterPhysicsSystem;
7 8
 
8 9
 class b2FixtureBodyPartFactory : public AbstractFactory<BodyPart, b2FixtureBodyPartFactory>
9 10
 {
@@ -29,6 +30,8 @@ class b2FixtureBodyPartFactory : public AbstractFactory<BodyPart, b2FixtureBodyP
29 30
             e_ShapeTypeMax,
30 31
         };
31 32
         ShapeType type;
  33
+        WaterPhysicsSystem* waterSystem; /// FIXME needs to not be hardcoded
  34
+        bool addToWaterSystem;
32 35
 };
33 36
 
34 37
 #endif // b2FixtureBodyPartFACTORY_H
2  Physics/PhysicsManager.cpp
@@ -8,6 +8,7 @@
8 8
 #include <Physics/RenderCallback.h>
9 9
 #include <Physics/ContactListener.h>
10 10
 #include <Physics/DebugDraw.h>
  11
+#include <Physics/WaterPhysicsSystem.h>
11 12
 #include <Timer.h>
12 13
 #include <stack>
13 14
 #define DEBUG_DRAW
@@ -32,6 +33,7 @@ void PhysicsManager::init(CollisionDatabase* _database)
32 33
     mWorld->SetDebugDraw(debugDraw);
33 34
 #endif
34 35
     mWorld->SetContactListener(contactListener);
  36
+    attachChild(new WaterPhysicsSystem);
35 37
 }
36 38
 void PhysicsManager::clear()
37 39
 {
389  Physics/WaterPhysicsSystem.cpp
... ...
@@ -0,0 +1,389 @@
  1
+#include "WaterPhysicsSystem.h"
  2
+#include <Physics/BodyParts/b2FixtureBodyPart.h>
  3
+#include <Box2D/Box2D.h>
  4
+#include <Types/Vec2f.h>
  5
+
  6
+WaterPhysicsSystem::WaterPhysicsSystem()
  7
+{
  8
+    //ctor
  9
+    addEdge({-10,0}, {-4,6}, nullptr);
  10
+    addEdge({4,6}, {10,0}, nullptr);
  11
+    addEdge({-6,5}, {6,5}, nullptr);
  12
+}
  13
+
  14
+WaterPhysicsSystem::~WaterPhysicsSystem()
  15
+{
  16
+    //dtor
  17
+}
  18
+
  19
+void WaterPhysicsSystem::registerActions(GameObjectType* _type)
  20
+{
  21
+
  22
+}
  23
+#include <iostream>
  24
+#define OUTPUT_VEC2F (a, b) a.x << ',' << a.y << ")->(" << b.x << ',' << b.y << ')'
  25
+void WaterPhysicsSystem::addFixture(b2FixtureBodyPart* _bodyPart)
  26
+{
  27
+    b2Fixture* fixture = _bodyPart->getFixture();
  28
+    b2Shape* shape = fixture->GetShape();
  29
+    assert(dynamic_cast<b2PolygonShape*>(shape)); /// Can't support circles at this time
  30
+    b2PolygonShape* polygon = static_cast<b2PolygonShape*>(shape);
  31
+    for (int i = 0; i != polygon->GetVertexCount(); i++)
  32
+    {
  33
+        Vec2f a = polygon->GetVertex(i);
  34
+        Vec2f b = polygon->GetVertex((i+1)%polygon->GetVertexCount());
  35
+        std::cout << "Edges size: " << edges.size() << std::endl;
  36
+        for (unsigned int i = 0; i != edges.size(); i++)
  37
+        {
  38
+            Vec2f a = edges[i]->aPosition();
  39
+            Vec2f b = edges[i]->bPosition();
  40
+            std::cout << "\t (" << a.x << ',' << a.y << ")->(" << b.x << ',' << b.y << ')' << std::endl;
  41
+        }
  42
+        std::cout << "Adding (" << a.x << ',' << a.y << ")->(" << b.x << ',' << b.y << ')' << std::endl;
  43
+        addEdge(a, b, _bodyPart);
  44
+    }
  45
+        std::cout << "Edges size: " << edges.size() << std::endl;
  46
+        for (unsigned int i = 0; i != edges.size(); i++)
  47
+        {
  48
+            Vec2f a = edges[i]->aPosition();
  49
+            Vec2f b = edges[i]->bPosition();
  50
+            std::cout << "\t (" << a.x << ',' << a.y << ")->(" << b.x << ',' << b.y << ')' << std::endl;
  51
+        }
  52
+}
  53
+
  54
+void WaterPhysicsSystem::addEdge(const Vec2f& _a, const Vec2f& _b, b2FixtureBodyPart* _destructionListener)
  55
+{
  56
+    Edge* edge = new Edge;
  57
+    Edge* intersections[2] = {0, 0};
  58
+    unsigned int numIntersections = 0;
  59
+    for (unsigned int i = 0; i != edges.size(); i++)
  60
+    {
  61
+        try
  62
+        {
  63
+            Vec2f position = intersectionPosition(_a, _b, edges[i]->aPosition(), edges[i]->bPosition());
  64
+            assert(numIntersections < 2);
  65
+            intersections[numIntersections] = edges[i];
  66
+            numIntersections++;
  67
+        }
  68
+        catch (int i)
  69
+        {
  70
+        }
  71
+    }
  72
+    switch (numIntersections)
  73
+    {
  74
+        case 0:
  75
+        {
  76
+            edge->setA(new Vertex(_a), _b);
  77
+            edge->setB(new Vertex(_b));
  78
+            edges.push_back(edge);
  79
+            break;
  80
+        }
  81
+        case 1:
  82
+        {
  83
+            float scalar = intersectionValue(_a, _b, intersections[0]->aPosition(), intersections[0]->bPosition());
  84
+            if (scalar < 0.1f)
  85
+            {
  86
+                Vec2f position = intersectionPosition(_a, _b, intersections[0]->aPosition(), intersections[0]->bPosition());
  87
+                edge->setB(position, new Vertex(_b));
  88
+                createVertex(position, intersections[0], edge);
  89
+            }
  90
+            else if (scalar > 0.9f)
  91
+            {
  92
+                Vec2f position = intersectionPosition(_a, _b, intersections[0]->aPosition(), intersections[0]->bPosition());
  93
+                edge->setA(new Vertex(_a), position);
  94
+                createVertex(position, edge, intersections[0]);
  95
+            }
  96
+            else assert(false);
  97
+            edges.push_back(edge);
  98
+            break;
  99
+        }
  100
+        case 2:
  101
+        {
  102
+            float scalar0 = intersectionValue( _a, _b, intersections[0]->aPosition(), intersections[0]->bPosition());
  103
+            float scalar1 = intersectionValue( _a, _b, intersections[1]->aPosition(), intersections[1]->bPosition());
  104
+            if (scalar0 > scalar1)
  105
+            {
  106
+                {
  107
+                    Edge* temp = intersections[0];
  108
+                    intersections[0] = intersections[1];
  109
+                    intersections[1] = temp;
  110
+                }
  111
+                {
  112
+                    float temp = scalar0;
  113
+                    scalar0 = scalar1;
  114
+                    scalar1 = temp;
  115
+                }
  116
+            }
  117
+            Vec2f position0 = positionFromScalarValue(_a, _b, scalar0); /// FIXME might be the wrong line
  118
+            Vec2f position1 = positionFromScalarValue(_a, _b, scalar1);
  119
+
  120
+            createVertex(position0, intersections[0], edge, &position1);
  121
+            createVertex(position1, edge, intersections[1]);
  122
+            edges.push_back(edge);
  123
+            break;
  124
+        }
  125
+        default:
  126
+        {
  127
+            assert(false);
  128
+        }
  129
+    }
  130
+    //edges.push_back({_a, _b});
  131
+}
  132
+
  133
+void WaterPhysicsSystem::createVertex(const Vec2f& _position, Edge* _a, Edge* _b, Vec2f* _bBPosition)
  134
+{
  135
+    Vertex* vertex = nullptr;
  136
+    Vertex* oldA = _a->getB();
  137
+    Vertex* oldB = _b->getA();
  138
+    if (oldA == nullptr)
  139
+    {
  140
+        if (oldB == nullptr)
  141
+        {
  142
+            vertex = new Vertex(_position);
  143
+        }
  144
+        else
  145
+        {
  146
+            vertex = oldB;
  147
+            vertex->position = _position;
  148
+        }
  149
+    }
  150
+    else
  151
+    {
  152
+        vertex = oldA;
  153
+        vertex->position = _position;
  154
+    }
  155
+    assert(vertex);
  156
+    _a->setB(vertex);
  157
+    if (_bBPosition)
  158
+        _b->setA(vertex, *_bBPosition);
  159
+    else
  160
+        _b->setA(vertex);
  161
+}
  162
+
  163
+Vec2f intersectionPosition(Vec2f _aStart, Vec2f _aEnd, Vec2f _bStart, Vec2f _bEnd, bool _aNeedsIntersect, bool _bNeedsIntersect);
  164
+
  165
+std::vector<Vec2f> WaterPhysicsSystem::getContainer(const Vec2f& _position)
  166
+{
  167
+    Vec2f a = _position;
  168
+    Vec2f b = a;
  169
+    a.x -= 10000;
  170
+    b.x += 10000;
  171
+
  172
+    Edge* left = nullptr;
  173
+    float leftX;
  174
+    Edge* right = nullptr;
  175
+    float rightX;
  176
+    unsigned int edgesSize = edges.size();
  177
+    for (unsigned int i = 0; i != edgesSize; i++)
  178
+    {
  179
+        Edge* edge = edges[i];
  180
+        try
  181
+        {
  182
+            Vec2f position = intersectionPosition(edge->aPosition(), edge->bPosition(), a, b, true, false);
  183
+            if (position.x < _position.x)
  184
+            {
  185
+                if (left)
  186
+                {
  187
+                    if (leftX < position.x)
  188
+                    {
  189
+                        left = edge;
  190
+                        leftX = position.x;
  191
+                    }
  192
+                }
  193
+                else
  194
+                {
  195
+                    left = edge;
  196
+                    leftX = position.x;
  197
+                }
  198
+            }
  199
+            else
  200
+            {
  201
+                if (right)
  202
+                {
  203
+                    if (rightX > position.x)
  204
+                    {
  205
+                        right = edge;
  206
+                        rightX = position.x;
  207
+                    }
  208
+                }
  209
+                else
  210
+                {
  211
+                    right = edge;
  212
+                    rightX = position.x;
  213
+                }
  214
+            }
  215
+        }
  216
+        catch (int i)
  217
+        {
  218
+        }
  219
+    }
  220
+    if (left == nullptr || right == nullptr)
  221
+    {
  222
+        assert(false);
  223
+    }
  224
+    std::vector<Vec2f> shape;
  225
+    shape.push_back({rightX, _position.y});
  226
+    while (true)
  227
+    {
  228
+        if (right == left)
  229
+        {
  230
+            shape.push_back({leftX, _position.y});
  231
+            return shape;
  232
+        }
  233
+        //if (right->getA()->getVertexShape() == Vertex::eLeft) /// FIXME make this get the next on the vertex
  234
+        {
  235
+            shape.push_back(right->aPosition());
  236
+            right = right->getA()->getNextOutfacing(right);
  237
+        }
  238
+        //else assert(false);
  239
+    }
  240
+}
  241
+
  242
+/*WaterPhysicsSystem::Vertex::VertexShape WaterPhysicsSystem::Vertex::getVertexShape()
  243
+{
  244
+    Vec2f dirA = a->aPosition() - position;
  245
+    Vec2f dirB = b->bPosition() - position;
  246
+    dirA /= dirA.Length();
  247
+    dirB /= dirB.Length();
  248
+    float angleA = atan2(dirA.x, dirA.y);
  249
+    float angleB = atan2(dirB.x, dirB.y);
  250
+    float angleDiff = angleB - angleA;
  251
+    return eLeft;
  252
+}*/
  253
+
  254
+void WaterPhysicsSystem::Edge::setA(Vertex* _a)
  255
+{
  256
+    if (a)
  257
+        if (a == _a)
  258
+            return;
  259
+        else
  260
+            a->removeEdge(this);
  261
+    a = _a;
  262
+    a->addOutwardsEdge(this);
  263
+}
  264
+void WaterPhysicsSystem::Edge::setB(Vertex* _b)
  265
+{
  266
+    if (b)
  267
+        if (b == _b)
  268
+            return;
  269
+        else
  270
+            b->removeEdge(this);
  271
+    b = _b;
  272
+    b->addInwardsEdge(this);
  273
+}
  274
+void WaterPhysicsSystem::Edge::setA(Vertex* _a, const Vec2f& _b)
  275
+{
  276
+    if (a)
  277
+        a->removeEdge(this);
  278
+    a = _a;
  279
+    a->addOutwardsEdge(this, _b);
  280
+}
  281
+void WaterPhysicsSystem::Edge::setB(const Vec2f& _a, Vertex* _b)
  282
+{
  283
+    if (b)
  284
+        b->removeEdge(this);
  285
+    b = _b;
  286
+    b->addInwardsEdge(_a, this);
  287
+}
  288
+WaterPhysicsSystem::Edge* WaterPhysicsSystem::Vertex::getNextOutfacing(Edge* _b)
  289
+{
  290
+    for (unsigned int i = 0; i != edges.size(); i++)
  291
+    {
  292
+        if (edges[i].edge == _b)
  293
+        {
  294
+            i++;
  295
+            if (i == edges.size())
  296
+                i = 0;
  297
+            return edges[i].edge;
  298
+        }
  299
+    }
  300
+    throw -1;
  301
+}
  302
+void WaterPhysicsSystem::Vertex::addInwardsEdge(Edge* _edge)
  303
+{
  304
+    addInwardsEdge(_edge->aPosition(), _edge);
  305
+}
  306
+void WaterPhysicsSystem::Vertex::addOutwardsEdge(Edge* _edge)
  307
+{
  308
+    addOutwardsEdge(_edge, _edge->bPosition());
  309
+}
  310
+void WaterPhysicsSystem::Vertex::addInwardsEdge(const Vec2f& _a, Edge* _edge)
  311
+{
  312
+    assert(_edge->getB() == this);
  313
+    Vec2f dir = _a - position;
  314
+    dir /= dir.Length();
  315
+    float angle = atan2(dir.y, dir.x);
  316
+    for (auto i = edges.begin(); i != edges.end(); i++)
  317
+    {
  318
+        if (i->angle > angle)
  319
+        {
  320
+            edges.insert(i, {angle, _edge});
  321
+            return;
  322
+        }
  323
+    }
  324
+    edges.push_back({angle, _edge});
  325
+}
  326
+void WaterPhysicsSystem::Vertex::addOutwardsEdge(Edge* _edge, const Vec2f& _b)
  327
+{
  328
+    assert(_edge->getA() == this);
  329
+    Vec2f dir = _b - position;
  330
+    dir /= dir.Length();
  331
+    float angle = atan2(dir.y, dir.x);
  332
+    for (auto i = edges.begin(); i != edges.end(); i++)
  333
+    {
  334
+        if (i->angle > angle)
  335
+        {
  336
+            edges.insert(i, {angle, _edge});
  337
+            return;
  338
+        }
  339
+    }
  340
+    edges.push_back({angle, _edge});
  341
+}
  342
+void WaterPhysicsSystem::Vertex::removeEdge(Edge* _b)
  343
+{
  344
+    for (auto i = edges.begin(); i != edges.end(); i++)
  345
+    {
  346
+        if (i->edge == _b)
  347
+        {
  348
+            edges.erase(i);
  349
+            if (edges.size() == 0)
  350
+                delete this;
  351
+            return;
  352
+        }
  353
+    }
  354
+    throw -1;
  355
+}
  356
+
  357
+
  358
+
  359
+
  360
+
  361
+
  362
+
  363
+
  364
+
  365
+
  366
+
  367
+
  368
+
  369
+
  370
+
  371
+
  372
+
  373
+
  374
+
  375
+
  376
+
  377
+
  378
+
  379
+
  380
+
  381
+
  382
+
  383
+
  384
+
  385
+
  386
+
  387
+
  388
+
  389
+
89  Physics/WaterPhysicsSystem.h
... ...
@@ -0,0 +1,89 @@
  1
+#ifndef WATERPHYSICSSYSTEM_H
  2
+#define WATERPHYSICSSYSTEM_H
  3
+
  4
+#include <GameObject.h>
  5
+#include <Types/Vec2f.h>
  6
+#include <unordered_map>
  7
+class b2FixtureBodyPart;
  8
+
  9
+class WaterPhysicsSystem : public GameObject<WaterPhysicsSystem>
  10
+{
  11
+    public:
  12
+        WaterPhysicsSystem();
  13
+        virtual ~WaterPhysicsSystem();
  14
+        static std::string name()
  15
+        {
  16
+            return "WaterPhysicsSystem";
  17
+        }
  18
+        static void registerActions(GameObjectType* _type);
  19
+        void addFixture(b2FixtureBodyPart* _bodyPart);
  20
+        std::vector<Vec2f> getContainer(const Vec2f& _position);
  21
+    protected:
  22
+    private:
  23
+        void addEdge(const Vec2f& _a, const Vec2f& _b, b2FixtureBodyPart* _destructionListener);
  24
+
  25
+        struct Vertex;
  26
+        class Edge
  27
+        {
  28
+            public:
  29
+                enum EdgeType
  30
+                {
  31
+                    eLeftFloor,
  32
+                    eRightFloor,
  33
+                    eRightRoof,
  34
+                    eLeftRoof,
  35
+                    eEdgeTypeMax
  36
+                };
  37
+                Edge(){a = b = nullptr;}
  38
+                void setA(Vertex* _a);
  39
+                void setB(Vertex* _b);
  40
+                void setA(Vertex* _a, const Vec2f& _bPosition);
  41
+                void setB(const Vec2f& _aPosition, Vertex* _b);
  42
+                Vertex* getA(){return a;}
  43
+                Vertex* getB(){return b;}
  44
+                Vec2f aPosition(){return a->position;}
  45
+                Vec2f bPosition(){return b->position;}
  46
+                EdgeType getEdgeType();
  47
+            private:
  48
+                Vertex* a,* b;
  49
+        };
  50
+        struct Vertex
  51
+        {
  52
+            enum VertexType
  53
+            {
  54
+                eBed,
  55
+                eRoot,
  56
+                eChangeInGradient,
  57
+                eVertexTypeMax
  58
+            };
  59
+            enum VertexShape
  60
+            {
  61
+                eLeft,
  62
+                eRight,
  63
+                eVertexShapeMax
  64
+            };
  65
+            Vertex(const Vec2f& _position){position = _position;}
  66
+            Vec2f position;
  67
+            struct EdgeAttachment
  68
+            {
  69
+                float angle;
  70
+                Edge* edge;
  71
+            };
  72
+            std::vector<EdgeAttachment> edges;
  73
+            void addInwardsEdge(Edge* _edge);
  74
+            void addOutwardsEdge(Edge* _edge);
  75
+            void addInwardsEdge(const Vec2f& _a, Edge* _edge);
  76
+            void addOutwardsEdge(Edge* _edge, const Vec2f& _b);
  77
+            void removeEdge(Edge* _edge);
  78
+            Edge* getNextOutfacing(Edge* _edge);
  79
+            //Edge* a,* b;
  80
+            //VertexType getVertexType();
  81
+            //VertexShape getVertexShape();
  82
+        };
  83
+
  84
+        void createVertex(const Vec2f& _position, Edge* _a, Edge* _b, Vec2f* _bBPosition = nullptr);
  85
+        //std::unordered_map<b2FixtureBodyPart*, std::vector<Edge>> edgeMap;
  86
+        std::vector<Edge*> edges;
  87
+};
  88
+
  89
+#endif // WATERPHYSICSSYSTEM_H
BIN  Resources/Graphics/Materials/player.mat
Binary file not shown
32  Resources/Levels.xml
@@ -108,6 +108,7 @@
108 108
 	</Factory>
109 109
 	<Factory Name="StaticWaterFixture" Type="b2FixtureBodyPartFactory" Product="BodyPart">
110 110
 	  <PropertyBag>
  111
+	    <Property Type="bool" Name="addToWaterSystem" Value="false" />
111 112
 	    <Property Type="string" Name="collisionResponse" Value="WaterVolumeResponse" />
112 113
 	    <Property Type="int" Name="shapeType" Value="2" />
113 114
 	  </PropertyBag>
@@ -150,7 +151,7 @@
150 151
 	    <Property Type="string" Name="body" Value="LevelGeometryBody" />
151 152
 	  </PropertyBag>
152 153
 	</Factory>
153  
-	<Factory Name="StaticWater" Type="LevelGeometryFactory" Product="Entity">
  154
+	<Factory Name="StaticWater" Type="WaterVolumeFactory" Product="Entity">
154 155
 	  <PropertyBag>
155 156
 	    <Property Type="string" Name="body" Value="StaticWaterBody" />
156 157
 	  </PropertyBag>
@@ -192,11 +193,16 @@
192 193
 	    <Property Type="string" Name="body" Value="WaterParticleBody" />
193 194
 	  </PropertyBag>
194 195
 	</Factory>
195  
-      </FactoryList>
196  
-      
  196
+      </FactoryList>	
197 197
       <EntityList Name="Entities">
  198
+	<Entity Type="StaticWater">
  199
+	  <PropertyBag>
  200
+	    <Property Type="float" Name="height" Value="2.0" />
  201
+	  </PropertyBag>
  202
+	</Entity>
198 203
 	<Entity Type="LevelGeometry">
199 204
 	  <PropertyBag>
  205
+	    <Property Type="bool" Name="addToWaterSystem" Value="true" />
200 206
 	    <Array Type="Vec2f" Name="points">
201 207
 	      <Member Value="-12.46667 17.4 " />
202 208
 	      <Member Value="13.4 18.1333 " />
@@ -205,26 +211,6 @@
205 211
 	    </Array>
206 212
 	  </PropertyBag>
207 213
 	</Entity>
208  
-	<Entity Type="LevelGeometry">
209  
-	  <PropertyBag>
210  
-	    <Array Type="Vec2f" Name="points">
211  
-	      <Member Value="-18 0" />
212  
-	      <Member Value="-12 18" />
213  
-	      <Member Value="-12 24" />
214  
-	      <Member Value="-18 24" />
215  
-	    </Array>
216  
-	  </PropertyBag>
217  
-	</Entity>
218  
-	<Entity Type="LevelGeometry">
219  
-	  <PropertyBag>
220  
-	    <Array Type="Vec2f" Name="points">
221  
-	      <Member Value="9 0" />
222  
-	      <Member Value="18 0" />
223  
-	      <Member Value="18 24" />
224  
-	      <Member Value="2 24" />
225  
-	    </Array>
226  
-	  </PropertyBag>
227  
-	</Entity>
228 214
       </EntityList>
229 215
     </Level>
230 216
     <EventList Name="Events" />
3  Types/Tree.h
@@ -71,7 +71,8 @@ template <typename Object>
71 71
 void List<Object>::attachChild(ListNode<Object>* _child)
72 72
 {
73 73
   assert(_child->getParent() != this);
74  
-  _child->getParent()->detachChild(_child);
  74
+  if (_child->getParent())
  75
+      _child->getParent()->detachChild(_child);
75 76
     _child->parent = static_cast<Object*>(this);
76 77
     _child->prev = nullptr;
77 78
     _child->next = children;
383  Types/Vec2f.cpp
... ...
@@ -1,4 +1,5 @@
1 1
 #include "Vec2f.h"
  2
+#include <cassert>
2 3
 using namespace std;
3 4
 std::ostream& operator<< (ostream &out, Vec2f &vec)
4 5
 {
@@ -12,3 +13,385 @@ istream& operator>> (istream &in, Vec2f &vec)
12 13
     in >> vec.y;
13 14
     return in;
14 15
 }
  16
+
  17
+
  18
+float intersectionXValue(Vec2f _aStart, Vec2f _aEnd, Vec2f _bStart, Vec2f _bEnd)
  19
+{
  20
+    /// y = mx + b
  21
+    float _adeltaX = _aEnd.x - _aStart.x;
  22
+    float _adeltaY = _aEnd.y - _aStart.y;
  23
+    float _bm = _adeltaY / _adeltaX;
  24
+    /// b = y - mx
  25
+    float _ab = _aStart.y - (_bm * _aStart.x);
  26
+
  27
+
  28
+    /// y = mx + b
  29
+    float _bdeltaX = _bEnd.x - _bStart.x;
  30
+    float _bdeltaY = _bEnd.y - _bStart.y;
  31
+    float _am = _bdeltaY / _bdeltaX;
  32
+    /// b = y - mx
  33
+    float _bb = _bStart.y - (_am * _bStart.x);
  34
+
  35
+    /// (_am*x) + _ab = (_bm*x) + _bb
  36
+    /// (_am*x) + _ab = (_bm*x) + _bb
  37
+    /// (_am*x) = (_bm*x) + _bb - _ab
  38
+    /// (_am*x) - (_bm*x) = _bb - _ab
  39
+    /// (_am + _bm)x = _bb - _ab
  40
+    /// x = (_bb - _ab) / (_am + _bm)
  41
+    float x = (_bb - _ab) / (_am + _bm);
  42
+
  43
+    return x;
  44
+}
  45
+/*float intersectionValue(Vec2f _aStart, Vec2f _aEnd, Vec2f _bStart, Vec2f _bEnd)
  46
+{
  47
+    /// y = mx + b
  48
+    float _adeltaX = _aEnd.x - _aStart.x;
  49
+    float _adeltaY = _aEnd.y - _aStart.y;
  50
+    float _am = _adeltaY / _adeltaX;
  51
+    if (_adeltaX == 0.0f)
  52
+    {
  53
+        assert(false);
  54
+    }
  55
+    /// b = y - mx
  56
+    float _ab = _aStart.y - (_am * _aStart.x);
  57
+
  58
+
  59
+    /// y = mx + b
  60
+    float _bdeltaX = _bEnd.x - _bStart.x;
  61
+    float _bdeltaY = _bEnd.y - _bStart.y;
  62
+    float _bm = _bdeltaY / _bdeltaX;
  63
+    if (_bdeltaX == 0.0f)
  64
+    {
  65
+        assert(false);
  66
+    }
  67
+    /// b = y - mx
  68
+    float _bb = _bStart.y - (_bm * _bStart.x);
  69
+
  70
+    /// (_am*x) + _ab = (_bm*x) + _bb
  71
+    /// (_am*x) + _ab = (_bm*x) + _bb
  72
+    /// (_am*x) = (_bm*x) + _bb - _ab
  73
+    /// (_am*x) - (_bm*x) = _bb - _ab
  74
+    /// (_am + _bm)x = _bb - _ab
  75
+    /// x = (_bb - _ab) / (_am + _bm)
  76
+    float x = (_bb - _ab) / (_am + _bm);
  77
+
  78
+    return (x - _aStart.x) / (_adeltaX);
  79
+}*/
  80
+
  81
+Vec2f intersectionPosition(Vec2f _aStart, Vec2f _aEnd, Vec2f _bStart, Vec2f _bEnd, bool _aNeedsIntersect, bool _bNeedsIntersect);
  82
+Vec2f intersectionPosition(Vec2f _aStart, Vec2f _aEnd, Vec2f _bStart, Vec2f _bEnd)
  83
+{
  84
+    return intersectionPosition(_aStart, _aEnd, _bStart, _bEnd, true, true);
  85
+}
  86
+float intersectionValue(Vec2f _aStart, Vec2f _aEnd, Vec2f _bStart, Vec2f _bEnd, bool _aNeedsIntersect, bool _bNeedsIntersect);
  87
+float intersectionValue(Vec2f _aStart, Vec2f _aEnd, Vec2f _bStart, Vec2f _bEnd)
  88
+{
  89
+    return intersectionValue(_aStart, _aEnd, _bStart, _bEnd, true, true);
  90
+}
  91
+
  92
+//  public domain function by Darel Rex Finley, 2006
  93
+
  94
+
  95
+
  96
+//  Determines the intersection point of the line segment defined by points A and B
  97
+//  with the line segment defined by points C and D.
  98
+//
  99
+//  Returns YES if the intersection point was found, and stores that point in X,Y.
  100
+//  Returns NO if there is no determinable intersection point, in which case X,Y will
  101
+//  be unmodified.
  102
+
  103
+bool lineSegmentIntersection(
  104
+float Ax, float Ay,
  105
+float Bx, float By,
  106
+float Cx, float Cy,
  107
+float Dx, float Dy,
  108
+float *X, float *Y,
  109
+float *aIntercept, bool _aNeedsIntersect)
  110
+{
  111
+
  112
+  float  distAB, theCos, theSin, newX, ABpos ;
  113
+
  114
+  //  Fail if either line segment is zero-length.
  115
+  if (Ax==Bx && Ay==By || Cx==Dx && Cy==Dy) return false;
  116
+
  117
+  //  Fail if the segments share an end-point.
  118
+  if (Ax==Cx && Ay==Cy || Bx==Cx && By==Cy
  119
+  ||  Ax==Dx && Ay==Dy || Bx==Dx && By==Dy) {
  120
+    return false; }
  121
+
  122
+  //  (1) Translate the system so that point A is on the origin.
  123
+  Bx-=Ax; By-=Ay;
  124
+  Cx-=Ax; Cy-=Ay;
  125
+  Dx-=Ax; Dy-=Ay;
  126
+
  127
+  //  Discover the length of segment A-B.
  128
+  distAB=sqrt(Bx*Bx+By*By);
  129
+
  130
+  //  (2) Rotate the system so that point B is on the positive X axis.
  131
+  theCos=Bx/distAB;
  132
+  theSin=By/distAB;
  133
+  newX=Cx*theCos+Cy*theSin;
  134
+  Cy  =Cy*theCos-Cx*theSin; Cx=newX;
  135
+  newX=Dx*theCos+Dy*theSin;
  136
+  Dy  =Dy*theCos-Dx*theSin; Dx=newX;
  137
+
  138
+
  139
+  //  Fail if segment C-D doesn't cross line A-B.
  140
+  if (Cy<0. && Dy<0. || Cy>=0. && Dy>=0.) return false;
  141
+
  142
+  //  (3) Discover the position of the intersection point along line A-B.
  143
+  ABpos=Dx+(Cx-Dx)*Dy/(Dy-Cy);
  144
+  *aIntercept = ABpos;
  145
+
  146
+  //  Fail if segment C-D crosses line A-B outside of segment A-B.
  147
+  if ((ABpos<0. || ABpos>distAB) && _aNeedsIntersect) return false;
  148
+
  149
+  //  (4) Apply the discovered position to line A-B in the original coordinate system.
  150
+  *X=Ax+ABpos*theCos;
  151
+  *Y=Ay+ABpos*theSin;
  152
+
  153
+  //  Success.
  154
+  return true;
  155
+}
  156
+float intersectionValue(Vec2f _aStart, Vec2f _aEnd, Vec2f _bStart, Vec2f _bEnd, bool _aNeedsIntersect, bool _bNeedsIntersect)
  157
+{
  158
+    float _adeltaX = _aEnd.x - _aStart.x;
  159
+    float _bdeltaX = _bEnd.x - _bStart.x;
  160
+    if (_adeltaX == 0.0f || _bdeltaX == 0.0f)
  161
+    {
  162
+        /// m = dX / dY
  163
+        float _adeltaX = _aEnd.x - _aStart.x;
  164
+        float _adeltaY = _aEnd.y - _aStart.y;
  165
+        float _bdeltaX = _bEnd.x - _bStart.x;
  166
+        float _bdeltaY = _bEnd.y - _bStart.y;
  167
+        if (_adeltaY == 0.0f)
  168
+        {
  169
+            assert(_adeltaX != 0.0f);
  170
+            return (_bStart.x - _aStart.x) / (_aEnd.x - _aStart.x);
  171
+        }
  172
+        if (_bdeltaY == 0.0f)
  173
+        {
  174
+            assert(_bdeltaX != 0.0f);
  175
+            float aIntersect1 = (_bStart.y - _aStart.y) / (_aEnd.y - _aStart.y);
  176
+            float aIntersect2 = (_bEnd.y - _aStart.y) / (_aEnd.y - _aStart.y);
  177
+            float bIntersect1 = (_aStart.x - _bStart.x) / (_bEnd.x - _bStart.x);
  178
+            float bIntersect2 = (_aEnd.x - _bStart.x) / (_bEnd.x - _bStart.x);
  179
+            return aIntersect1;
  180
+        }
  181
+        /*if (_bdeltaX == 0.0f)
  182
+        {
  183
+            assert(false);
  184
+        }*/
  185
+        float _am = _adeltaX / _adeltaY;
  186
+        float _bm = _bdeltaX / _bdeltaY;
  187
+
  188
+        /// b = x - my
  189
+        float _ab = _aStart.x - (_am * _aStart.y);
  190
+        float _bb = _bStart.x - (_bm * _bStart.y);
  191
+
  192
+        /// (_am*y) + _ab = (_bm*y) + _bb
  193
+        /// (_am*y) + _ab = (_bm*y) + _bb
  194
+        /// (_am*y) = (_bm*y) + _bb - _ab
  195
+        /// (_am*y) - (_bm*y) = _bb - _ab
  196
+        /// (_am - _bm)y = _bb - _ab
  197
+        /// y = (_bb - _ab) / (_am - _bm)
  198
+        float y = (_bb - _ab) / (_am - _bm);
  199
+        if (_am + _bm == 0.0f)
  200
+        {
  201
+            throw -1;
  202
+        }
  203
+
  204
+        /// x = my + b
  205
+        float x = (_am * y) + _ab;
  206
+
  207
+        float _aIntersection = (y - _aStart.y) / (_adeltaY);
  208
+        float _bIntersection = (y - _bStart.y) / (_bdeltaY);
  209
+        if ((_aIntersection < 0 || _aIntersection > 1) && _aNeedsIntersect)
  210
+        {
  211
+            throw -1;
  212
+        }
  213
+        if (_bNeedsIntersect && (_bIntersection < 0 || _bIntersection > 1))
  214
+        {
  215
+            throw -1; /// Doesn't lie on the line
  216
+        }
  217
+        return (y - _aStart.y) / (_adeltaY);
  218
+    }
  219
+    else
  220
+    {
  221
+        /// y = mx + b
  222
+        float _adeltaY = _aEnd.y - _aStart.y;
  223
+        float _am = _adeltaY / _adeltaX;
  224
+        if (_adeltaX == 0.0f)
  225
+        {
  226
+            assert(false);
  227
+        }
  228
+        /// b = y - mx
  229
+        float _ab = _aStart.y - (_am * _aStart.x);
  230
+
  231
+
  232
+        /// y = mx + b
  233
+        float _bdeltaY = _bEnd.y - _bStart.y;
  234
+        float _bm = _bdeltaY / _bdeltaX;
  235
+        if (_bdeltaX == 0.0f)
  236
+        {
  237
+            assert(false);
  238
+        }
  239
+        /// b = y - mx
  240
+        float _bb = _bStart.y - (_bm * _bStart.x);
  241
+
  242
+        /// (_am*x) + _ab = (_bm*x) + _bb
  243
+        /// (_am*x) + _ab = (_bm*x) + _bb
  244
+        /// (_am*x) = (_bm*x) + _bb - _ab
  245
+        /// (_am*x) - (_bm*x) = _bb - _ab
  246
+        /// (_am - _bm)x = _bb - _ab
  247
+        /// x = (_bb - _ab) / (_am - _bm)
  248
+        float x = (_bb - _ab) / (_am - _bm);
  249
+        if (_am + _bm == 0.0f)
  250
+        {
  251
+            throw -1;
  252
+        }
  253
+
  254
+        /// y = mx + b
  255
+        float y = (_am * x) + _ab;
  256
+
  257
+        float _aIntersection = (x - _aStart.x) / (_adeltaX);
  258
+        float _bIntersection = (x - _bStart.x) / (_bdeltaX);
  259
+        if ((_aIntersection < 0 || _aIntersection > 1) && _aNeedsIntersect)
  260
+        {
  261
+            throw -1;
  262
+        }
  263
+        if (_bNeedsIntersect && (_bIntersection < 0 || _bIntersection > 1))
  264
+        {
  265
+            throw -1; /// Doesn't lie on the line
  266
+        }
  267
+        return (x - _aStart.x) / (_adeltaX);
  268
+    }
  269
+}
  270
+Vec2f intersectionPosition(Vec2f _aStart, Vec2f _aEnd, Vec2f _bStart, Vec2f _bEnd, bool _aNeedsIntersect, bool _bNeedsIntersect)
  271
+{
  272
+    float intersect = intersectionValue(_aStart, _aEnd, _bStart, _bEnd, _aNeedsIntersect, _bNeedsIntersect);
  273
+    return ((_aEnd - _aStart) * intersect) + _aStart;
  274
+    /*
  275
+    float _adeltaX = _aEnd.x - _aStart.x;
  276
+    float _bdeltaX = _bEnd.x - _bStart.x;
  277
+    if (_adeltaX == 0.0f || _bdeltaX == 0.0f)
  278
+    {
  279
+        /// m = dX / dY
  280
+        float _adeltaX = _aEnd.x - _aStart.x;
  281
+        float _adeltaY = _aEnd.y - _aStart.y;
  282
+        float _bdeltaX = _bEnd.x - _bStart.x;
  283
+        float _bdeltaY = _bEnd.y - _bStart.y;
  284
+        if (_adeltaY == 0.0f)
  285
+        {
  286
+            assert(_adeltaX != 0.0f);
  287
+            return {_bStart.x, _aStart.y};
  288
+        }
  289
+        /*if (_bdeltaX == 0.0f)
  290
+        {
  291
+            assert(false);
  292
+        }* /
  293
+        float _am = _adeltaX / _adeltaY;
  294
+        float _bm = _bdeltaX / _bdeltaY;
  295
+
  296
+        /// b = x - my
  297
+        float _ab = _aStart.x - (_am * _aStart.y);
  298
+        float _bb = _bStart.x - (_bm * _bStart.y);
  299
+
  300
+        /// (_am*y) + _ab = (_bm*y) + _bb
  301
+        /// (_am*y) + _ab = (_bm*y) + _bb
  302
+        /// (_am*y) = (_bm*y) + _bb - _ab
  303
+        /// (_am*y) - (_bm*y) = _bb - _ab
  304
+        /// (_am - _bm)y = _bb - _ab
  305
+        /// y = (_bb - _ab) / (_am - _bm)
  306
+        float y = (_bb - _ab) / (_am - _bm);
  307
+        if (_am + _bm == 0.0f)
  308
+        {
  309
+            throw -1;
  310
+        }
  311
+
  312
+        /// x = my + b
  313
+        float x = (_am * y) + _ab;
  314
+
  315
+        float _aIntersection = (y - _aStart.y) / (_adeltaY);
  316
+        float _bIntersection = (y - _bStart.y) / (_bdeltaY);
  317
+        if (_aIntersection < 0 || _aIntersection > 1 || !_aNeedsIntersect)
  318
+        {
  319
+            throw -1;
  320
+        }
  321
+        if (!_bNeedsIntersect || _bIntersection < 0 || _bIntersection > 1)
  322
+        {
  323
+            throw -1; /// Doesn't lie on the line
  324
+        }
  325
+        return {x, y};
  326
+    }
  327
+    /// y = mx + b
  328
+    float _adeltaY = _aEnd.y - _aStart.y;
  329
+    float _am = _adeltaY / _adeltaX;
  330
+    if (_adeltaX == 0.0f)
  331
+    {
  332
+        assert(false);
  333
+    }
  334
+    /// b = y - mx
  335
+    float _ab = _aStart.y - (_am * _aStart.x);
  336
+
  337
+
  338
+    /// y = mx + b
  339
+    float _bdeltaY = _bEnd.y - _bStart.y;
  340
+    float _bm = _bdeltaY / _bdeltaX;
  341
+    if (_bdeltaX == 0.0f)
  342
+    {
  343
+        assert(false);
  344
+    }
  345
+    /// b = y - mx
  346
+    float _bb = _bStart.y - (_bm * _bStart.x);
  347
+    /// y = mx + b
  348
+    /// m1x + b1 = m2x + b2
  349
+    /// (_am*x) + _ab = (_bm*x) + _bb
  350
+    /// (_am*x) = (_bm*x) + _bb - _ab
  351
+    /// (_am*x) - (_bm*x) = _bb - _ab
  352
+    /// (_am - _bm)x = _bb - _ab
  353
+    /// x = (_bb - _ab) / (_am - _bm)
  354
+    float x = (_bb - _ab) / (_am - _bm);
  355
+    if (_am + _bm == 0.0f)
  356
+    {
  357
+        throw -1;
  358
+    }
  359
+
  360
+    /// y = mx + b
  361
+    float y = (_am * x) + _ab;
  362
+
  363
+    float _aIntersection = (x - _aStart.x) / (_adeltaX);
  364
+    float _bIntersection = (x - _bStart.x) / (_bdeltaX);
  365
+    if (_aIntersection < 0 || _aIntersection > 1 || !_aNeedsIntersect)
  366
+    {
  367
+        throw -1;
  368
+    }
  369
+    if (!_bNeedsIntersect || _bIntersection < 0 || _bIntersection > 1)
  370
+    {
  371
+        throw -1; /// Doesn't lie on the line
  372
+    }
  373
+    return {x, y};*/
  374
+}
  375
+
  376
+Vec2f positionFromScalarValue(Vec2f _start, Vec2f _end, float _scalar)
  377
+{
  378
+    return _start + ((_end - _start) * _scalar);
  379
+}
  380
+
  381
+
  382
+
  383
+
  384
+
  385
+
  386
+
  387
+
  388
+
  389
+
  390
+
  391
+
  392
+
  393
+
  394
+
  395
+
  396
+
  397
+
3  Types/Vec2f.h
@@ -104,6 +104,9 @@ bool b2IsValid(float32 x);
104 104
 
105 105
 //float operator*(Vec2f A, Vec2f B ){ return A.x*B.x + A.y*B.y; }
106 106
 
  107
+float intersectionValue(Vec2f _aStart, Vec2f _aEnd, Vec2f _bStart, Vec2f _bEnd); /// Returns the intersection point on _a
  108
+Vec2f intersectionPosition(Vec2f _aStart, Vec2f _aEnd, Vec2f _bStart, Vec2f _bEnd);
  109
+Vec2f positionFromScalarValue(Vec2f _start, Vec2f _end, float _scalar);
107 110
 inline Vec2f operator*(Vec2f a, float s ){ return Vec2f( a.x*s, a.y*s ); }
108 111
 
109 112
 std::ostream& operator<< (std::ostream &out, Vec2f &vec);
2  yaac
... ...
@@ -1 +1 @@
1  
-Subproject commit c104d17a0fe9798fdc628c1d661741124ddaa00c
  1
+Subproject commit d031bd000b9ef2bfad00abac61fd422d62e11d10

No commit comments for this range

Something went wrong with that request. Please try again.