2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* collision_solver_2d_sw.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 01:10:30 +00: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 "collision_solver_2d_sw.h"
|
|
|
|
#include "collision_solver_2d_sat.h"
|
|
|
|
|
|
|
|
#define collision_solver sat_2d_calculate_penetration
|
|
|
|
//#define collision_solver gjk_epa_calculate_penetration
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool CollisionSolver2DSW::solve_static_line(const Shape2DSW *p_shape_A, const Matrix32 &p_transform_A, const Shape2DSW *p_shape_B, const Matrix32 &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const LineShape2DSW *line = static_cast<const LineShape2DSW *>(p_shape_A);
|
|
|
|
if (p_shape_B->get_type() == Physics2DServer::SHAPE_LINE)
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Vector2 n = p_transform_A.basis_xform(line->get_normal()).normalized();
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 p = p_transform_A.xform(line->get_normal() * line->get_d());
|
2014-02-10 01:10:30 +00:00
|
|
|
real_t d = n.dot(p);
|
|
|
|
|
|
|
|
Vector2 supports[2];
|
|
|
|
int support_count;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
p_shape_B->get_supports(p_transform_A.affine_inverse().basis_xform(-n).normalized(), supports, support_count);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool found = false;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < support_count; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
supports[i] = p_transform_B.xform(supports[i]);
|
2014-02-10 01:10:30 +00:00
|
|
|
real_t pd = n.dot(supports[i]);
|
2017-03-18 23:36:26 +00:00
|
|
|
if (pd >= d)
|
2014-02-10 01:10:30 +00:00
|
|
|
continue;
|
2017-03-18 23:36:26 +00:00
|
|
|
found = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 support_A = supports[i] - n * (pd - d);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p_result_callback) {
|
|
|
|
if (p_swap_result)
|
2017-03-18 23:36:26 +00:00
|
|
|
p_result_callback(supports[i], support_A, p_userdata);
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
p_result_callback(support_A, supports[i], p_userdata);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool CollisionSolver2DSW::solve_raycast(const Shape2DSW *p_shape_A, const Matrix32 &p_transform_A, const Shape2DSW *p_shape_B, const Matrix32 &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, Vector2 *sep_axis) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const RayShape2DSW *ray = static_cast<const RayShape2DSW *>(p_shape_A);
|
|
|
|
if (p_shape_B->get_type() == Physics2DServer::SHAPE_RAY)
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
Vector2 from = p_transform_A.get_origin();
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 to = from + p_transform_A[1] * ray->get_length();
|
|
|
|
Vector2 support_A = to;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2014-02-19 14:57:14 +00:00
|
|
|
Matrix32 invb = p_transform_B.affine_inverse();
|
|
|
|
from = invb.xform(from);
|
|
|
|
to = invb.xform(to);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 p, n;
|
|
|
|
if (!p_shape_B->intersect_segment(from, to, p, n)) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (sep_axis)
|
2017-03-18 23:36:26 +00:00
|
|
|
*sep_axis = p_transform_A[1].normalized();
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 support_B = p_transform_B.xform(p);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (p_result_callback) {
|
|
|
|
if (p_swap_result)
|
2017-03-18 23:36:26 +00:00
|
|
|
p_result_callback(support_B, support_A, p_userdata);
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
p_result_callback(support_A, support_B, p_userdata);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
bool CollisionSolver2DSW::solve_ray(const Shape2DSW *p_shape_A,const Matrix32& p_transform_A,const Shape2DSW *p_shape_B,const Matrix32& p_transform_B,const Matrix32& p_inverse_B,CallbackResult p_result_callback,void *p_userdata,bool p_swap_result) {
|
|
|
|
|
|
|
|
|
|
|
|
const RayShape2DSW *ray = static_cast<const RayShape2DSW*>(p_shape_A);
|
|
|
|
|
|
|
|
Vector2 from = p_transform_A.origin;
|
|
|
|
Vector2 to = from+p_transform_A.basis.get_axis(2)*ray->get_length();
|
|
|
|
Vector2 support_A=to;
|
|
|
|
|
|
|
|
from = p_inverse_B.xform(from);
|
|
|
|
to = p_inverse_B.xform(to);
|
|
|
|
|
|
|
|
Vector2 p,n;
|
|
|
|
if (!p_shape_B->intersect_segment(from,to,&p,&n))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
Vector2 support_B=p_transform_B.xform(p);
|
|
|
|
|
|
|
|
if (p_result_callback) {
|
|
|
|
if (p_swap_result)
|
|
|
|
p_result_callback(support_B,support_A,p_userdata);
|
|
|
|
else
|
|
|
|
p_result_callback(support_A,support_B,p_userdata);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct _ConcaveCollisionInfo2D {
|
|
|
|
|
|
|
|
const Matrix32 *transform_A;
|
|
|
|
const Shape2DSW *shape_A;
|
|
|
|
const Matrix32 *transform_B;
|
2014-02-19 14:57:14 +00:00
|
|
|
Vector2 motion_A;
|
|
|
|
Vector2 motion_B;
|
2014-02-22 23:28:19 +00:00
|
|
|
real_t margin_A;
|
|
|
|
real_t margin_B;
|
2014-02-10 01:10:30 +00:00
|
|
|
CollisionSolver2DSW::CallbackResult result_callback;
|
|
|
|
void *userdata;
|
|
|
|
bool swap_result;
|
|
|
|
bool collided;
|
|
|
|
int aabb_tests;
|
|
|
|
int collisions;
|
|
|
|
Vector2 *sep_axis;
|
|
|
|
};
|
|
|
|
|
|
|
|
void CollisionSolver2DSW::concave_callback(void *p_userdata, Shape2DSW *p_convex) {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
_ConcaveCollisionInfo2D &cinfo = *(_ConcaveCollisionInfo2D *)(p_userdata);
|
2014-02-10 01:10:30 +00:00
|
|
|
cinfo.aabb_tests++;
|
|
|
|
if (!cinfo.result_callback && cinfo.collided)
|
|
|
|
return; //already collided and no contacts requested, don't test anymore
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool collided = collision_solver(cinfo.shape_A, *cinfo.transform_A, cinfo.motion_A, p_convex, *cinfo.transform_B, cinfo.motion_B, cinfo.result_callback, cinfo.userdata, cinfo.swap_result, cinfo.sep_axis, cinfo.margin_A, cinfo.margin_B);
|
2014-02-10 01:10:30 +00:00
|
|
|
if (!collided)
|
|
|
|
return;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
cinfo.collided = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
cinfo.collisions++;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool CollisionSolver2DSW::solve_concave(const Shape2DSW *p_shape_A, const Matrix32 &p_transform_A, const Vector2 &p_motion_A, const Shape2DSW *p_shape_B, const Matrix32 &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, Vector2 *sep_axis, float p_margin_A, float p_margin_B) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
const ConcaveShape2DSW *concave_B = static_cast<const ConcaveShape2DSW *>(p_shape_B);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
_ConcaveCollisionInfo2D cinfo;
|
2017-03-18 23:36:26 +00:00
|
|
|
cinfo.transform_A = &p_transform_A;
|
|
|
|
cinfo.shape_A = p_shape_A;
|
|
|
|
cinfo.transform_B = &p_transform_B;
|
|
|
|
cinfo.motion_A = p_motion_A;
|
|
|
|
cinfo.result_callback = p_result_callback;
|
|
|
|
cinfo.userdata = p_userdata;
|
|
|
|
cinfo.swap_result = p_swap_result;
|
|
|
|
cinfo.collided = false;
|
|
|
|
cinfo.collisions = 0;
|
|
|
|
cinfo.sep_axis = sep_axis;
|
|
|
|
cinfo.margin_A = p_margin_A;
|
|
|
|
cinfo.margin_B = p_margin_B;
|
|
|
|
|
|
|
|
cinfo.aabb_tests = 0;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
Matrix32 rel_transform = p_transform_A;
|
2017-03-18 23:36:26 +00:00
|
|
|
rel_transform.elements[2] -= p_transform_B.elements[2];
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
//quickly compute a local Rect2
|
|
|
|
|
|
|
|
Rect2 local_aabb;
|
2017-03-18 23:36:26 +00:00
|
|
|
for (int i = 0; i < 2; i++) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Vector2 axis(p_transform_B.elements[i]);
|
|
|
|
float axis_scale = 1.0 / axis.length();
|
|
|
|
axis *= axis_scale;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
float smin, smax;
|
|
|
|
p_shape_A->project_rangev(axis, rel_transform, smin, smax);
|
|
|
|
smin *= axis_scale;
|
|
|
|
smax *= axis_scale;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
local_aabb.pos[i] = smin;
|
|
|
|
local_aabb.size[i] = smax - smin;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
concave_B->cull(local_aabb, concave_callback, &cinfo);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
// print_line("Rect2 TESTS: "+itos(cinfo.aabb_tests));
|
2014-02-10 01:10:30 +00:00
|
|
|
return cinfo.collided;
|
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
bool CollisionSolver2DSW::solve(const Shape2DSW *p_shape_A, const Matrix32 &p_transform_A, const Vector2 &p_motion_A, const Shape2DSW *p_shape_B, const Matrix32 &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, Vector2 *sep_axis, float p_margin_A, float p_margin_B) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
Physics2DServer::ShapeType type_A = p_shape_A->get_type();
|
|
|
|
Physics2DServer::ShapeType type_B = p_shape_B->get_type();
|
|
|
|
bool concave_A = p_shape_A->is_concave();
|
|
|
|
bool concave_B = p_shape_B->is_concave();
|
|
|
|
real_t margin_A = p_margin_A, margin_B = p_margin_B;
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
bool swap = false;
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (type_A > type_B) {
|
|
|
|
SWAP(type_A, type_B);
|
|
|
|
SWAP(concave_A, concave_B);
|
|
|
|
SWAP(margin_A, margin_B);
|
|
|
|
swap = true;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (type_A == Physics2DServer::SHAPE_LINE) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (type_B == Physics2DServer::SHAPE_LINE || type_B == Physics2DServer::SHAPE_RAY) {
|
2014-02-10 01:10:30 +00:00
|
|
|
return false;
|
2017-03-18 23:36:26 +00:00
|
|
|
//if (type_B==Physics2DServer::SHAPE_RAY) {
|
|
|
|
// return false;
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (swap) {
|
2017-03-18 23:36:26 +00:00
|
|
|
return solve_static_line(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
return solve_static_line(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
/*} else if (type_A==Physics2DServer::SHAPE_RAY) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
if (type_B==Physics2DServer::SHAPE_RAY)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (swap) {
|
|
|
|
return solve_ray(p_shape_B,p_transform_B,p_shape_A,p_transform_A,p_inverse_A,p_result_callback,p_userdata,true);
|
|
|
|
} else {
|
|
|
|
return solve_ray(p_shape_A,p_transform_A,p_shape_B,p_transform_B,p_inverse_B,p_result_callback,p_userdata,false);
|
|
|
|
}
|
|
|
|
*/
|
2017-03-18 23:36:26 +00:00
|
|
|
} else if (type_A == Physics2DServer::SHAPE_RAY) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
if (type_B == Physics2DServer::SHAPE_RAY) {
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
return false; //no ray-ray
|
|
|
|
}
|
|
|
|
|
|
|
|
if (swap) {
|
2017-03-18 23:36:26 +00:00
|
|
|
return solve_raycast(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true, sep_axis);
|
2014-02-10 01:10:30 +00:00
|
|
|
} else {
|
2017-03-18 23:36:26 +00:00
|
|
|
return solve_raycast(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, sep_axis);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (concave_B) {
|
|
|
|
|
|
|
|
if (concave_A)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!swap)
|
2017-03-18 23:36:26 +00:00
|
|
|
return solve_concave(p_shape_A, p_transform_A, p_motion_A, p_shape_B, p_transform_B, p_motion_B, p_result_callback, p_userdata, false, sep_axis, margin_A, margin_B);
|
2014-02-10 01:10:30 +00:00
|
|
|
else
|
2017-03-18 23:36:26 +00:00
|
|
|
return solve_concave(p_shape_B, p_transform_B, p_motion_B, p_shape_A, p_transform_A, p_motion_A, p_result_callback, p_userdata, true, sep_axis, margin_A, margin_B);
|
2014-02-10 01:10:30 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-18 23:36:26 +00:00
|
|
|
return collision_solver(p_shape_A, p_transform_A, p_motion_A, p_shape_B, p_transform_B, p_motion_B, p_result_callback, p_userdata, false, sep_axis, margin_A, margin_B);
|
2014-02-10 01:10:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|