godot/servers/physics/space_sw.cpp

679 lines
22 KiB
C++
Raw Normal View History

2015-10-08 20:00:40 +02:00
/*************************************************************************/
/* space_sw.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */
2015-10-08 20:00:40 +02:00
/* */
/* 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 "space_sw.h"
#include "collision_solver_sw.h"
#include "global_config.h"
2015-10-08 20:00:40 +02:00
#include "physics_server_sw.h"
2017-06-13 17:45:01 +02:00
_FORCE_INLINE_ static bool _match_object_type_query(CollisionObjectSW *p_object, uint32_t p_collision_layer, uint32_t p_type_mask) {
2015-10-08 20:00:40 +02:00
if (p_object->get_type() == CollisionObjectSW::TYPE_AREA)
return p_type_mask & PhysicsDirectSpaceState::TYPE_MASK_AREA;
2015-10-08 20:00:40 +02:00
2017-06-13 17:45:01 +02:00
if ((p_object->get_collision_layer() & p_collision_layer) == 0)
2015-10-08 20:00:40 +02:00
return false;
BodySW *body = static_cast<BodySW *>(p_object);
2015-10-08 20:00:40 +02:00
return (1 << body->get_mode()) & p_type_mask;
2015-10-08 20:00:40 +02:00
}
2017-06-13 17:45:01 +02:00
bool PhysicsDirectSpaceStateSW::intersect_ray(const Vector3 &p_from, const Vector3 &p_to, RayResult &r_result, const Set<RID> &p_exclude, uint32_t p_collision_layer, uint32_t p_object_type_mask, bool p_pick_ray) {
2015-10-08 20:00:40 +02:00
ERR_FAIL_COND_V(space->locked, false);
2015-10-08 20:00:40 +02:00
Vector3 begin, end;
2015-10-08 20:00:40 +02:00
Vector3 normal;
begin = p_from;
end = p_to;
normal = (end - begin).normalized();
2015-10-08 20:00:40 +02:00
int amount = space->broadphase->cull_segment(begin, end, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
2015-10-08 20:00:40 +02:00
//todo, create another array tha references results, compute AABBs and check closest point to ray origin, sort, and stop evaluating results when beyond first collision
bool collided = false;
Vector3 res_point, res_normal;
2015-10-08 20:00:40 +02:00
int res_shape;
const CollisionObjectSW *res_obj;
real_t min_d = 1e10;
2015-10-08 20:00:40 +02:00
for (int i = 0; i < amount; i++) {
2015-10-08 20:00:40 +02:00
2017-06-13 17:45:01 +02:00
if (!_match_object_type_query(space->intersection_query_results[i], p_collision_layer, p_object_type_mask))
2015-10-08 20:00:40 +02:00
continue;
if (p_pick_ray && !(static_cast<CollisionObjectSW *>(space->intersection_query_results[i])->is_ray_pickable()))
2015-10-08 20:00:40 +02:00
continue;
if (p_exclude.has(space->intersection_query_results[i]->get_self()))
2015-10-08 20:00:40 +02:00
continue;
const CollisionObjectSW *col_obj = space->intersection_query_results[i];
2015-10-08 20:00:40 +02:00
int shape_idx = space->intersection_query_subindex_results[i];
2015-10-08 20:00:40 +02:00
Transform inv_xform = col_obj->get_shape_inv_transform(shape_idx) * col_obj->get_inv_transform();
Vector3 local_from = inv_xform.xform(begin);
Vector3 local_to = inv_xform.xform(end);
const ShapeSW *shape = col_obj->get_shape(shape_idx);
Vector3 shape_point, shape_normal;
2015-10-08 20:00:40 +02:00
if (shape->intersect_segment(local_from, local_to, shape_point, shape_normal)) {
2015-10-08 20:00:40 +02:00
Transform xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
shape_point = xform.xform(shape_point);
2015-10-08 20:00:40 +02:00
real_t ld = normal.dot(shape_point);
if (ld < min_d) {
2015-10-08 20:00:40 +02:00
min_d = ld;
res_point = shape_point;
res_normal = inv_xform.basis.xform_inv(shape_normal).normalized();
res_shape = shape_idx;
res_obj = col_obj;
collided = true;
2015-10-08 20:00:40 +02:00
}
}
}
if (!collided)
return false;
r_result.collider_id = res_obj->get_instance_id();
if (r_result.collider_id != 0)
r_result.collider = ObjectDB::get_instance(r_result.collider_id);
2015-10-08 20:00:40 +02:00
else
r_result.collider = NULL;
r_result.normal = res_normal;
r_result.position = res_point;
r_result.rid = res_obj->get_self();
r_result.shape = res_shape;
2015-10-08 20:00:40 +02:00
return true;
}
2017-06-13 17:45:01 +02:00
int PhysicsDirectSpaceStateSW::intersect_shape(const RID &p_shape, const Transform &p_xform, real_t p_margin, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude, uint32_t p_collision_layer, uint32_t p_object_type_mask) {
2015-10-08 20:00:40 +02:00
if (p_result_max <= 0)
2015-10-08 20:00:40 +02:00
return 0;
ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(p_shape);
ERR_FAIL_COND_V(!shape, 0);
2015-10-08 20:00:40 +02:00
Rect3 aabb = p_xform.xform(shape->get_aabb());
2015-10-08 20:00:40 +02:00
int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
2015-10-08 20:00:40 +02:00
int cc = 0;
2015-10-08 20:00:40 +02:00
//Transform ai = p_xform.affine_inverse();
for (int i = 0; i < amount; i++) {
2015-10-08 20:00:40 +02:00
if (cc >= p_result_max)
2015-10-08 20:00:40 +02:00
break;
2017-06-13 17:45:01 +02:00
if (!_match_object_type_query(space->intersection_query_results[i], p_collision_layer, p_object_type_mask))
2015-10-08 20:00:40 +02:00
continue;
//area can't be picked by ray (default)
2015-10-08 20:00:40 +02:00
if (p_exclude.has(space->intersection_query_results[i]->get_self()))
2015-10-08 20:00:40 +02:00
continue;
const CollisionObjectSW *col_obj = space->intersection_query_results[i];
int shape_idx = space->intersection_query_subindex_results[i];
2015-10-08 20:00:40 +02:00
if (!CollisionSolverSW::solve_static(shape, p_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), NULL, NULL, NULL, p_margin, 0))
2015-10-08 20:00:40 +02:00
continue;
if (r_results) {
r_results[cc].collider_id = col_obj->get_instance_id();
if (r_results[cc].collider_id != 0)
r_results[cc].collider = ObjectDB::get_instance(r_results[cc].collider_id);
else
r_results[cc].collider = NULL;
r_results[cc].rid = col_obj->get_self();
r_results[cc].shape = shape_idx;
}
cc++;
2015-10-08 20:00:40 +02:00
}
return cc;
}
2017-06-13 17:45:01 +02:00
bool PhysicsDirectSpaceStateSW::cast_motion(const RID &p_shape, const Transform &p_xform, const Vector3 &p_motion, real_t p_margin, real_t &p_closest_safe, real_t &p_closest_unsafe, const Set<RID> &p_exclude, uint32_t p_collision_layer, uint32_t p_object_type_mask, ShapeRestInfo *r_info) {
2015-10-08 20:00:40 +02:00
ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(p_shape);
ERR_FAIL_COND_V(!shape, false);
2015-10-08 20:00:40 +02:00
Rect3 aabb = p_xform.xform(shape->get_aabb());
aabb = aabb.merge(Rect3(aabb.position + p_motion, aabb.size)); //motion
aabb = aabb.grow(p_margin);
2015-10-08 20:00:40 +02:00
/*
if (p_motion!=Vector3())
print_line(p_motion);
*/
2015-10-08 20:00:40 +02:00
int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
2015-10-08 20:00:40 +02:00
real_t best_safe = 1;
real_t best_unsafe = 1;
2015-10-08 20:00:40 +02:00
Transform xform_inv = p_xform.affine_inverse();
MotionShapeSW mshape;
mshape.shape = shape;
mshape.motion = xform_inv.basis.xform(p_motion);
2015-10-08 20:00:40 +02:00
bool best_first = true;
2015-10-08 20:00:40 +02:00
Vector3 closest_A, closest_B;
2015-10-08 20:00:40 +02:00
for (int i = 0; i < amount; i++) {
2015-10-08 20:00:40 +02:00
2017-06-13 17:45:01 +02:00
if (!_match_object_type_query(space->intersection_query_results[i], p_collision_layer, p_object_type_mask))
2015-10-08 20:00:40 +02:00
continue;
if (p_exclude.has(space->intersection_query_results[i]->get_self()))
2015-10-08 20:00:40 +02:00
continue; //ignore excluded
const CollisionObjectSW *col_obj = space->intersection_query_results[i];
int shape_idx = space->intersection_query_subindex_results[i];
2015-10-08 20:00:40 +02:00
Vector3 point_A, point_B;
Vector3 sep_axis = p_motion.normalized();
2015-10-08 20:00:40 +02:00
Transform col_obj_xform = col_obj->get_transform() * col_obj->get_shape_transform(shape_idx);
//test initial overlap, does it collide if going all the way?
if (CollisionSolverSW::solve_distance(&mshape, p_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) {
2015-10-08 20:00:40 +02:00
//print_line("failed motion cast (no collision)");
continue;
}
//test initial overlap
2015-10-08 20:00:40 +02:00
#if 0
if (CollisionSolverSW::solve_static(shape,p_xform,col_obj->get_shape(shape_idx),col_obj_xform,NULL,NULL,&sep_axis)) {
print_line("failed initial cast (collision at beginning)");
2015-10-08 20:00:40 +02:00
return false;
}
#else
sep_axis = p_motion.normalized();
2015-10-08 20:00:40 +02:00
if (!CollisionSolverSW::solve_distance(shape, p_xform, col_obj->get_shape(shape_idx), col_obj_xform, point_A, point_B, aabb, &sep_axis)) {
2015-10-08 20:00:40 +02:00
//print_line("failed motion cast (no collision)");
return false;
}
#endif
//just do kinematic solving
real_t low = 0;
real_t hi = 1;
Vector3 mnormal = p_motion.normalized();
2015-10-08 20:00:40 +02:00
for (int i = 0; i < 8; i++) { //steps should be customizable..
2015-10-08 20:00:40 +02:00
real_t ofs = (low + hi) * 0.5;
2015-10-08 20:00:40 +02:00
Vector3 sep = mnormal; //important optimization for this to work fast enough
2015-10-08 20:00:40 +02:00
mshape.motion = xform_inv.basis.xform(p_motion * ofs);
2015-10-08 20:00:40 +02:00
Vector3 lA, lB;
2015-10-08 20:00:40 +02:00
bool collided = !CollisionSolverSW::solve_distance(&mshape, p_xform, col_obj->get_shape(shape_idx), col_obj_xform, lA, lB, aabb, &sep);
2015-10-08 20:00:40 +02:00
if (collided) {
//print_line(itos(i)+": "+rtos(ofs));
hi = ofs;
2015-10-08 20:00:40 +02:00
} else {
point_A = lA;
point_B = lB;
low = ofs;
2015-10-08 20:00:40 +02:00
}
}
if (low < best_safe) {
best_first = true; //force reset
best_safe = low;
best_unsafe = hi;
2015-10-08 20:00:40 +02:00
}
if (r_info && (best_first || (point_A.distance_squared_to(point_B) < closest_A.distance_squared_to(closest_B) && low <= best_safe))) {
closest_A = point_A;
closest_B = point_B;
r_info->collider_id = col_obj->get_instance_id();
r_info->rid = col_obj->get_self();
r_info->shape = shape_idx;
r_info->point = closest_B;
r_info->normal = (closest_A - closest_B).normalized();
best_first = false;
if (col_obj->get_type() == CollisionObjectSW::TYPE_BODY) {
const BodySW *body = static_cast<const BodySW *>(col_obj);
r_info->linear_velocity = body->get_linear_velocity() + (body->get_angular_velocity()).cross(body->get_transform().origin - closest_B);
2015-10-08 20:00:40 +02:00
}
}
}
p_closest_safe = best_safe;
p_closest_unsafe = best_unsafe;
2015-10-08 20:00:40 +02:00
return true;
}
2017-06-13 17:45:01 +02:00
bool PhysicsDirectSpaceStateSW::collide_shape(RID p_shape, const Transform &p_shape_xform, real_t p_margin, Vector3 *r_results, int p_result_max, int &r_result_count, const Set<RID> &p_exclude, uint32_t p_collision_layer, uint32_t p_object_type_mask) {
2015-10-08 20:00:40 +02:00
if (p_result_max <= 0)
2015-10-08 20:00:40 +02:00
return 0;
ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(p_shape);
ERR_FAIL_COND_V(!shape, 0);
2015-10-08 20:00:40 +02:00
Rect3 aabb = p_shape_xform.xform(shape->get_aabb());
aabb = aabb.grow(p_margin);
2015-10-08 20:00:40 +02:00
int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
2015-10-08 20:00:40 +02:00
bool collided = false;
r_result_count = 0;
2015-10-08 20:00:40 +02:00
PhysicsServerSW::CollCbkData cbk;
cbk.max = p_result_max;
cbk.amount = 0;
cbk.ptr = r_results;
CollisionSolverSW::CallbackResult cbkres = NULL;
PhysicsServerSW::CollCbkData *cbkptr = NULL;
if (p_result_max > 0) {
cbkptr = &cbk;
cbkres = PhysicsServerSW::_shape_col_cbk;
2015-10-08 20:00:40 +02:00
}
for (int i = 0; i < amount; i++) {
2015-10-08 20:00:40 +02:00
2017-06-13 17:45:01 +02:00
if (!_match_object_type_query(space->intersection_query_results[i], p_collision_layer, p_object_type_mask))
2015-10-08 20:00:40 +02:00
continue;
const CollisionObjectSW *col_obj = space->intersection_query_results[i];
int shape_idx = space->intersection_query_subindex_results[i];
2015-10-08 20:00:40 +02:00
if (p_exclude.has(col_obj->get_self())) {
2015-10-08 20:00:40 +02:00
continue;
}
//print_line("AGAINST: "+itos(col_obj->get_self().get_id())+":"+itos(shape_idx));
//print_line("THE ABBB: "+(col_obj->get_transform() * col_obj->get_shape_transform(shape_idx)).xform(col_obj->get_shape(shape_idx)->get_aabb()));
if (CollisionSolverSW::solve_static(shape, p_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), cbkres, cbkptr, NULL, p_margin)) {
collided = true;
2015-10-08 20:00:40 +02:00
}
}
r_result_count = cbk.amount;
2015-10-08 20:00:40 +02:00
return collided;
}
struct _RestCallbackData {
const CollisionObjectSW *object;
const CollisionObjectSW *best_object;
int shape;
int best_shape;
Vector3 best_contact;
Vector3 best_normal;
real_t best_len;
2015-10-08 20:00:40 +02:00
};
static void _rest_cbk_result(const Vector3 &p_point_A, const Vector3 &p_point_B, void *p_userdata) {
2015-10-08 20:00:40 +02:00
_RestCallbackData *rd = (_RestCallbackData *)p_userdata;
2015-10-08 20:00:40 +02:00
Vector3 contact_rel = p_point_B - p_point_A;
real_t len = contact_rel.length();
2015-10-08 20:00:40 +02:00
if (len <= rd->best_len)
return;
rd->best_len = len;
rd->best_contact = p_point_B;
rd->best_normal = contact_rel / len;
rd->best_object = rd->object;
rd->best_shape = rd->shape;
2015-10-08 20:00:40 +02:00
}
2017-06-13 17:45:01 +02:00
bool PhysicsDirectSpaceStateSW::rest_info(RID p_shape, const Transform &p_shape_xform, real_t p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude, uint32_t p_collision_layer, uint32_t p_object_type_mask) {
2015-10-08 20:00:40 +02:00
ShapeSW *shape = static_cast<PhysicsServerSW *>(PhysicsServer::get_singleton())->shape_owner.get(p_shape);
ERR_FAIL_COND_V(!shape, 0);
2015-10-08 20:00:40 +02:00
Rect3 aabb = p_shape_xform.xform(shape->get_aabb());
aabb = aabb.grow(p_margin);
2015-10-08 20:00:40 +02:00
int amount = space->broadphase->cull_aabb(aabb, space->intersection_query_results, SpaceSW::INTERSECTION_QUERY_MAX, space->intersection_query_subindex_results);
2015-10-08 20:00:40 +02:00
_RestCallbackData rcd;
rcd.best_len = 0;
rcd.best_object = NULL;
rcd.best_shape = 0;
2015-10-08 20:00:40 +02:00
for (int i = 0; i < amount; i++) {
2015-10-08 20:00:40 +02:00
2017-06-13 17:45:01 +02:00
if (!_match_object_type_query(space->intersection_query_results[i], p_collision_layer, p_object_type_mask))
2015-10-08 20:00:40 +02:00
continue;
const CollisionObjectSW *col_obj = space->intersection_query_results[i];
int shape_idx = space->intersection_query_subindex_results[i];
2015-10-08 20:00:40 +02:00
if (p_exclude.has(col_obj->get_self()))
2015-10-08 20:00:40 +02:00
continue;
rcd.object = col_obj;
rcd.shape = shape_idx;
bool sc = CollisionSolverSW::solve_static(shape, p_shape_xform, col_obj->get_shape(shape_idx), col_obj->get_transform() * col_obj->get_shape_transform(shape_idx), _rest_cbk_result, &rcd, NULL, p_margin);
2015-10-08 20:00:40 +02:00
if (!sc)
continue;
}
if (rcd.best_len == 0)
2015-10-08 20:00:40 +02:00
return false;
r_info->collider_id = rcd.best_object->get_instance_id();
r_info->shape = rcd.best_shape;
r_info->normal = rcd.best_normal;
r_info->point = rcd.best_contact;
r_info->rid = rcd.best_object->get_self();
if (rcd.best_object->get_type() == CollisionObjectSW::TYPE_BODY) {
2015-10-08 20:00:40 +02:00
const BodySW *body = static_cast<const BodySW *>(rcd.best_object);
2015-10-08 20:00:40 +02:00
r_info->linear_velocity = body->get_linear_velocity() +
(body->get_angular_velocity()).cross(body->get_transform().origin - rcd.best_contact); // * mPos);
2015-10-08 20:00:40 +02:00
} else {
r_info->linear_velocity = Vector3();
2015-10-08 20:00:40 +02:00
}
return true;
}
PhysicsDirectSpaceStateSW::PhysicsDirectSpaceStateSW() {
space = NULL;
2015-10-08 20:00:40 +02:00
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void *SpaceSW::_broadphase_pair(CollisionObjectSW *A, int p_subindex_A, CollisionObjectSW *B, int p_subindex_B, void *p_self) {
2015-10-08 20:00:40 +02:00
CollisionObjectSW::Type type_A = A->get_type();
CollisionObjectSW::Type type_B = B->get_type();
if (type_A > type_B) {
2015-10-08 20:00:40 +02:00
SWAP(A, B);
SWAP(p_subindex_A, p_subindex_B);
SWAP(type_A, type_B);
2015-10-08 20:00:40 +02:00
}
SpaceSW *self = (SpaceSW *)p_self;
2015-10-08 20:00:40 +02:00
self->collision_pairs++;
if (type_A == CollisionObjectSW::TYPE_AREA) {
2015-10-08 20:00:40 +02:00
AreaSW *area = static_cast<AreaSW *>(A);
if (type_B == CollisionObjectSW::TYPE_AREA) {
2015-10-08 20:00:40 +02:00
AreaSW *area_b = static_cast<AreaSW *>(B);
Area2PairSW *area2_pair = memnew(Area2PairSW(area_b, p_subindex_B, area, p_subindex_A));
2015-10-08 20:00:40 +02:00
return area2_pair;
} else {
BodySW *body = static_cast<BodySW *>(B);
AreaPairSW *area_pair = memnew(AreaPairSW(body, p_subindex_B, area, p_subindex_A));
2015-10-08 20:00:40 +02:00
return area_pair;
}
} else {
BodyPairSW *b = memnew(BodyPairSW((BodySW *)A, p_subindex_A, (BodySW *)B, p_subindex_B));
2015-10-08 20:00:40 +02:00
return b;
}
return NULL;
}
void SpaceSW::_broadphase_unpair(CollisionObjectSW *A, int p_subindex_A, CollisionObjectSW *B, int p_subindex_B, void *p_data, void *p_self) {
2015-10-08 20:00:40 +02:00
SpaceSW *self = (SpaceSW *)p_self;
2015-10-08 20:00:40 +02:00
self->collision_pairs--;
ConstraintSW *c = (ConstraintSW *)p_data;
2015-10-08 20:00:40 +02:00
memdelete(c);
}
const SelfList<BodySW>::List &SpaceSW::get_active_body_list() const {
2015-10-08 20:00:40 +02:00
return active_list;
}
void SpaceSW::body_add_to_active_list(SelfList<BodySW> *p_body) {
2015-10-08 20:00:40 +02:00
active_list.add(p_body);
}
void SpaceSW::body_remove_from_active_list(SelfList<BodySW> *p_body) {
2015-10-08 20:00:40 +02:00
active_list.remove(p_body);
}
void SpaceSW::body_add_to_inertia_update_list(SelfList<BodySW> *p_body) {
2015-10-08 20:00:40 +02:00
inertia_update_list.add(p_body);
}
void SpaceSW::body_remove_from_inertia_update_list(SelfList<BodySW> *p_body) {
2015-10-08 20:00:40 +02:00
inertia_update_list.remove(p_body);
}
BroadPhaseSW *SpaceSW::get_broadphase() {
return broadphase;
}
void SpaceSW::add_object(CollisionObjectSW *p_object) {
ERR_FAIL_COND(objects.has(p_object));
2015-10-08 20:00:40 +02:00
objects.insert(p_object);
}
void SpaceSW::remove_object(CollisionObjectSW *p_object) {
ERR_FAIL_COND(!objects.has(p_object));
2015-10-08 20:00:40 +02:00
objects.erase(p_object);
}
const Set<CollisionObjectSW *> &SpaceSW::get_objects() const {
2015-10-08 20:00:40 +02:00
return objects;
}
void SpaceSW::body_add_to_state_query_list(SelfList<BodySW> *p_body) {
2015-10-08 20:00:40 +02:00
state_query_list.add(p_body);
}
void SpaceSW::body_remove_from_state_query_list(SelfList<BodySW> *p_body) {
2015-10-08 20:00:40 +02:00
state_query_list.remove(p_body);
}
void SpaceSW::area_add_to_monitor_query_list(SelfList<AreaSW> *p_area) {
2015-10-08 20:00:40 +02:00
monitor_query_list.add(p_area);
}
void SpaceSW::area_remove_from_monitor_query_list(SelfList<AreaSW> *p_area) {
2015-10-08 20:00:40 +02:00
monitor_query_list.remove(p_area);
}
void SpaceSW::area_add_to_moved_list(SelfList<AreaSW> *p_area) {
2015-10-08 20:00:40 +02:00
area_moved_list.add(p_area);
}
void SpaceSW::area_remove_from_moved_list(SelfList<AreaSW> *p_area) {
2015-10-08 20:00:40 +02:00
area_moved_list.remove(p_area);
}
const SelfList<AreaSW>::List &SpaceSW::get_moved_area_list() const {
2015-10-08 20:00:40 +02:00
return area_moved_list;
}
void SpaceSW::call_queries() {
while (state_query_list.first()) {
2015-10-08 20:00:40 +02:00
BodySW *b = state_query_list.first()->self();
2015-10-08 20:00:40 +02:00
b->call_queries();
state_query_list.remove(state_query_list.first());
}
while (monitor_query_list.first()) {
2015-10-08 20:00:40 +02:00
AreaSW *a = monitor_query_list.first()->self();
2015-10-08 20:00:40 +02:00
a->call_queries();
monitor_query_list.remove(monitor_query_list.first());
}
}
void SpaceSW::setup() {
contact_debug_count = 0;
while (inertia_update_list.first()) {
2015-10-08 20:00:40 +02:00
inertia_update_list.first()->self()->update_inertias();
inertia_update_list.remove(inertia_update_list.first());
}
}
void SpaceSW::update() {
broadphase->update();
}
void SpaceSW::set_param(PhysicsServer::SpaceParameter p_param, real_t p_value) {
switch (p_param) {
2015-10-08 20:00:40 +02:00
case PhysicsServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS: contact_recycle_radius = p_value; break;
case PhysicsServer::SPACE_PARAM_CONTACT_MAX_SEPARATION: contact_max_separation = p_value; break;
case PhysicsServer::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION: contact_max_allowed_penetration = p_value; break;
case PhysicsServer::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_TRESHOLD: body_linear_velocity_sleep_threshold = p_value; break;
case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_TRESHOLD: body_angular_velocity_sleep_threshold = p_value; break;
case PhysicsServer::SPACE_PARAM_BODY_TIME_TO_SLEEP: body_time_to_sleep = p_value; break;
case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO: body_angular_velocity_damp_ratio = p_value; break;
case PhysicsServer::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS: constraint_bias = p_value; break;
2015-10-08 20:00:40 +02:00
}
}
real_t SpaceSW::get_param(PhysicsServer::SpaceParameter p_param) const {
switch (p_param) {
2015-10-08 20:00:40 +02:00
case PhysicsServer::SPACE_PARAM_CONTACT_RECYCLE_RADIUS: return contact_recycle_radius;
case PhysicsServer::SPACE_PARAM_CONTACT_MAX_SEPARATION: return contact_max_separation;
case PhysicsServer::SPACE_PARAM_BODY_MAX_ALLOWED_PENETRATION: return contact_max_allowed_penetration;
case PhysicsServer::SPACE_PARAM_BODY_LINEAR_VELOCITY_SLEEP_TRESHOLD: return body_linear_velocity_sleep_threshold;
case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_SLEEP_TRESHOLD: return body_angular_velocity_sleep_threshold;
case PhysicsServer::SPACE_PARAM_BODY_TIME_TO_SLEEP: return body_time_to_sleep;
case PhysicsServer::SPACE_PARAM_BODY_ANGULAR_VELOCITY_DAMP_RATIO: return body_angular_velocity_damp_ratio;
case PhysicsServer::SPACE_PARAM_CONSTRAINT_DEFAULT_BIAS: return constraint_bias;
}
return 0;
}
void SpaceSW::lock() {
locked = true;
2015-10-08 20:00:40 +02:00
}
void SpaceSW::unlock() {
locked = false;
2015-10-08 20:00:40 +02:00
}
bool SpaceSW::is_locked() const {
return locked;
}
PhysicsDirectSpaceStateSW *SpaceSW::get_direct_state() {
return direct_access;
}
SpaceSW::SpaceSW() {
collision_pairs = 0;
active_objects = 0;
island_count = 0;
contact_debug_count = 0;
2015-10-08 20:00:40 +02:00
locked = false;
contact_recycle_radius = 0.01;
contact_max_separation = 0.05;
contact_max_allowed_penetration = 0.01;
2015-10-08 20:00:40 +02:00
constraint_bias = 0.01;
body_linear_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_linear", 0.1);
body_angular_velocity_sleep_threshold = GLOBAL_DEF("physics/3d/sleep_threshold_angular", (8.0 / 180.0 * Math_PI));
body_time_to_sleep = GLOBAL_DEF("physics/3d/time_before_sleep", 0.5);
body_angular_velocity_damp_ratio = 10;
2015-10-08 20:00:40 +02:00
broadphase = BroadPhaseSW::create_func();
broadphase->set_pair_callback(_broadphase_pair, this);
broadphase->set_unpair_callback(_broadphase_unpair, this);
area = NULL;
2015-10-08 20:00:40 +02:00
direct_access = memnew(PhysicsDirectSpaceStateSW);
direct_access->space = this;
for (int i = 0; i < ELAPSED_TIME_MAX; i++)
elapsed_time[i] = 0;
2015-10-08 20:00:40 +02:00
}
SpaceSW::~SpaceSW() {
memdelete(broadphase);
memdelete(direct_access);
2015-10-08 20:00:40 +02:00
}