Path: blob/master/modules/godot_physics_2d/godot_collision_solver_2d.cpp
10277 views
/**************************************************************************/1/* godot_collision_solver_2d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "godot_collision_solver_2d.h"31#include "godot_collision_solver_2d_sat.h"3233#define collision_solver sat_2d_calculate_penetration34//#define collision_solver gjk_epa_calculate_penetration3536bool GodotCollisionSolver2D::solve_static_world_boundary(const GodotShape2D *p_shape_A, const Transform2D &p_transform_A, const GodotShape2D *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, real_t p_margin) {37const GodotWorldBoundaryShape2D *world_boundary = static_cast<const GodotWorldBoundaryShape2D *>(p_shape_A);38if (p_shape_B->get_type() == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {39return false;40}4142Vector2 n = p_transform_A.basis_xform(world_boundary->get_normal()).normalized();43Vector2 p = p_transform_A.xform(world_boundary->get_normal() * world_boundary->get_d());44real_t d = n.dot(p);4546Vector2 supports[2];47int support_count;4849p_shape_B->get_supports(p_transform_B.affine_inverse().basis_xform(-n).normalized(), supports, support_count);5051bool found = false;5253for (int i = 0; i < support_count; i++) {54supports[i] += p_margin * supports[i].normalized();55supports[i] = p_transform_B.xform(supports[i]);56supports[i] += p_motion_B;57real_t pd = n.dot(supports[i]);58if (pd >= d) {59continue;60}61found = true;6263Vector2 support_A = supports[i] - n * (pd - d);6465if (p_result_callback) {66if (p_swap_result) {67p_result_callback(supports[i], support_A, p_userdata);68} else {69p_result_callback(support_A, supports[i], p_userdata);70}71}72}7374return found;75}7677bool GodotCollisionSolver2D::solve_separation_ray(const GodotShape2D *p_shape_A, const Vector2 &p_motion_A, const Transform2D &p_transform_A, const GodotShape2D *p_shape_B, const Transform2D &p_transform_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, Vector2 *r_sep_axis, real_t p_margin) {78const GodotSeparationRayShape2D *ray = static_cast<const GodotSeparationRayShape2D *>(p_shape_A);79if (p_shape_B->get_type() == PhysicsServer2D::SHAPE_SEPARATION_RAY) {80return false;81}8283Vector2 from = p_transform_A.get_origin();84Vector2 to = from + p_transform_A[1] * (ray->get_length() + p_margin);85if (p_motion_A != Vector2()) {86//not the best but should be enough87Vector2 normal = (to - from).normalized();88to += normal * MAX(0.0, normal.dot(p_motion_A));89}90Vector2 support_A = to;9192Transform2D invb = p_transform_B.affine_inverse();93from = invb.xform(from);94to = invb.xform(to);9596Vector2 p, n;97if (!p_shape_B->intersect_segment(from, to, p, n)) {98if (r_sep_axis) {99*r_sep_axis = p_transform_A[1].normalized();100}101return false;102}103104// Discard contacts when the ray is fully contained inside the shape.105if (n == Vector2()) {106if (r_sep_axis) {107*r_sep_axis = p_transform_A[1].normalized();108}109return false;110}111112// Discard contacts in the wrong direction.113if (n.dot(from - to) < CMP_EPSILON) {114if (r_sep_axis) {115*r_sep_axis = p_transform_A[1].normalized();116}117return false;118}119120Vector2 support_B = p_transform_B.xform(p);121if (ray->get_slide_on_slope()) {122Vector2 global_n = invb.basis_xform_inv(n).normalized();123support_B = support_A + (support_B - support_A).length() * global_n;124}125126if (p_result_callback) {127if (p_swap_result) {128p_result_callback(support_B, support_A, p_userdata);129} else {130p_result_callback(support_A, support_B, p_userdata);131}132}133return true;134}135136struct _ConcaveCollisionInfo2D {137const Transform2D *transform_A = nullptr;138const GodotShape2D *shape_A = nullptr;139const Transform2D *transform_B = nullptr;140Vector2 motion_A;141Vector2 motion_B;142real_t margin_A = 0.0;143real_t margin_B = 0.0;144GodotCollisionSolver2D::CallbackResult result_callback = nullptr;145void *userdata = nullptr;146bool swap_result = false;147bool collided = false;148int aabb_tests = 0;149int collisions = 0;150Vector2 *sep_axis = nullptr;151};152153bool GodotCollisionSolver2D::concave_callback(void *p_userdata, GodotShape2D *p_convex) {154_ConcaveCollisionInfo2D &cinfo = *(static_cast<_ConcaveCollisionInfo2D *>(p_userdata));155cinfo.aabb_tests++;156157bool 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);158if (!collided) {159return false;160}161162cinfo.collided = true;163cinfo.collisions++;164165// Stop at first collision if contacts are not needed.166return !cinfo.result_callback;167}168169bool GodotCollisionSolver2D::solve_concave(const GodotShape2D *p_shape_A, const Transform2D &p_transform_A, const Vector2 &p_motion_A, const GodotShape2D *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, bool p_swap_result, Vector2 *r_sep_axis, real_t p_margin_A, real_t p_margin_B) {170const GodotConcaveShape2D *concave_B = static_cast<const GodotConcaveShape2D *>(p_shape_B);171172_ConcaveCollisionInfo2D cinfo;173cinfo.transform_A = &p_transform_A;174cinfo.shape_A = p_shape_A;175cinfo.transform_B = &p_transform_B;176cinfo.motion_A = p_motion_A;177cinfo.result_callback = p_result_callback;178cinfo.userdata = p_userdata;179cinfo.swap_result = p_swap_result;180cinfo.collided = false;181cinfo.collisions = 0;182cinfo.sep_axis = r_sep_axis;183cinfo.margin_A = p_margin_A;184cinfo.margin_B = p_margin_B;185186cinfo.aabb_tests = 0;187188Transform2D rel_transform = p_transform_A;189rel_transform.columns[2] -= p_transform_B.get_origin();190191// Quickly compute a local Rect2.192Rect2 local_aabb;193for (int i = 0; i < 2; i++) {194Vector2 axis(p_transform_B.columns[i]);195real_t axis_scale = 1.0 / axis.length();196axis *= axis_scale;197198real_t smin = 0.0, smax = 0.0;199p_shape_A->project_rangev(axis, rel_transform, smin, smax);200smin *= axis_scale;201smax *= axis_scale;202203local_aabb.position[i] = smin;204local_aabb.size[i] = smax - smin;205}206// In case of motion, expand the Rect2 in the motion direction.207if (p_motion_A != Vector2()) {208Rect2 moved_aabb = local_aabb;209moved_aabb.position += p_motion_A;210local_aabb = local_aabb.merge(moved_aabb);211}212213concave_B->cull(local_aabb, concave_callback, &cinfo);214215return cinfo.collided;216}217218bool GodotCollisionSolver2D::solve(const GodotShape2D *p_shape_A, const Transform2D &p_transform_A, const Vector2 &p_motion_A, const GodotShape2D *p_shape_B, const Transform2D &p_transform_B, const Vector2 &p_motion_B, CallbackResult p_result_callback, void *p_userdata, Vector2 *r_sep_axis, real_t p_margin_A, real_t p_margin_B) {219PhysicsServer2D::ShapeType type_A = p_shape_A->get_type();220PhysicsServer2D::ShapeType type_B = p_shape_B->get_type();221bool concave_A = p_shape_A->is_concave();222bool concave_B = p_shape_B->is_concave();223real_t margin_A = p_margin_A, margin_B = p_margin_B;224225bool swap = false;226227if (type_A > type_B) {228SWAP(type_A, type_B);229SWAP(concave_A, concave_B);230SWAP(margin_A, margin_B);231swap = true;232}233234if (type_A == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {235if (type_B == PhysicsServer2D::SHAPE_WORLD_BOUNDARY) {236WARN_PRINT_ONCE("Collisions between world boundaries are not supported.");237return false;238}239240if (swap) {241return solve_static_world_boundary(p_shape_B, p_transform_B, p_shape_A, p_transform_A, p_motion_A, p_result_callback, p_userdata, true, p_margin_A);242} else {243return solve_static_world_boundary(p_shape_A, p_transform_A, p_shape_B, p_transform_B, p_motion_B, p_result_callback, p_userdata, false, p_margin_B);244}245246} else if (type_A == PhysicsServer2D::SHAPE_SEPARATION_RAY) {247if (type_B == PhysicsServer2D::SHAPE_SEPARATION_RAY) {248WARN_PRINT_ONCE("Collisions between two rays are not supported.");249return false; //no ray-ray250}251252if (swap) {253return solve_separation_ray(p_shape_B, p_motion_B, p_transform_B, p_shape_A, p_transform_A, p_result_callback, p_userdata, true, r_sep_axis, p_margin_B);254} else {255return solve_separation_ray(p_shape_A, p_motion_A, p_transform_A, p_shape_B, p_transform_B, p_result_callback, p_userdata, false, r_sep_axis, p_margin_A);256}257258} else if (concave_B) {259if (concave_A) {260WARN_PRINT_ONCE("Collisions between two concave shapes are not supported.");261return false;262}263264if (!swap) {265return 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, r_sep_axis, margin_A, margin_B);266} else {267return 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, r_sep_axis, margin_A, margin_B);268}269270} else {271return 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, r_sep_axis, margin_A, margin_B);272}273}274275276