2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* space_2d_sw.h */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2014-02-10 01:10:30 +00:00
|
|
|
/*************************************************************************/
|
2022-01-13 08:45:09 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-02-19 14:57:14 +00:00
|
|
|
#ifndef SPACE_2D_SW_H
|
|
|
|
#define SPACE_2D_SW_H
|
|
|
|
|
|
|
|
#include "area_2d_sw.h"
|
|
|
|
#include "area_pair_2d_sw.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "body_2d_sw.h"
|
|
|
|
#include "body_pair_2d_sw.h"
|
2014-02-19 14:57:14 +00:00
|
|
|
#include "broad_phase_2d_sw.h"
|
|
|
|
#include "collision_object_2d_sw.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/hash_map.h"
|
|
|
|
#include "core/project_settings.h"
|
|
|
|
#include "core/typedefs.h"
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
class Physics2DDirectSpaceStateSW : public Physics2DDirectSpaceState {
|
2017-03-05 15:44:50 +00:00
|
|
|
GDCLASS(Physics2DDirectSpaceStateSW, Physics2DDirectSpaceState);
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2018-08-24 22:03:26 +00:00
|
|
|
int _intersect_point_impl(const Vector2 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude, uint32_t p_collision_mask, bool p_collide_with_bodies, bool p_collide_with_areas, bool p_pick_point, bool p_filter_by_canvas = false, ObjectID p_canvas_instance_id = 0);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
public:
|
2014-02-19 14:57:14 +00:00
|
|
|
Space2DSW *space;
|
|
|
|
|
2018-08-21 18:30:41 +00:00
|
|
|
virtual int intersect_point(const Vector2 &p_point, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, bool p_pick_point = false);
|
2018-08-24 22:03:26 +00:00
|
|
|
virtual int intersect_point_on_canvas(const Vector2 &p_point, ObjectID p_canvas_instance_id, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false, bool p_pick_point = false);
|
2018-08-21 18:30:41 +00:00
|
|
|
virtual bool intersect_ray(const Vector2 &p_from, const Vector2 &p_to, RayResult &r_result, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
|
|
|
|
virtual int intersect_shape(const RID &p_shape, const Transform2D &p_xform, const Vector2 &p_motion, real_t p_margin, ShapeResult *r_results, int p_result_max, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
|
|
|
|
virtual bool cast_motion(const RID &p_shape, const Transform2D &p_xform, const Vector2 &p_motion, real_t p_margin, real_t &p_closest_safe, real_t &p_closest_unsafe, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
|
|
|
|
virtual bool collide_shape(RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, real_t p_margin, Vector2 *r_results, int p_result_max, int &r_result_count, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
|
|
|
|
virtual bool rest_info(RID p_shape, const Transform2D &p_shape_xform, const Vector2 &p_motion, real_t p_margin, ShapeRestInfo *r_info, const Set<RID> &p_exclude = Set<RID>(), uint32_t p_collision_mask = 0xFFFFFFFF, bool p_collide_with_bodies = true, bool p_collide_with_areas = false);
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
Physics2DDirectSpaceStateSW();
|
|
|
|
};
|
|
|
|
|
2016-10-03 19:33:42 +00:00
|
|
|
class Space2DSW : public RID_Data {
|
2016-05-22 00:18:16 +00:00
|
|
|
public:
|
|
|
|
enum ElapsedTime {
|
|
|
|
ELAPSED_TIME_INTEGRATE_FORCES,
|
|
|
|
ELAPSED_TIME_GENERATE_ISLANDS,
|
|
|
|
ELAPSED_TIME_SETUP_CONSTRAINTS,
|
|
|
|
ELAPSED_TIME_SOLVE_CONSTRAINTS,
|
|
|
|
ELAPSED_TIME_INTEGRATE_VELOCITIES,
|
|
|
|
ELAPSED_TIME_MAX
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
private:
|
2017-09-17 12:38:09 +00:00
|
|
|
struct ExcludedShapeSW {
|
|
|
|
Shape2DSW *local_shape;
|
|
|
|
const CollisionObject2DSW *against_object;
|
2017-09-17 13:18:09 +00:00
|
|
|
int against_shape_index;
|
2017-09-17 12:38:09 +00:00
|
|
|
};
|
|
|
|
|
2016-05-22 00:18:16 +00:00
|
|
|
uint64_t elapsed_time[ELAPSED_TIME_MAX];
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
Physics2DDirectSpaceStateSW *direct_access;
|
|
|
|
RID self;
|
|
|
|
|
|
|
|
BroadPhase2DSW *broadphase;
|
|
|
|
SelfList<Body2DSW>::List active_list;
|
|
|
|
SelfList<Body2DSW>::List inertia_update_list;
|
|
|
|
SelfList<Body2DSW>::List state_query_list;
|
|
|
|
SelfList<Area2DSW>::List monitor_query_list;
|
|
|
|
SelfList<Area2DSW>::List area_moved_list;
|
|
|
|
|
2021-10-21 19:06:09 +00:00
|
|
|
static void *_broadphase_pair(CollisionObject2DSW *p_object_A, int p_subindex_A, CollisionObject2DSW *p_object_B, int p_subindex_B, void *p_pair_data, void *p_self);
|
|
|
|
static void _broadphase_unpair(CollisionObject2DSW *p_object_A, int p_subindex_A, CollisionObject2DSW *p_object_B, int p_subindex_B, void *p_pair_data, void *p_self);
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Set<CollisionObject2DSW *> objects;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
Area2DSW *area;
|
|
|
|
|
|
|
|
real_t contact_recycle_radius;
|
|
|
|
real_t contact_max_separation;
|
|
|
|
real_t contact_max_allowed_penetration;
|
|
|
|
real_t constraint_bias;
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
INTERSECTION_QUERY_MAX = 2048
|
2014-02-19 14:57:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CollisionObject2DSW *intersection_query_results[INTERSECTION_QUERY_MAX];
|
|
|
|
int intersection_query_subindex_results[INTERSECTION_QUERY_MAX];
|
|
|
|
|
2017-07-08 15:12:18 +00:00
|
|
|
real_t body_linear_velocity_sleep_threshold;
|
|
|
|
real_t body_angular_velocity_sleep_threshold;
|
2017-02-13 23:25:05 +00:00
|
|
|
real_t body_time_to_sleep;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
bool locked;
|
|
|
|
|
2020-10-19 19:31:03 +00:00
|
|
|
real_t step;
|
2014-09-03 02:13:40 +00:00
|
|
|
int island_count;
|
|
|
|
int active_objects;
|
|
|
|
int collision_pairs;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int _cull_aabb_for_body(Body2DSW *p_body, const Rect2 &p_aabb);
|
2015-05-04 01:37:10 +00:00
|
|
|
|
2015-09-20 16:03:46 +00:00
|
|
|
Vector<Vector2> contact_debug;
|
|
|
|
int contact_debug_count;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
friend class Physics2DDirectSpaceStateSW;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
public:
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
|
2014-02-19 14:57:14 +00:00
|
|
|
_FORCE_INLINE_ RID get_self() const { return self; }
|
|
|
|
|
2020-10-19 19:31:03 +00:00
|
|
|
_FORCE_INLINE_ void set_step(const real_t &p_step) { step = p_step; }
|
|
|
|
_FORCE_INLINE_ real_t get_step() const { return step; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void set_default_area(Area2DSW *p_area) { area = p_area; }
|
2014-02-19 14:57:14 +00:00
|
|
|
Area2DSW *get_default_area() const { return area; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
const SelfList<Body2DSW>::List &get_active_body_list() const;
|
|
|
|
void body_add_to_active_list(SelfList<Body2DSW> *p_body);
|
|
|
|
void body_remove_from_active_list(SelfList<Body2DSW> *p_body);
|
|
|
|
void body_add_to_inertia_update_list(SelfList<Body2DSW> *p_body);
|
|
|
|
void body_remove_from_inertia_update_list(SelfList<Body2DSW> *p_body);
|
|
|
|
void area_add_to_moved_list(SelfList<Area2DSW> *p_area);
|
|
|
|
void area_remove_from_moved_list(SelfList<Area2DSW> *p_area);
|
|
|
|
const SelfList<Area2DSW>::List &get_moved_area_list() const;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void body_add_to_state_query_list(SelfList<Body2DSW> *p_body);
|
|
|
|
void body_remove_from_state_query_list(SelfList<Body2DSW> *p_body);
|
2014-02-19 14:57:14 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void area_add_to_monitor_query_list(SelfList<Area2DSW> *p_area);
|
|
|
|
void area_remove_from_monitor_query_list(SelfList<Area2DSW> *p_area);
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
BroadPhase2DSW *get_broadphase();
|
|
|
|
|
|
|
|
void add_object(CollisionObject2DSW *p_object);
|
|
|
|
void remove_object(CollisionObject2DSW *p_object);
|
2017-03-05 15:44:50 +00:00
|
|
|
const Set<CollisionObject2DSW *> &get_objects() const;
|
2014-02-19 14:57:14 +00:00
|
|
|
|
|
|
|
_FORCE_INLINE_ real_t get_contact_recycle_radius() const { return contact_recycle_radius; }
|
|
|
|
_FORCE_INLINE_ real_t get_contact_max_separation() const { return contact_max_separation; }
|
|
|
|
_FORCE_INLINE_ real_t get_contact_max_allowed_penetration() const { return contact_max_allowed_penetration; }
|
|
|
|
_FORCE_INLINE_ real_t get_constraint_bias() const { return constraint_bias; }
|
2017-07-08 15:12:18 +00:00
|
|
|
_FORCE_INLINE_ real_t get_body_linear_velocity_sleep_threshold() const { return body_linear_velocity_sleep_threshold; }
|
|
|
|
_FORCE_INLINE_ real_t get_body_angular_velocity_sleep_threshold() const { return body_angular_velocity_sleep_threshold; }
|
2014-02-19 14:57:14 +00:00
|
|
|
_FORCE_INLINE_ real_t get_body_time_to_sleep() const { return body_time_to_sleep; }
|
2015-01-05 21:37:12 +00:00
|
|
|
|
2014-02-19 14:57:14 +00:00
|
|
|
void update();
|
|
|
|
void setup();
|
|
|
|
void call_queries();
|
|
|
|
|
|
|
|
bool is_locked() const;
|
|
|
|
void lock();
|
|
|
|
void unlock();
|
|
|
|
|
|
|
|
void set_param(Physics2DServer::SpaceParameter p_param, real_t p_value);
|
|
|
|
real_t get_param(Physics2DServer::SpaceParameter p_param) const;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void set_island_count(int p_island_count) { island_count = p_island_count; }
|
2014-09-03 02:13:40 +00:00
|
|
|
int get_island_count() const { return island_count; }
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void set_active_objects(int p_active_objects) { active_objects = p_active_objects; }
|
2014-09-03 02:13:40 +00:00
|
|
|
int get_active_objects() const { return active_objects; }
|
|
|
|
|
|
|
|
int get_collision_pairs() const { return collision_pairs; }
|
|
|
|
|
2021-07-04 18:05:22 +00:00
|
|
|
bool test_body_motion(Body2DSW *p_body, const Transform2D &p_from, const Vector2 &p_motion, bool p_infinite_inertia, real_t p_margin, Physics2DServer::MotionResult *r_result, bool p_exclude_raycast_shapes = true, const Set<RID> &p_exclude = Set<RID>());
|
2018-07-16 23:04:07 +00:00
|
|
|
int test_body_ray_separation(Body2DSW *p_body, const Transform2D &p_transform, bool p_infinite_inertia, Vector2 &r_recover_motion, Physics2DServer::SeparationResult *r_results, int p_result_max, real_t p_margin);
|
2015-09-20 16:03:46 +00:00
|
|
|
|
|
|
|
void set_debug_contacts(int p_amount) { contact_debug.resize(p_amount); }
|
|
|
|
_FORCE_INLINE_ bool is_debugging_contacts() const { return !contact_debug.empty(); }
|
2017-03-05 15:44:50 +00:00
|
|
|
_FORCE_INLINE_ void add_debug_contact(const Vector2 &p_contact) {
|
2021-05-05 10:44:11 +00:00
|
|
|
if (contact_debug_count < contact_debug.size()) {
|
2021-05-04 12:28:27 +00:00
|
|
|
contact_debug.write[contact_debug_count++] = p_contact;
|
2021-05-05 10:44:11 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
}
|
2015-09-20 16:03:46 +00:00
|
|
|
_FORCE_INLINE_ Vector<Vector2> get_debug_contacts() { return contact_debug; }
|
|
|
|
_FORCE_INLINE_ int get_debug_contact_count() { return contact_debug_count; }
|
|
|
|
|
2014-02-19 14:57:14 +00:00
|
|
|
Physics2DDirectSpaceStateSW *get_direct_state();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void set_elapsed_time(ElapsedTime p_time, uint64_t p_msec) { elapsed_time[p_time] = p_msec; }
|
2016-05-22 00:18:16 +00:00
|
|
|
uint64_t get_elapsed_time(ElapsedTime p_time) const { return elapsed_time[p_time]; }
|
|
|
|
|
2014-02-19 14:57:14 +00:00
|
|
|
Space2DSW();
|
|
|
|
~Space2DSW();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SPACE_2D_SW_H
|