Path: blob/master/modules/godot_physics_2d/godot_collision_object_2d.h
21792 views
/**************************************************************************/1/* godot_collision_object_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_broad_phase_2d.h"33#include "godot_shape_2d.h"3435#include "core/templates/self_list.h"36#include "servers/physics_2d/physics_server_2d.h"3738class GodotSpace2D;3940class GodotCollisionObject2D : public GodotShapeOwner2D {41public:42enum Type {43TYPE_AREA,44TYPE_BODY45};4647private:48Type type;49RID self;50ObjectID instance_id;51ObjectID canvas_instance_id;52bool pickable = true;5354struct Shape {55Transform2D xform;56Transform2D xform_inv;57GodotBroadPhase2D::ID bpid = 0;58Rect2 aabb_cache; //for rayqueries59GodotShape2D *shape = nullptr;60bool disabled = false;61bool one_way_collision = false;62real_t one_way_collision_margin = 0.0;63Vector2 one_way_collision_direction = Vector2(0.0, 1.0);64};6566Vector<Shape> shapes;67GodotSpace2D *space = nullptr;68Transform2D transform;69Transform2D inv_transform;70uint32_t collision_mask = 1;71uint32_t collision_layer = 1;72real_t collision_priority = 1.0;73bool _static = true;7475SelfList<GodotCollisionObject2D> pending_shape_update_list;7677void _update_shapes();7879protected:80void _update_shapes_with_motion(const Vector2 &p_motion);81void _unregister_shapes();8283_FORCE_INLINE_ void _set_transform(const Transform2D &p_transform, bool p_update_shapes = true) {84transform = p_transform;85if (p_update_shapes) {86_update_shapes();87}88}89_FORCE_INLINE_ void _set_inv_transform(const Transform2D &p_transform) { inv_transform = p_transform; }90void _set_static(bool p_static);9192virtual void _shapes_changed() = 0;93void _set_space(GodotSpace2D *p_space);9495GodotCollisionObject2D(Type p_type);9697public:98_FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }99_FORCE_INLINE_ RID get_self() const { return self; }100101_FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }102_FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }103104_FORCE_INLINE_ void set_canvas_instance_id(const ObjectID &p_canvas_instance_id) { canvas_instance_id = p_canvas_instance_id; }105_FORCE_INLINE_ ObjectID get_canvas_instance_id() const { return canvas_instance_id; }106107void _shape_changed() override;108109_FORCE_INLINE_ Type get_type() const { return type; }110void add_shape(GodotShape2D *p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false);111void set_shape(int p_index, GodotShape2D *p_shape);112void set_shape_transform(int p_index, const Transform2D &p_transform);113114_FORCE_INLINE_ int get_shape_count() const { return shapes.size(); }115_FORCE_INLINE_ GodotShape2D *get_shape(int p_index) const {116CRASH_BAD_INDEX(p_index, shapes.size());117return shapes[p_index].shape;118}119_FORCE_INLINE_ const Transform2D &get_shape_transform(int p_index) const {120CRASH_BAD_INDEX(p_index, shapes.size());121return shapes[p_index].xform;122}123_FORCE_INLINE_ const Transform2D &get_shape_inv_transform(int p_index) const {124CRASH_BAD_INDEX(p_index, shapes.size());125return shapes[p_index].xform_inv;126}127_FORCE_INLINE_ const Rect2 &get_shape_aabb(int p_index) const {128CRASH_BAD_INDEX(p_index, shapes.size());129return shapes[p_index].aabb_cache;130}131132_FORCE_INLINE_ const Transform2D &get_transform() const { return transform; }133_FORCE_INLINE_ const Transform2D &get_inv_transform() const { return inv_transform; }134_FORCE_INLINE_ GodotSpace2D *get_space() const { return space; }135136void set_shape_disabled(int p_idx, bool p_disabled);137_FORCE_INLINE_ bool is_shape_disabled(int p_idx) const {138ERR_FAIL_INDEX_V(p_idx, shapes.size(), false);139return shapes[p_idx].disabled;140}141142_FORCE_INLINE_ void set_shape_as_one_way_collision(int p_idx, bool p_one_way_collision, real_t p_margin, const Vector2 &p_direction) {143CRASH_BAD_INDEX(p_idx, shapes.size());144shapes.write[p_idx].one_way_collision = p_one_way_collision;145shapes.write[p_idx].one_way_collision_margin = p_margin;146shapes.write[p_idx].one_way_collision_direction = p_direction;147}148_FORCE_INLINE_ bool is_shape_set_as_one_way_collision(int p_idx) const {149CRASH_BAD_INDEX(p_idx, shapes.size());150return shapes[p_idx].one_way_collision;151}152153_FORCE_INLINE_ real_t get_shape_one_way_collision_margin(int p_idx) const {154CRASH_BAD_INDEX(p_idx, shapes.size());155return shapes[p_idx].one_way_collision_margin;156}157158Vector2 get_shape_one_way_collision_direction(int p_idx) const {159CRASH_BAD_INDEX(p_idx, shapes.size());160return shapes[p_idx].one_way_collision_direction;161}162163void set_collision_mask(uint32_t p_mask) {164collision_mask = p_mask;165_shape_changed();166}167_FORCE_INLINE_ uint32_t get_collision_mask() const { return collision_mask; }168169void set_collision_layer(uint32_t p_layer) {170collision_layer = p_layer;171_shape_changed();172}173_FORCE_INLINE_ uint32_t get_collision_layer() const { return collision_layer; }174175_FORCE_INLINE_ void set_collision_priority(real_t p_priority) {176ERR_FAIL_COND_MSG(p_priority <= 0, "Priority must be greater than 0.");177collision_priority = p_priority;178_shape_changed();179}180_FORCE_INLINE_ real_t get_collision_priority() const { return collision_priority; }181182void remove_shape(GodotShape2D *p_shape) override;183void remove_shape(int p_index);184185virtual void set_space(GodotSpace2D *p_space) = 0;186187_FORCE_INLINE_ bool is_static() const { return _static; }188189void set_pickable(bool p_pickable) { pickable = p_pickable; }190_FORCE_INLINE_ bool is_pickable() const { return pickable; }191192_FORCE_INLINE_ bool collides_with(GodotCollisionObject2D *p_other) const {193return p_other->collision_layer & collision_mask;194}195196_FORCE_INLINE_ bool interacts_with(const GodotCollisionObject2D *p_other) const {197return collision_layer & p_other->collision_mask || p_other->collision_layer & collision_mask;198}199200virtual ~GodotCollisionObject2D() {}201};202203204