Path: blob/master/modules/godot_physics_2d/godot_space_2d.h
10277 views
/**************************************************************************/1/* godot_space_2d.h */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#pragma once3132#include "godot_area_2d.h"33#include "godot_body_2d.h"34#include "godot_broad_phase_2d.h"35#include "godot_collision_object_2d.h"3637#include "core/typedefs.h"3839class GodotPhysicsDirectSpaceState2D : public PhysicsDirectSpaceState2D {40GDCLASS(GodotPhysicsDirectSpaceState2D, PhysicsDirectSpaceState2D);4142public:43GodotSpace2D *space = nullptr;4445virtual int intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) override;46virtual bool intersect_ray(const RayParameters &p_parameters, RayResult &r_result) override;47virtual int intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) override;48virtual bool cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe) override;49virtual bool collide_shape(const ShapeParameters &p_parameters, Vector2 *r_results, int p_result_max, int &r_result_count) override;50virtual bool rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) override;5152GodotPhysicsDirectSpaceState2D() {}53};5455class GodotSpace2D {56public:57enum ElapsedTime {58ELAPSED_TIME_INTEGRATE_FORCES,59ELAPSED_TIME_GENERATE_ISLANDS,60ELAPSED_TIME_SETUP_CONSTRAINTS,61ELAPSED_TIME_SOLVE_CONSTRAINTS,62ELAPSED_TIME_INTEGRATE_VELOCITIES,63ELAPSED_TIME_MAX6465};6667private:68struct ExcludedShapeSW {69GodotShape2D *local_shape = nullptr;70const GodotCollisionObject2D *against_object = nullptr;71int against_shape_index = 0;72};7374uint64_t elapsed_time[ELAPSED_TIME_MAX] = {};7576GodotPhysicsDirectSpaceState2D *direct_access = nullptr;77RID self;7879GodotBroadPhase2D *broadphase = nullptr;80SelfList<GodotBody2D>::List active_list;81SelfList<GodotBody2D>::List mass_properties_update_list;82SelfList<GodotBody2D>::List state_query_list;83SelfList<GodotArea2D>::List monitor_query_list;84SelfList<GodotArea2D>::List area_moved_list;8586static void *_broadphase_pair(GodotCollisionObject2D *A, int p_subindex_A, GodotCollisionObject2D *B, int p_subindex_B, void *p_self);87static void _broadphase_unpair(GodotCollisionObject2D *A, int p_subindex_A, GodotCollisionObject2D *B, int p_subindex_B, void *p_data, void *p_self);8889HashSet<GodotCollisionObject2D *> objects;9091GodotArea2D *area = nullptr;9293int solver_iterations = 0;9495real_t contact_recycle_radius = 0.0;96real_t contact_max_separation = 0.0;97real_t contact_max_allowed_penetration = 0.0;98real_t contact_bias = 0.0;99real_t constraint_bias = 0.0;100101enum {102INTERSECTION_QUERY_MAX = 2048103};104105GodotCollisionObject2D *intersection_query_results[INTERSECTION_QUERY_MAX];106int intersection_query_subindex_results[INTERSECTION_QUERY_MAX];107108real_t body_linear_velocity_sleep_threshold = 0.0;109real_t body_angular_velocity_sleep_threshold = 0.0;110real_t body_time_to_sleep = 0.0;111112bool locked = false;113114real_t last_step = 0.001;115116int island_count = 0;117int active_objects = 0;118int collision_pairs = 0;119120int _cull_aabb_for_body(GodotBody2D *p_body, const Rect2 &p_aabb);121122Vector<Vector2> contact_debug;123int contact_debug_count = 0;124125friend class GodotPhysicsDirectSpaceState2D;126127public:128_FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }129_FORCE_INLINE_ RID get_self() const { return self; }130131void set_default_area(GodotArea2D *p_area) { area = p_area; }132GodotArea2D *get_default_area() const { return area; }133134const SelfList<GodotBody2D>::List &get_active_body_list() const;135void body_add_to_active_list(SelfList<GodotBody2D> *p_body);136void body_remove_from_active_list(SelfList<GodotBody2D> *p_body);137void body_add_to_mass_properties_update_list(SelfList<GodotBody2D> *p_body);138void body_remove_from_mass_properties_update_list(SelfList<GodotBody2D> *p_body);139void area_add_to_moved_list(SelfList<GodotArea2D> *p_area);140void area_remove_from_moved_list(SelfList<GodotArea2D> *p_area);141const SelfList<GodotArea2D>::List &get_moved_area_list() const;142143void body_add_to_state_query_list(SelfList<GodotBody2D> *p_body);144void body_remove_from_state_query_list(SelfList<GodotBody2D> *p_body);145146void area_add_to_monitor_query_list(SelfList<GodotArea2D> *p_area);147void area_remove_from_monitor_query_list(SelfList<GodotArea2D> *p_area);148149GodotBroadPhase2D *get_broadphase();150151void add_object(GodotCollisionObject2D *p_object);152void remove_object(GodotCollisionObject2D *p_object);153const HashSet<GodotCollisionObject2D *> &get_objects() const;154155_FORCE_INLINE_ int get_solver_iterations() const { return solver_iterations; }156_FORCE_INLINE_ real_t get_contact_recycle_radius() const { return contact_recycle_radius; }157_FORCE_INLINE_ real_t get_contact_max_separation() const { return contact_max_separation; }158_FORCE_INLINE_ real_t get_contact_max_allowed_penetration() const { return contact_max_allowed_penetration; }159_FORCE_INLINE_ real_t get_contact_bias() const { return contact_bias; }160_FORCE_INLINE_ real_t get_constraint_bias() const { return constraint_bias; }161_FORCE_INLINE_ real_t get_body_linear_velocity_sleep_threshold() const { return body_linear_velocity_sleep_threshold; }162_FORCE_INLINE_ real_t get_body_angular_velocity_sleep_threshold() const { return body_angular_velocity_sleep_threshold; }163_FORCE_INLINE_ real_t get_body_time_to_sleep() const { return body_time_to_sleep; }164165void update();166void setup();167void call_queries();168169bool is_locked() const;170void lock();171void unlock();172173real_t get_last_step() const { return last_step; }174void set_last_step(real_t p_step) { last_step = p_step; }175176void set_param(PhysicsServer2D::SpaceParameter p_param, real_t p_value);177real_t get_param(PhysicsServer2D::SpaceParameter p_param) const;178179void set_island_count(int p_island_count) { island_count = p_island_count; }180int get_island_count() const { return island_count; }181182void set_active_objects(int p_active_objects) { active_objects = p_active_objects; }183int get_active_objects() const { return active_objects; }184185int get_collision_pairs() const { return collision_pairs; }186187bool test_body_motion(GodotBody2D *p_body, const PhysicsServer2D::MotionParameters &p_parameters, PhysicsServer2D::MotionResult *r_result);188189void set_debug_contacts(int p_amount) { contact_debug.resize(p_amount); }190_FORCE_INLINE_ bool is_debugging_contacts() const { return !contact_debug.is_empty(); }191_FORCE_INLINE_ void add_debug_contact(const Vector2 &p_contact) {192if (contact_debug_count < contact_debug.size()) {193contact_debug.write[contact_debug_count++] = p_contact;194}195}196_FORCE_INLINE_ Vector<Vector2> get_debug_contacts() { return contact_debug; }197_FORCE_INLINE_ int get_debug_contact_count() { return contact_debug_count; }198199GodotPhysicsDirectSpaceState2D *get_direct_state();200201void set_elapsed_time(ElapsedTime p_time, uint64_t p_msec) { elapsed_time[p_time] = p_msec; }202uint64_t get_elapsed_time(ElapsedTime p_time) const { return elapsed_time[p_time]; }203204GodotSpace2D();205~GodotSpace2D();206};207208209