Procházet zdrojové kódy

CUBE VECTOR COLLISION

develop
Pierre-Antoine 'ZHAJOR' Tible před 8 roky
rodič
revize
390534c782

binární
TheGame/assets/objs/enterprise/NormalMap.png Zobrazit soubor


+ 14
- 0
TheGame/assets/objs/enterprise/USSEnterprise.mtl Zobrazit soubor

@@ -0,0 +1,14 @@
1
+# Blender MTL File: 'USSEnterprise.blend'
2
+# Material Count: 1
3
+
4
+newmtl Material
5
+Ns 96.078431
6
+Ka 0.000000 0.000000 0.000000
7
+Kd 0.800000 0.800000 0.800000
8
+Ks 0.800000 0.800000 0.800000
9
+Ni 1.000000
10
+d 1.000000
11
+illum 2
12
+map_Kd /home/robin/Downloads/UV Maps/UV Textur.png
13
+map_Bump /home/robin/Downloads/UV Maps/NormalMap.png
14
+map_Ks /home/robin/Downloads/UV Maps/UV Textur.png

+ 8888
- 0
TheGame/assets/objs/enterprise/USSEnterprise.obj
Diff nebyl zobrazen, protože je příliš veliký
Zobrazit soubor


binární
TheGame/assets/objs/enterprise/UV Textur.png Zobrazit soubor


binární
TheGame/assets/textures/rubiks.png Zobrazit soubor


+ 38
- 17
TheGame/renderwidget.cpp Zobrazit soubor

@@ -17,15 +17,20 @@ RenderWidget::RenderWidget(QWidget *parent) :
17 17
     setFocusPolicy(Qt::StrongFocus);
18 18
     setAutoFillBackground(false);
19 19
     setAutoBufferSwap(false);
20
+    #ifdef __APPLE__
21
+    _assetsPath = "../../../assets/";
22
+    #else
23
+    _assetsPath = "./assets/";
24
+    #endif
20 25
 
21 26
     _engine = new UGameEngine(new OpenGLRenderDevice(this));
22 27
     _camera = new FreeFlyCamera(_engine, this);
23 28
 
24
-    _engine->addEntity(new UGEEntityAxes(_engine));
29
+    //_engine->addEntity(new UGEEntityAxes(_engine));
25 30
 
26
-    for (int i = 0; i < 1000; ++i) {
31
+    for (int i = 0; i < 1; ++i) {
27 32
         UGEEntityCube* cube = new GameCube(_engine);
28
-        cube->setTextureId("test");
33
+        cube->setTextureId("rubiks");
29 34
 //        cube->rotate(Vector3D(0.0, 45.0, 45.0));
30 35
         cube->move(Vector3D(0, i, i));
31 36
 //        cube->setScale(Vector3D(1.0, 2.0, 1.0));
@@ -33,21 +38,22 @@ RenderWidget::RenderWidget(QWidget *parent) :
33 38
         _entities.append(cube);
34 39
     }
35 40
 
36
-    for(int i = 0,j = 0; i * j< 625;){
37
-        UGEEntityCube* cube = new GameCube(_engine);
38
-        cube->setTextureId("test");
39
-        cube->move(Vector3D(i, 0, j++));
40
-        _engine->addEntity(cube);
41
-        _entities.append(cube);
42
-        if(j == 26){
43
-            j = 0;
44
-            i++;
45
-        }
46
-    }
41
+//    for(int i = 0,j = 0; i * j< 626;){
42
+//        UGEEntityCube* cube = new GameCube(_engine);
43
+//        cube->setTextureId("rubiks");
44
+//        cube->move(Vector3D(i, 0, j++));
45
+//        _engine->addEntity(cube);
46
+//        _entities.append(cube);
47
+//        if(j == 26){
48
+//            j = 0;
49
+//            i++;
50
+//        }
51
+//    }
47 52
 
48 53
     WaveFrontObj* wavefrontObj = new WaveFrontObj(this);
49
-    wavefrontObj->openFile("/home/robin/Downloads/enterprise/obj/USSEnterprise.obj");
54
+    wavefrontObj->openFile(_assetsPath + "objs/enterprise/USSEnterprise.obj");
50 55
     UGEEntityWaveFrontObj* obj = new UGEEntityWaveFrontObj(wavefrontObj, this);
56
+    obj->hide();
51 57
     _engine->addEntity(obj);
52 58
     _entities.append(obj);
53 59
     animate();
@@ -59,7 +65,7 @@ void RenderWidget::initializeGL()
59 65
     _engine->setClearColor(Qt::gray);
60 66
     _engine->initialize(70, width(), height());
61 67
 
62
-    _engine->loadTextureFromFile("test", "/home/robin/Downloads/test.png");
68
+    _engine->loadTextureFromFile("rubiks", _assetsPath + "textures/rubiks.png");
63 69
 }
64 70
 
