浏览代码

CUBE VECTOR COLLISION

develop
Pierre-Antoine 'ZHAJOR' Tible 7 年前
父节点
当前提交
390534c782

二进制
TheGame/assets/objs/enterprise/NormalMap.png 查看文件


+ 14
- 0
TheGame/assets/objs/enterprise/USSEnterprise.mtl 查看文件

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
文件差异内容过多而无法显示
查看文件


二进制
TheGame/assets/objs/enterprise/UV Textur.png 查看文件


二进制
TheGame/assets/textures/rubiks.png 查看文件


+ 38
- 17
TheGame/renderwidget.cpp 查看文件

17
     setFocusPolicy(Qt::StrongFocus);
17
     setFocusPolicy(Qt::StrongFocus);
18
     setAutoFillBackground(false);
18
     setAutoFillBackground(false);
19
     setAutoBufferSwap(false);
19
     setAutoBufferSwap(false);
20
+    #ifdef __APPLE__
21
+    _assetsPath = "../../../assets/";
22
+    #else
23
+    _assetsPath = "./assets/";
24
+    #endif
20
 
25
 
21
     _engine = new UGameEngine(new OpenGLRenderDevice(this));
26
     _engine = new UGameEngine(new OpenGLRenderDevice(this));
22
     _camera = new FreeFlyCamera(_engine, this);
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
         UGEEntityCube* cube = new GameCube(_engine);
32
         UGEEntityCube* cube = new GameCube(_engine);
28
-        cube->setTextureId("test");
33
+        cube->setTextureId("rubiks");
29
 //        cube->rotate(Vector3D(0.0, 45.0, 45.0));
34
 //        cube->rotate(Vector3D(0.0, 45.0, 45.0));
30
         cube->move(Vector3D(0, i, i));
35
         cube->move(Vector3D(0, i, i));
31
 //        cube->setScale(Vector3D(1.0, 2.0, 1.0));
36
 //        cube->setScale(Vector3D(1.0, 2.0, 1.0));
33
         _entities.append(cube);
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
     WaveFrontObj* wavefrontObj = new WaveFrontObj(this);
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
     UGEEntityWaveFrontObj* obj = new UGEEntityWaveFrontObj(wavefrontObj, this);
55
     UGEEntityWaveFrontObj* obj = new UGEEntityWaveFrontObj(wavefrontObj, this);
56
+    obj->hide();
51
     _engine->addEntity(obj);
57
     _engine->addEntity(obj);
52
     _entities.append(obj);
58
     _entities.append(obj);
53
     animate();
59
     animate();
59
     _engine->setClearColor(Qt::gray);
65
     _engine->setClearColor(Qt::gray);
60
     _engine->initialize(70, width(), height());
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
 void RenderWidget::paintGL()
71
 void RenderWidget::paintGL()
77
 {
83
 {
78
     _camera->mousePressEvent(event);
84
     _camera->mousePressEvent(event);
79
     if (event->buttons() & Qt::LeftButton) {
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
         qDebug() << pos.getX() << pos.getY() << pos.getZ();
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 查看文件

43
     QList<UGEEntity*> _entities;
43
     QList<UGEEntity*> _entities;
44
 
44
 
45
     AbstractCamera* _camera;
45
     AbstractCamera* _camera;
46
+
47
+    QString _assetsPath;
46
 };
48
 };
47
 
49
 
48
 #endif // RENDERWIDGET_H
50
 #endif // RENDERWIDGET_H

+ 20
- 0
UGameEngine/cameras/abstractcamera.cpp 查看文件

40
 {
40
 {
41
     (void) event;
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 查看文件

25
 
25
 
26
     virtual void updateLookAt() = 0;
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
 protected:
34
 protected:
29
     UGameEngine* _engine;
35
     UGameEngine* _engine;
30
 
36
 
31
     QWidget* _widget;
37
     QWidget* _widget;
38
+
39
+    Vector3D _direction;
40
+
41
+    Vector3D _position;
32
 };
42
 };
33
 
43
 
34
 #endif // ABSTRACTCAMERA_H
44
 #endif // ABSTRACTCAMERA_H

+ 0
- 3
UGameEngine/cameras/freeflycamera.h 查看文件

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

+ 3
- 2
UGameEngine/cameras/rotationcamera.cpp 查看文件

50
 {
50
 {
51
     float theta = fabs(_theta / 180.0 * M_PI);
51
     float theta = fabs(_theta / 180.0 * M_PI);
52
     float phi = _phi / 180.0 * M_PI;
52
     float phi = _phi / 180.0 * M_PI;
53
-    Vector3D center = Vector3D(
53
+    _position = Vector3D(
54
                 _radius * sin(theta) * sin(phi),
54
                 _radius * sin(theta) * sin(phi),
55
                 _radius * cos(theta),
55
                 _radius * cos(theta),
56
                 _radius * sin(theta) * cos(phi)
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
 void RotationCamera::rotate(float phi, float theta)
62
 void RotationCamera::rotate(float phi, float theta)

+ 2
- 0
UGameEngine/engine/abstractrenderdevice.cpp 查看文件

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

+ 25
- 0
UGameEngine/entities/ugeentity.cpp 查看文件

6
     , _scale(1.0, 1.0, 1.0)
6
     , _scale(1.0, 1.0, 1.0)
7
     , _visible(true)
7
     , _visible(true)
8
     , _needUpdate(true)
8
     , _needUpdate(true)
9
+    , _zero(0.001)
9
 {
10
 {
10
     connect(this, SIGNAL(positionChanged()), this, SLOT(needUpdate()));
11
     connect(this, SIGNAL(positionChanged()), this, SLOT(needUpdate()));
11
     connect(this, SIGNAL(rotationChanged()), this, SLOT(needUpdate()));
12
     connect(this, SIGNAL(rotationChanged()), this, SLOT(needUpdate()));
232
 {
233
 {
233
     _needUpdate = true;
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 查看文件

61
 
61
 
62
     virtual Vector3D getVectorNearestIntesection(const Vector3D &vector, const Vector3D &pos, bool* ok) = 0;
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
     void update();
68
     void update();
65
 
69
 
66
     virtual void onUpdate();
70
     virtual void onUpdate();
102
 
106
 
103
     Matrix3x3 _tranformation;
107
     Matrix3x3 _tranformation;
104
 
108
 
109
+    double _zero;
110
+
105
 };
111
 };
106
 
112
 
107
 #endif // UGEENTITY_H
113
 #endif // UGEENTITY_H

+ 21
- 1
UGameEngine/entities/ugeentitycube.cpp 查看文件

114
 Vector3D UGEEntityCube::getVectorNearestIntesection(const Vector3D &vector, const Vector3D &origin, bool *ok)
114
 Vector3D UGEEntityCube::getVectorNearestIntesection(const Vector3D &vector, const Vector3D &origin, bool *ok)
115
 {
115
 {
116
     *ok = false;
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
 //    Vector3D inv(1.0 / vector.getX(), 1.0 / vector.getY(), 1.0 / vector.getZ());
138
 //    Vector3D inv(1.0 / vector.getX(), 1.0 / vector.getY(), 1.0 / vector.getZ());
119
 //    float tmin, tmax, tymin, tymax, tzmin, tzmax;
139
 //    float tmin, tmax, tymin, tymax, tzmin, tzmax;
120
 
140
 

正在加载...
取消
保存