Path: blob/master/modules/godot_physics_3d/godot_collision_object_3d.h
10277 views
/**************************************************************************/1/* godot_collision_object_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_broad_phase_3d.h"33#include "godot_shape_3d.h"3435#include "core/templates/self_list.h"36#include "servers/physics_server_3d.h"3738#ifdef DEBUG_ENABLED39#define MAX_OBJECT_DISTANCE 3.1622776601683791e+184041#define MAX_OBJECT_DISTANCE_X2 (MAX_OBJECT_DISTANCE * MAX_OBJECT_DISTANCE)42#endif4344class GodotSpace3D;4546class GodotCollisionObject3D : public GodotShapeOwner3D {47public:48enum Type {49TYPE_AREA,50TYPE_BODY,51TYPE_SOFT_BODY,52};5354private:55Type type;56RID self;57ObjectID instance_id;58uint32_t collision_layer = 1;59uint32_t collision_mask = 1;60real_t collision_priority = 1.0;6162struct Shape {63Transform3D xform;64Transform3D xform_inv;65GodotBroadPhase3D::ID bpid;66AABB aabb_cache; //for rayqueries67real_t area_cache = 0.0;68GodotShape3D *shape = nullptr;69bool disabled = false;70};7172Vector<Shape> shapes;73GodotSpace3D *space = nullptr;74Transform3D transform;75Transform3D inv_transform;76bool _static = true;7778SelfList<GodotCollisionObject3D> pending_shape_update_list;7980void _update_shapes();8182protected:83void _update_shapes_with_motion(const Vector3 &p_motion);84void _unregister_shapes();8586_FORCE_INLINE_ void _set_transform(const Transform3D &p_transform, bool p_update_shapes = true) {87#ifdef DEBUG_ENABLED8889ERR_FAIL_COND_MSG(p_transform.origin.length_squared() > MAX_OBJECT_DISTANCE_X2, "Object went too far away (more than '" + itos(MAX_OBJECT_DISTANCE) + "' units from origin).");90#endif9192transform = p_transform;93if (p_update_shapes) {94_update_shapes();95}96}97_FORCE_INLINE_ void _set_inv_transform(const Transform3D &p_transform) { inv_transform = p_transform; }98void _set_static(bool p_static);99100virtual void _shapes_changed() = 0;101void _set_space(GodotSpace3D *p_space);102103bool ray_pickable = true;104105GodotCollisionObject3D(Type p_type);106107public:108_FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }109_FORCE_INLINE_ RID get_self() const { return self; }110111_FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }112_FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }113114void _shape_changed() override;115116_FORCE_INLINE_ Type get_type() const { return type; }117void add_shape(GodotShape3D *p_shape, const Transform3D &p_transform = Transform3D(), bool p_disabled = false);118void set_shape(int p_index, GodotShape3D *p_shape);119void set_shape_transform(int p_index, const Transform3D &p_transform);120_FORCE_INLINE_ int get_shape_count() const { return shapes.size(); }121_FORCE_INLINE_ GodotShape3D *get_shape(int p_index) const {122CRASH_BAD_INDEX(p_index, shapes.size());123return shapes[p_index].shape;124}125_FORCE_INLINE_ const Transform3D &get_shape_transform(int p_index) const {126CRASH_BAD_INDEX(p_index, shapes.size());127return shapes[p_index].xform;128}129_FORCE_INLINE_ const Transform3D &get_shape_inv_transform(int p_index) const {130CRASH_BAD_INDEX(p_index, shapes.size());131return shapes[p_index].xform_inv;132}133_FORCE_INLINE_ const AABB &get_shape_aabb(int p_index) const {134CRASH_BAD_INDEX(p_index, shapes.size());135return shapes[p_index].aabb_cache;136}137_FORCE_INLINE_ real_t get_shape_area(int p_index) const {138CRASH_BAD_INDEX(p_index, shapes.size());139return shapes[p_index].area_cache;140}141142_FORCE_INLINE_ const Transform3D &get_transform() const { return transform; }143_FORCE_INLINE_ const Transform3D &get_inv_transform() const { return inv_transform; }144_FORCE_INLINE_ GodotSpace3D *get_space() const { return space; }145146_FORCE_INLINE_ void set_ray_pickable(bool p_enable) { ray_pickable = p_enable; }147_FORCE_INLINE_ bool is_ray_pickable() const { return ray_pickable; }148149void set_shape_disabled(int p_idx, bool p_disabled);150_FORCE_INLINE_ bool is_shape_disabled(int p_idx) const {151ERR_FAIL_INDEX_V(p_idx, shapes.size(), false);152return shapes[p_idx].disabled;153}154155_FORCE_INLINE_ void set_collision_layer(uint32_t p_layer) {156collision_layer = p_layer;157_shape_changed();158}159_FORCE_INLINE_ uint32_t get_collision_layer() const { return collision_layer; }160161_FORCE_INLINE_ void set_collision_mask(uint32_t p_mask) {162collision_mask = p_mask;163_shape_changed();164}165_FORCE_INLINE_ uint32_t get_collision_mask() const { return collision_mask; }166167_FORCE_INLINE_ void set_collision_priority(real_t p_priority) {168ERR_FAIL_COND_MSG(p_priority <= 0, "Priority must be greater than 0.");169collision_priority = p_priority;170_shape_changed();171}172_FORCE_INLINE_ real_t get_collision_priority() const { return collision_priority; }173174_FORCE_INLINE_ bool collides_with(GodotCollisionObject3D *p_other) const {175return p_other->collision_layer & collision_mask;176}177178_FORCE_INLINE_ bool interacts_with(const GodotCollisionObject3D *p_other) const {179return collision_layer & p_other->collision_mask || p_other->collision_layer & collision_mask;180}181182void remove_shape(GodotShape3D *p_shape) override;183void remove_shape(int p_index);184185virtual void set_space(GodotSpace3D *p_space) = 0;186187_FORCE_INLINE_ bool is_static() const { return _static; }188189virtual ~GodotCollisionObject3D() {}190};191192193