forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathspx_physic_mgr.cpp
More file actions
336 lines (286 loc) · 12.2 KB
/
spx_physic_mgr.cpp
File metadata and controls
336 lines (286 loc) · 12.2 KB
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/**************************************************************************/
/* spx_physic_mgr.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* 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. */
/**************************************************************************/
#include "spx_physic_mgr.h"
#include "core/templates/hash_set.h"
#include "core/variant/typed_array.h"
#include "gdextension_spx_ext.h"
#include "scene/resources/world_2d.h"
#include "servers/physics_server_2d.h"
#include "servers/physics_server_3d.h"
#include "spx_sprite.h"
#include "spx_sprite_mgr.h"
#include "scene/2d/camera_2d.h"
#include "scene/2d/physics/collision_shape_2d.h"
#include "scene/main/window.h"
#include "scene/resources/2d/circle_shape_2d.h"
#include "scene/2d/physics/area_2d.h"
#include "scene/resources/2d/rectangle_shape_2d.h"
#include "spx_engine.h"
#include "spx_sprite_mgr.h"
#define spriteMgr SpxEngine::get_singleton()->get_sprite()
GdFloat SpxPhysicDefine::global_gravity = 1.0;
GdFloat SpxPhysicDefine::global_friction = 1.0;
GdFloat SpxPhysicDefine::global_air_drag = 1.0;
GdArray SpxRaycastInfo::ToArray(){
GdArray result_array = SpxBaseMgr::create_array(GD_ARRAY_TYPE_INT64, 6);
SpxBaseMgr::set_array(result_array,0,(GdInt)collide);
SpxBaseMgr::set_array(result_array,1,(GdInt)sprite_gid);
SpxBaseMgr::set_array(result_array,2,spx_float_to_int(position.x));
SpxBaseMgr::set_array(result_array,3,spx_float_to_int(position.y));
SpxBaseMgr::set_array(result_array,4,spx_float_to_int(normal.x));
SpxBaseMgr::set_array(result_array,5,spx_float_to_int(normal.y));
return result_array;
}
void SpxPhysicDefine::set_global_gravity(GdFloat gravity) {
SpxPhysicDefine::global_gravity = gravity;
}
GdFloat SpxPhysicDefine::get_global_gravity() {
return SpxPhysicDefine::global_gravity;
}
void SpxPhysicDefine::set_global_friction(GdFloat friction) {
SpxPhysicDefine::global_friction = friction;
}
GdFloat SpxPhysicDefine::get_global_friction() {
return SpxPhysicDefine::global_friction;
}
void SpxPhysicDefine::set_global_air_drag(GdFloat air_drag) {
SpxPhysicDefine::global_air_drag = air_drag;
}
GdFloat SpxPhysicDefine::get_global_air_drag() {
return SpxPhysicDefine::global_air_drag ;
}
void SpxPhysicMgr::on_awake() {
SpxBaseMgr::on_awake();
is_collision_by_pixel = true;
}
void SpxPhysicMgr::on_reset(int reset_code) {
}
SpxRaycastInfo SpxPhysicMgr::_raycast(GdVec2 from, GdVec2 to,GdArray ignore_sprites,GdInt collision_mask,GdBool collide_with_areas,GdBool collide_with_bodies) {
SpxRaycastInfo info;
info.collide = false;
info.position = GdVec2{0, 0};
info.normal = GdVec2{0, 0};
info.sprite_gid = 0;
HashSet<RID> ignore_set;
if(ignore_sprites && ignore_sprites->size > 0){
GdObj* sprite_data = (SpxBaseMgr::get_array<GdObj>(ignore_sprites, 0));
for (int i = 0; i < ignore_sprites->size; i++) {
auto obj = sprite_data[i];
auto sprite = spriteMgr->get_sprite(obj);
if(sprite != nullptr){
ignore_set.insert(sprite->get_rid());
auto trigger = sprite->get_area2d();
if(trigger != nullptr){
ignore_set.insert(trigger->get_rid());
}
}
}
}
PhysicsDirectSpaceState2D::RayResult result;
PhysicsDirectSpaceState2D::RayParameters params;
params.from = from;
params.to = to;
params.collision_mask = (uint32_t)collision_mask;
params.collide_with_areas = collide_with_areas;
params.collide_with_bodies = collide_with_bodies;
params.exclude = ignore_set;
auto node = (Node2D *)get_root();
PhysicsDirectSpaceState2D *space_state = node->get_world_2d()->get_direct_space_state();
if (!space_state) {
return info;
}
bool hit = space_state->intersect_ray(params, result);
if (!hit) {
return info;
}
SpxSprite *collider = dynamic_cast<SpxSprite *>(result.collider);
GdObj current_gid = collider ? collider->get_gid() : 0;
info.collide = true;
info.position = result.position;
info.normal = result.normal;
info.sprite_gid = current_gid;
return info;
}
GdArray SpxPhysicMgr::raycast_with_details(GdVec2 from, GdVec2 to,GdArray ignore_sprites,GdInt collision_mask,GdBool collide_with_areas,GdBool collide_with_bodies){
SpxRaycastInfo info = _raycast(from, to, ignore_sprites, collision_mask, collide_with_areas, collide_with_bodies);
return info.ToArray();
}
GdObj SpxPhysicMgr::raycast(GdVec2 from, GdVec2 to, GdInt collision_mask) {
auto node = (Node2D *)get_root();
PhysicsDirectSpaceState2D *space_state = node->get_world_2d()->get_direct_space_state();
PhysicsDirectSpaceState2D::RayResult result;
PhysicsDirectSpaceState2D::RayParameters params;
params.from = from;
params.to = to;
params.collision_mask = (uint32_t)collision_mask;
bool hit = space_state->intersect_ray(params, result);
if (hit) {
SpxSprite *collider = dynamic_cast<SpxSprite *>(result.collider);
if (collider != nullptr) {
return collider->get_gid();
}
}
return 0;
}
GdBool SpxPhysicMgr::check_collision(GdVec2 from, GdVec2 to, GdInt collision_mask, GdBool collide_with_areas, GdBool collide_with_bodies) {
auto node = (Node2D *)get_root();
PhysicsDirectSpaceState2D *space_state = node->get_world_2d()->get_direct_space_state();
PhysicsDirectSpaceState2D::RayResult result;
PhysicsDirectSpaceState2D::RayParameters params;
params.from = from;
params.to = to;
params.collision_mask = (uint32_t)collision_mask;
params.collide_with_areas = collide_with_areas;
params.collide_with_bodies = collide_with_bodies;
bool hit = space_state->intersect_ray(params, result);
return hit;
}
const GdInt BOUND_CAM_LEFT = 1 << 0;
const GdInt BOUND_CAM_TOP = 1 << 1;
const GdInt BOUND_CAM_RIGHT = 1 << 2;
const GdInt BOUND_CAM_BOTTOM = 1 << 3;
GdInt SpxPhysicMgr::check_touched_camera_boundaries(GdObj obj) {
auto sprite = spriteMgr->get_sprite(obj);
if (sprite == nullptr) {
print_error("try to get property of a null sprite gid=" + itos(obj));
return false;
}
Transform2D sprite_transform = sprite->get_global_transform();
CollisionShape2D *collision_shape = sprite->get_trigger();
if (!collision_shape) {
return false;
}
Ref<Shape2D> sprite_shape = collision_shape->get_shape();
if (sprite_shape.is_null()) {
return false;
}
Camera2D *camera = get_tree()->get_root()->get_camera_2d();
if (!camera) {
return false;
}
Transform2D camera_transform = camera->get_global_transform();
Vector2 viewport_size = camera->get_viewport_rect().size;
Vector2 zoom = camera->get_zoom();
Vector2 half_size = (viewport_size / zoom) * 0.5;
Vector2 camera_position = camera_transform.get_origin();
Ref<RectangleShape2D> vertical_edge_shape;
vertical_edge_shape.instantiate();
vertical_edge_shape->set_size(Vector2(2, half_size.y*50));// mutil by 50 is to check the case of some collider is out of boundary
Ref<RectangleShape2D> horizontal_edge_shape;
horizontal_edge_shape.instantiate();
horizontal_edge_shape->set_size(Vector2(half_size.x*50, 2));
Transform2D left_edge_transform(0, camera_position + Vector2(-half_size.x, 0));
Transform2D right_edge_transform(0, camera_position + Vector2(half_size.x, 0));
Transform2D top_edge_transform(0, camera_position + Vector2(0, -half_size.y));
Transform2D bottom_edge_transform(0, camera_position + Vector2(0, half_size.y));
bool is_colliding_left = sprite_shape->collide(sprite_transform, vertical_edge_shape, left_edge_transform);
bool is_colliding_right = sprite_shape->collide(sprite_transform, vertical_edge_shape, right_edge_transform);
bool is_colliding_top = sprite_shape->collide(sprite_transform, horizontal_edge_shape, top_edge_transform);
bool is_colliding_bottom = sprite_shape->collide(sprite_transform, horizontal_edge_shape, bottom_edge_transform);
GdInt result = 0;
result += is_colliding_top ? BOUND_CAM_TOP : 0;
result += is_colliding_right ? BOUND_CAM_RIGHT : 0;
result += is_colliding_bottom ? BOUND_CAM_BOTTOM : 0;
result += is_colliding_left ? BOUND_CAM_LEFT : 0;
return result;
}
GdBool SpxPhysicMgr::check_touched_camera_boundary(GdObj obj, GdInt board_type) {
auto result = check_touched_camera_boundaries(obj);
return (result & board_type) != 0;
}
//
void SpxPhysicMgr::set_collision_system_type(GdBool is_collision_by_alpha) {
this->is_collision_by_pixel = is_collision_by_alpha;
}
void SpxPhysicMgr::set_global_gravity(GdFloat gravity) {
SpxPhysicDefine::set_global_gravity(gravity);
}
GdFloat SpxPhysicMgr::get_global_gravity() {
return SpxPhysicDefine::get_global_gravity();
}
void SpxPhysicMgr::set_global_friction(GdFloat friction) {
SpxPhysicDefine::set_global_friction(friction);
}
GdFloat SpxPhysicMgr::get_global_friction() {
return SpxPhysicDefine::get_global_friction();
}
void SpxPhysicMgr::set_global_air_drag(GdFloat air_drag) {
SpxPhysicDefine::set_global_air_drag(air_drag);
}
GdFloat SpxPhysicMgr::get_global_air_drag() {
return SpxPhysicDefine::get_global_air_drag();
}
GdArray SpxPhysicMgr::_check_collision(RID shape, GdVec2 pos, GdInt collision_mask){
auto node = (Node2D *)get_root();
PhysicsDirectSpaceState2D *space_state = node->get_world_2d()->get_direct_space_state();
if (!space_state) {
return create_array(GD_ARRAY_TYPE_GDOBJ, 0);
}
Transform2D query_transform(0, pos);
PhysicsDirectSpaceState2D::ShapeParameters params;
params.shape_rid = shape;
params.transform = query_transform;
params.collision_mask = (uint32_t)collision_mask;
params.collide_with_areas = true;
params.collide_with_bodies = true;
params.motion = Vector2(0, 0);
params.margin = 0.0;
PhysicsDirectSpaceState2D::ShapeResult results[32];
int result_count = space_state->intersect_shape(params, results, 32);
Array resultIds;
for (int i = 0; i < result_count; i++) {
Object *collider_obj = results[i].collider;
if (collider_obj) {
SpxSprite *sprite = dynamic_cast<SpxSprite *>(collider_obj);
if (sprite) {
resultIds.push_back(sprite->get_gid());
}
}
}
int valid_count = resultIds.size();
GdArray result_array = create_array(GD_ARRAY_TYPE_GDOBJ, valid_count);
for(int i=0; i < valid_count; i++){
auto val=(GdObj)resultIds.get(i);
set_array(result_array,i,val);
}
return result_array;
}
GdArray SpxPhysicMgr::check_collision_rect(GdVec2 pos, GdVec2 size,GdInt collision_mask) {
Ref<RectangleShape2D> rect_shape;
rect_shape.instantiate();
rect_shape->set_size(size);
return _check_collision(rect_shape->get_rid(), pos, collision_mask);
}
GdArray SpxPhysicMgr::check_collision_circle(GdVec2 pos, GdFloat radius,GdInt collision_mask) {
Ref<CircleShape2D> circle_shape;
circle_shape.instantiate();
circle_shape->set_radius(radius);
return _check_collision(circle_shape->get_rid(), pos, collision_mask);
}