65 71
 void RenderWidget::paintGL()
@@ -77,8 +83,23 @@ void RenderWidget::mousePressEvent(QMouseEvent *event)
77 83
 {
78 84
     _camera->mousePressEvent(event);
79 85
     if (event->buttons() & Qt::LeftButton) {
80
-        Vector3D pos = _engine->get3DFrom2D(Vector2D(event->x(), height() - event->y()));
86
+//        Vector3D pos = _engine->get3DFrom2D(Vector2D(event->x(), height() - event->y()));
87
+        Vector3D pos = _camera->getDirection();
81 88
         qDebug() << pos.getX() << pos.getY() << pos.getZ();
89
+        Vector3D bestp;
90
+        bool ok;
91
+        for (int i = 0; i < _engine->getEntities().size(); i++) {
92
+            UGEEntity* entity = _engine->getEntities()[i];
93
+            bool provi;
94
+            Vector3D collision = entity->getVectorNearestIntesection(_camera->getDirection(), _camera->getPosition(), &provi);
95
+            if (provi) {
96
+                bestp = collision;
97
+                UGEEntityAxes* axe = new UGEEntityAxes(_engine);
98
+                axe->move(bestp);
99
+                _engine->addEntity(axe);
100
+                ok = true;
101
+            }
102
+        }
82 103
     }
83 104
 }
84 105
 

+ 2
- 0
TheGame/renderwidget.h Zobrazit soubor

@@ -43,6 +43,8 @@ private:
43 43
     QList<UGEEntity*> _entities;
44 44
 
45 45
     AbstractCamera* _camera;
46
+
47
+    QString _assetsPath;
46 48
 };
47 49
 
48 50
 #endif // RENDERWIDGET_H

+ 20
- 0
UGameEngine/cameras/abstractcamera.cpp Zobrazit soubor

@@ -40,4 +40,24 @@ void AbstractCamera::keyReleaseEvent(QKeyEvent* event)
40 40
 {
41 41
     (void) event;
42 42
 }
43
+Vector3D AbstractCamera::getDirection() const
44
+{
45
+    return _direction;
46
+}
47
+
48
+void AbstractCamera::setDirection(const Vector3D &direction)
49
+{
50
+    _direction = direction;
51
+}
52
+Vector3D AbstractCamera::getPosition() const
53
+{
54
+    return _position;
55
+}
56
+
57
+void AbstractCamera::setPosition(const Vector3D &position)
58
+{
59
+    _position = position;
60
+}
61
+
62
+
43 63
 

+ 10
- 0
UGameEngine/cameras/abstractcamera.h Zobrazit soubor

@@ -25,10 +25,20 @@ public:
25 25
 
26 26
     virtual void updateLookAt() = 0;
27 27
 
28
+    Vector3D getDirection() const;
29
+    void setDirection(const Vector3D &getDirection);
30
+
31
+    Vector3D getPosition() const;
32
+    void setPosition(const Vector3D &getPosition);
33
+
28 34
 protected:
29 35
     UGameEngine* _engine;
30 36
 
31 37
     QWidget* _widget;
38
+
39
+    Vector3D _direction;
40
+
41
+    Vector3D _position;
32 42
 };
33 43
 
34 44
 #endif // ABSTRACTCAMERA_H

+ 0
- 3
UGameEngine/cameras/freeflycamera.h Zobrazit soubor

@@ -19,9 +19,6 @@ public:
19 19
     void rotate(float phi, float theta);
20 20
 
21 21
 private:
22
-    Vector3D _direction;
23
-
24
-    Vector3D _position;
25 22
 
26 23
     Vector2D _speedVector;
27 24
 

+ 3
- 2
UGameEngine/cameras/rotationcamera.cpp Zobrazit soubor

