forked from erayzesen/QuarkPhysics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qexamplescene.cpp
208 lines (171 loc) · 6.29 KB
/
qexamplescene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/************************************************************************************
* MIT License
*
* Copyright (c) 2023 Eray Zesen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* https://github.com/erayzesen/QuarkPhysics
*
**************************************************************************************/
#include "qexamplescene.h"
#include "QuarkPhysics/qmesh.h"
#include "QuarkPhysics/extensions/qspatialhashing.h"
QExampleScene::QExampleScene(QVector sceneSize)
{
world=new QWorld();
//Trying QSpatialHashing broadphase extension
/* QSpatialHashing *broadPhase=new QSpatialHashing(world->bodies,128.0f);
world->SetBroadphase(broadPhase); */
this->sceneSize=sceneSize;
}
QRigidBody* QExampleScene::AddRectBody(float posX, float posY, float width, float height)
{
QRigidBody *body=new QRigidBody();
body->AddMesh(QMesh::CreateWithRect(QVector(width,height),QVector(0.0f,0.0f) ) )->SetPosition(QVector(posX,posY));
world->AddBody(body);
return body;
}
QRigidBody* QExampleScene::AddCircleBody(float posX, float posY, float radius)
{
QRigidBody *body=new QRigidBody();
body->AddMesh(QMesh::CreateWithCircle(radius,QVector::Zero() ) )->SetPosition(QVector(posX,posY));
world->AddBody(body);
return body;
}
QRigidBody *QExampleScene::AddPolygonBody(float posX, float posY, float radius, int sideCount)
{
QRigidBody *body=new QRigidBody();
body->AddMesh(QMesh::CreateWithPolygon(radius,sideCount,QVector::Zero()) )->SetPosition(QVector(posX,posY));
world->AddBody(body);
return body;
}
QRigidBody* QExampleScene::AddRectBody(float posX, float posY)
{
return AddRectBody(posX,posY,spawnRectSize,spawnRectSize);
}
QRigidBody* QExampleScene::AddCircleBody(float posX, float posY)
{
return AddCircleBody(posX,posY,spawnCircleRadius);
}
QRigidBody *QExampleScene::AddPolygonBody(float posX, float posY, int sideCount)
{
return AddPolygonBody(posX,posY,spawnCircleRadius,sideCount);
}
QSoftBody *QExampleScene::AddBlobBody(float posX, float posY, float radius, int sideCount)
{
QSoftBody *blob=new QSoftBody();
blob->AddMesh(QMesh::CreateWithPolygon(radius,sideCount,QVector(0,0)))->SetPosition(QVector(posX,posY));
blob->SetAreaPreservingEnabled(true);
blob->SetAreaPreservingRate(0.8f);
//blob->SetRigidity(0.5f);
world->AddBody(blob);
return blob;
}
void QExampleScene::CreateSceneBorders()
{
QVector hBoxSizes=QVector(128,sceneSize.y);
QVector vBoxSizes=QVector(sceneSize.x,128);
QRigidBody *bodyLeft=new QRigidBody();
bodyLeft->AddMesh( QMesh::CreateWithRect(hBoxSizes) )->SetPosition(QVector(-hBoxSizes.x*0.5f,hBoxSizes.y*0.5f));
bodyLeft->SetMode(QBody::STATIC);
bodyLeft->SetRestitution(1.0f);
world->AddBody(bodyLeft);
QRigidBody *bodyTop=new QRigidBody();
bodyTop->AddMesh( QMesh::CreateWithRect(vBoxSizes) )->SetPosition(QVector(vBoxSizes.x*0.5f,-vBoxSizes.y*0.5f));
bodyTop->SetMode(QBody::STATIC);
bodyTop->SetRestitution(1.0f);
world->AddBody(bodyTop);
QRigidBody *bodyRight=new QRigidBody();
bodyRight->AddMesh( QMesh::CreateWithRect(hBoxSizes) )->SetPosition(QVector(sceneSize.x+hBoxSizes.x*0.5f,hBoxSizes.y*0.5f));
bodyRight->SetMode(QBody::STATIC);
bodyRight->SetRestitution(1.0f);
world->AddBody(bodyRight);
QRigidBody *bodyBottom=new QRigidBody();
bodyBottom->AddMesh( QMesh::CreateWithRect(vBoxSizes) )->SetPosition(QVector(vBoxSizes.x*0.5f,sceneSize.y+vBoxSizes.y*0.5f));
bodyBottom->SetMode(QBody::STATIC);
bodyBottom->SetRestitution(1.0f);
world->AddBody(bodyBottom);
}
int QExampleScene::RandomRange(int rangeMin, int rangeMax)
{
int res=rangeMin+rand()%rangeMax;
return res;
}
//###This side is also an example about controlling objects with mouse
void QExampleScene::CreateJointOrSpringBetweenMouseAndBody(QVector mousePosition){
if(mouseJoint!=nullptr){
world->RemoveJoint(mouseJoint);
delete mouseJoint;
mouseJoint=nullptr;
}
if(mouseSpring!=nullptr){
world->RemoveSpring(mouseSpring);
delete mouseSpring;
mouseSpring=nullptr;
}
QRigidBody *targetBody=nullptr;
QParticle *targetParticle=nullptr;
vector<QBody*> hitBodies=world->GetBodiesHitByPoint(mousePosition,1,true);
if(hitBodies.size()>0){
if(hitBodies[0]->GetMode()!=QBody::STATIC){
targetBody=static_cast<QRigidBody*>(hitBodies[0]);
}
}
if(targetBody!=nullptr){
mouseJoint=new QJoint(targetBody,mousePosition,nullptr);
world->AddJoint(mouseJoint);
}else{
vector<QParticle*> hitParticles=world->GetParticlesCloseToPoint(mousePosition,8.0f,1,true);
if(hitParticles.size()>0){
targetParticle=hitParticles[0];
}
}
if(targetParticle!=nullptr){
mouseSpring=new QSpring(targetParticle,&mousePointParticle,0.0f);
mouseSpring->SetRigidity(1.0f);
world->AddSpring(mouseSpring);
}
}
void QExampleScene::OnMousePressed(QVector mousePosition){
CreateJointOrSpringBetweenMouseAndBody(mousePosition);
}
void QExampleScene::OnMouseReleased(QVector mousePosition){
if(mouseJoint!=nullptr){
world->RemoveJoint(mouseJoint);
delete mouseJoint;
mouseJoint=nullptr;
}
if(mouseSpring!=nullptr){
world->RemoveSpring(mouseSpring);
delete mouseSpring;
mouseSpring=nullptr;
}
}
void QExampleScene::OnMouseMoved(QVector mousePosition){
if(mouseJoint!=nullptr){
mouseJoint->SetAnchorBPosition(mousePosition);
}
mousePointParticle.SetGlobalPosition(mousePosition);
}
void QExampleScene::OnKeyPressed(sf::Keyboard::Key key)
{
}
void QExampleScene::OnKeyReleased(sf::Keyboard::Key key)
{
}