Path: blob/master/modules/godot_physics_3d/godot_space_3d.h
10277 views
/**************************************************************************/1/* godot_space_3d.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_3d.h"33#include "godot_body_3d.h"34#include "godot_broad_phase_3d.h"35#include "godot_collision_object_3d.h"36#include "godot_soft_body_3d.h"3738#include "core/typedefs.h"3940class GodotPhysicsDirectSpaceState3D : public PhysicsDirectSpaceState3D {41GDCLASS(GodotPhysicsDirectSpaceState3D, PhysicsDirectSpaceState3D);4243public:44GodotSpace3D *space = nullptr;4546virtual int intersect_point(const PointParameters &p_parameters, ShapeResult *r_results, int p_result_max) override;47virtual bool intersect_ray(const RayParameters &p_parameters, RayResult &r_result) override;48virtual int intersect_shape(const ShapeParameters &p_parameters, ShapeResult *r_results, int p_result_max) override;49virtual bool cast_motion(const ShapeParameters &p_parameters, real_t &p_closest_safe, real_t &p_closest_unsafe, ShapeRestInfo *r_info = nullptr) override;50virtual bool collide_shape(const ShapeParameters &p_parameters, Vector3 *r_results, int p_result_max, int &r_result_count) override;51virtual bool rest_info(const ShapeParameters &p_parameters, ShapeRestInfo *r_info) override;52virtual Vector3 get_closest_point_to_object_volume(RID p_object, const Vector3 p_point) const override;5354GodotPhysicsDirectSpaceState3D();55};5657class GodotSpace3D {58public:59enum ElapsedTime {60ELAPSED_TIME_INTEGRATE_FORCES,61ELAPSED_TIME_GENERATE_ISLANDS,62ELAPSED_TIME_SETUP_CONSTRAINTS,63ELAPSED_TIME_SOLVE_CONSTRAINTS,64ELAPSED_TIME_INTEGRATE_VELOCITIES,65ELAPSED_TIME_MAX6667};6869private:70uint64_t elapsed_time[ELAPSED_TIME_MAX] = {};7172GodotPhysicsDirectSpaceState3D *direct_access = nullptr;73RID self;7475GodotBroadPhase3D *broadphase = nullptr;76SelfList<GodotBody3D>::List active_list;77SelfList<GodotBody3D>::List mass_properties_update_list;78SelfList<GodotBody3D>::List state_query_list;79SelfList<GodotArea3D>::List monitor_query_list;80SelfList<GodotArea3D>::List area_moved_list;81SelfList<GodotSoftBody3D>::List active_soft_body_list;8283static void *_broadphase_pair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_self);84static void _broadphase_unpair(GodotCollisionObject3D *A, int p_subindex_A, GodotCollisionObject3D *B, int p_subindex_B, void *p_data, void *p_self);8586HashSet<GodotCollisionObject3D *> objects;8788GodotArea3D *area = nullptr;8990int solver_iterations = 0;9192real_t contact_recycle_radius = 0.0;93real_t contact_max_separation = 0.0;94real_t contact_max_allowed_penetration = 0.0;95real_t contact_bias = 0.0;9697enum {98INTERSECTION_QUERY_MAX = 204899};100101GodotCollisionObject3D *intersection_query_results[INTERSECTION_QUERY_MAX];102int intersection_query_subindex_results[INTERSECTION_QUERY_MAX];103104real_t body_linear_velocity_sleep_threshold = 0.0;105real_t body_angular_velocity_sleep_threshold = 0.0;106real_t body_time_to_sleep = 0.0;107108bool locked = false;109110real_t last_step = 0.001;111112int island_count = 0;113int active_objects = 0;114int collision_pairs = 0;115116RID static_global_body;117118Vector<Vector3> contact_debug;119int contact_debug_count = 0;120121friend class GodotPhysicsDirectSpaceState3D;122123int _cull_aabb_for_body(GodotBody3D *p_body, const AABB &p_aabb);124125public:126_FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }127_FORCE_INLINE_ RID get_self() const { return self; }128129void set_default_area(GodotArea3D *p_area) { area = p_area; }130GodotArea3D *get_default_area() const { return area; }131132const SelfList<GodotBody3D>::List &get_active_body_list() const;133void body_add_to_active_list(SelfList<GodotBody3D> *p_body);134void body_remove_from_active_list(SelfList<GodotBody3D> *p_body);135void body_add_to_mass_properties_update_list(SelfList<GodotBody3D> *p_body);136void body_remove_from_mass_properties_update_list(SelfList<GodotBody3D> *p_body);137138void body_add_to_state_query_list(SelfList<GodotBody3D> *p_body);139void body_remove_from_state_query_list(SelfList<GodotBody3D> *p_body);140141void area_add_to_monitor_query_list(SelfList<GodotArea3D> *p_area);142void area_remove_from_monitor_query_list(SelfList<GodotArea3D> *p_area);143void area_add_to_moved_list(SelfList<GodotArea3D> *p_area);144void area_remove_from_moved_list(SelfList<GodotArea3D> *p_area);145const SelfList<GodotArea3D>::List &get_moved_area_list() const;146147const SelfList<GodotSoftBody3D>::List &get_active_soft_body_list() const;148void soft_body_add_to_active_list(SelfList<GodotSoftBody3D> *p_soft_body);149void soft_body_remove_from_active_list(SelfList<GodotSoftBody3D> *p_soft_body);150151GodotBroadPhase3D *get_broadphase();152153void add_object(GodotCollisionObject3D *p_object);154void remove_object(GodotCollisionObject3D *p_object);155const HashSet<GodotCollisionObject3D *> &get_objects() const;156157_FORCE_INLINE_ int get_solver_iterations() const { return solver_iterations; }158_FORCE_INLINE_ real_t get_contact_recycle_radius() const { return contact_recycle_radius; }159_FORCE_INLINE_ real_t get_contact_max_separation() const { return contact_max_separation; }160_FORCE_INLINE_ real_t get_contact_max_allowed_penetration() const { return contact_max_allowed_penetration; }161_FORCE_INLINE_ real_t get_contact_bias() const { return contact_bias; }162_FORCE_INLINE_ real_t get_body_linear_velocity_sleep_threshold() const { return body_linear_velocity_sleep_threshold; }163_FORCE_INLINE_ real_t get_body_angular_velocity_sleep_threshold() const { return body_angular_velocity_sleep_threshold; }164_FORCE_INLINE_ real_t get_body_time_to_sleep() const { return body_time_to_sleep; }165166void update();167void setup();168void call_queries();169170bool is_locked() const;171void lock();172void unlock();173174real_t get_last_step() const { return last_step; }175void set_last_step(real_t p_step) { last_step = p_step; }176177void set_param(PhysicsServer3D::SpaceParameter p_param, real_t p_value);178real_t get_param(PhysicsServer3D::SpaceParameter p_param) const;179180void set_island_count(int p_island_count) { island_count = p_island_count; }181int get_island_count() const { return island_count; }182183void set_active_objects(int p_active_objects) { active_objects = p_active_objects; }184int get_active_objects() const { return active_objects; }185186int get_collision_pairs() const { return collision_pairs; }187188GodotPhysicsDirectSpaceState3D *get_direct_state();189190void set_debug_contacts(int p_amount) { contact_debug.resize(p_amount); }191_FORCE_INLINE_ bool is_debugging_contacts() const { return !contact_debug.is_empty(); }192_FORCE_INLINE_ void add_debug_contact(const Vector3 &p_contact) {193if (contact_debug_count < contact_debug.size()) {194contact_debug.write[contact_debug_count++] = p_contact;195}196}197_FORCE_INLINE_ Vector<Vector3> get_debug_contacts() { return contact_debug; }198_FORCE_INLINE_ int get_debug_contact_count() { return contact_debug_count; }199200void set_static_global_body(RID p_body) { static_global_body = p_body; }201RID get_static_global_body() { return static_global_body; }202203void set_elapsed_time(ElapsedTime p_time, uint64_t p_msec) { elapsed_time[p_time] = p_msec; }204uint64_t get_elapsed_time(ElapsedTime p_time) const { return elapsed_time[p_time]; }205206bool test_body_motion(GodotBody3D *p_body, const PhysicsServer3D::MotionParameters &p_parameters, PhysicsServer3D::MotionResult *r_result);207208GodotSpace3D();209~GodotSpace3D();210};211212213