@@ -50,12 +50,13 @@ void RotationCamera::updateLookAt()
50 50
 {
51 51
     float theta = fabs(_theta / 180.0 * M_PI);
52 52
     float phi = _phi / 180.0 * M_PI;
53
-    Vector3D center = Vector3D(
53
+    _position = Vector3D(
54 54
                 _radius * sin(theta) * sin(phi),
55 55
                 _radius * cos(theta),
56 56
                 _radius * sin(theta) * cos(phi)
57 57
                 );
58
-    _engine->lookAt(center, Vector3D(0.0f, 0.0f, 0.0f));
58
+    _direction = -_position;
59
+    _engine->lookAt(_position, Vector3D(0.0f, 0.0f, 0.0f));
59 60
 }
60 61
 
61 62
 void RotationCamera::rotate(float phi, float theta)

+ 2
- 0
UGameEngine/engine/abstractrenderdevice.cpp Zobrazit soubor

@@ -1,5 +1,6 @@
1 1
 #include "abstractrenderdevice.h"
2 2
 #include <QImage>
3
+#include <QDir>
3 4
 
4 5
 AbstractRenderDevice::AbstractRenderDevice(QObject *parent) :
5 6
     QObject(parent)
@@ -46,6 +47,7 @@ void AbstractRenderDevice::lookAt(const Vector3D &eye, const Vector3D &center, c
46 47
 void AbstractRenderDevice::loadTextureFromFile(const QVariant &id, const QString &filename)
47 48
 {
48 49
     QImage img;
50
+    qDebug() << QDir::currentPath() << filename;
49 51
     if (img.load(filename)) {
50 52
         loadTexture(id, img);
51 53
     }

+ 25
- 0
UGameEngine/entities/ugeentity.cpp Zobrazit soubor

@@ -6,6 +6,7 @@ UGEEntity::UGEEntity(QObject *parent)
6 6
     , _scale(1.0, 1.0, 1.0)
7 7
     , _visible(true)
8 8
     , _needUpdate(true)
9
+    , _zero(0.001)
9 10
 {
10 11
     connect(this, SIGNAL(positionChanged()), this, SLOT(needUpdate()));
11 12
     connect(this, SIGNAL(rotationChanged()), this, SLOT(needUpdate()));
@@ -232,3 +233,27 @@ void UGEEntity::needUpdate()
232 233
 {
233 234
     _needUpdate = true;
234 235
 }
236
+
237
+
238
+Vector3D UGEEntity::getVectorNearestFaceIntersection(const Vector3D & p0, const Vector3D &p1, const Vector3D &p2, const Vector3D &vector, const Vector3D &pos, bool* ok){
239
+    Vector3D v1 = p1 - p0;
240
+    Vector3D v2 = p2 - p0;
241
+    Vector3D n = v1.crossProduct(v2);
242
+    double dotProduct = vector.dotProduct(n);
243
+    if(isZero(dotProduct)){
244
+        *ok = false;
245
+        return Vector3D();
246
+    }
247
+    double l2 = n.dotProduct(p0 - pos) / dotProduct;
248
+    if(-_zero > l2){
249
+        *ok = false;
250
+        return Vector3D();
251
+    }
252
+    *ok = true;
253
+    Vector3D unit = (vector / vector.norm()) * l2;
254
+    return pos + unit;
255
+}
256
+
257
+bool UGEEntity::isZero(double v){
258
+    return v < _zero && v > -_zero;
259
+}

+ 6
- 0
UGameEngine/entities/ugeentity.h Zobrazit soubor

@@ -61,6 +61,10 @@ public:
61 61
 
62 62
     virtual Vector3D getVectorNearestIntesection(const Vector3D &vector, const Vector3D &pos, bool* ok) = 0;
63 63
 
64
+    Vector3D getVectorNearestFaceIntersection(const Vector3D & p0, const Vector3D &p1, const Vector3D &p2, const Vector3D &vector, const Vector3D &pos, bool* ok);
65
+
66
+    bool isZero(double v);
67
+
64 68
     void update();
65 69
 
66 70
     virtual void onUpdate();
@@ -102,6 +106,8 @@ protected:
102 106
 
103 107
     Matrix3x3 _tranformation;
104 108
 
109
+    double _zero;
110
+
105 111
 };
106 112
 
107 113
 #endif // UGEENTITY_H

+ 21
- 1
UGameEngine/entities/ugeentitycube.cpp Zobrazit soubor

@@ -114,7 +114,27 @@ void UGEEntityCube::onUpdate()
114 114
 Vector3D UGEEntityCube::getVectorNearestIntesection(const Vector3D &vector, const Vector3D &origin, bool *ok)
115 115
 {
116 116
     *ok = false;
117
-    return Vector3D();
117
+    //    Vector3D p1 = points[1] - p;
118
+    //    Vector3D p2 = points[1];
119
+    //    Vector3D n = p1.crossProduct(p2);
120
+    Vector3D bestP;
121
+    for (int i = 0; i < _facesTexture.size(); ++i){
122
+        bool provi = false;
123
+        QList<TextureVector3D> face = _facesTexture[i];
124
+        Vector3D pinter = getVectorNearestFaceIntersection(face[0], face[1], face[2], vector, origin, &provi);
125
+        if (provi) {
126
+            bool isNearest = (origin - pinter).norm() < (origin - bestP).norm();
127
+            bool isInFace = (_position - face[0]).norm() >= (_position - pinter).norm();
128
+            if (isInFace && (!(*ok) || isNearest)) {
129
+                bestP = pinter;
130
+                *ok = true;
131
+            }
132
+            qDebug() << bestP.getX() << bestP.getY() << bestP.getZ();
133
+        }
134
+    }
135
+    return bestP;
136
+
137
+
118 138
 //    Vector3D inv(1.0 / vector.getX(), 1.0 / vector.getY(), 1.0 / vector.getZ());
119 139
 //    float tmin, tmax, tymin, tymax, tzmin, tzmax;
120 140
 

Načítá se…
Zrušit
Uložit