Path: blob/master/modules/godot_physics_2d/godot_collision_object_2d.h
10277 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_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;63};6465Vector<Shape> shapes;66GodotSpace2D *space = nullptr;67Transform2D transform;68Transform2D inv_transform;69uint32_t collision_mask = 1;70uint32_t collision_layer = 1;71real_t collision_priority = 1.0;72bool _static = true;7374SelfList<GodotCollisionObject2D> pending_shape_update_list;7576void _update_shapes();7778protected:79void _update_shapes_with_motion(const Vector2 &p_motion);80void _unregister_shapes();8182_FORCE_INLINE_ void _set_transform(const Transform2D &p_transform, bool p_update_shapes = true) {83transform = p_transform;84if (p_update_shapes) {85_update_shapes();86}87}88_FORCE_INLINE_ void _set_inv_transform(const Transform2D &p_transform) { inv_transform = p_transform; }89void _set_static(bool p_static);9091virtual void _shapes_changed() = 0;92void _set_space(GodotSpace2D *p_space);9394GodotCollisionObject2D(Type p_type);9596public:97_FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }98_FORCE_INLINE_ RID get_self() const { return self; }99100_FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }101_FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }102103_FORCE_INLINE_ void set_canvas_instance_id(const ObjectID &p_canvas_instance_id) { canvas_instance_id = p_canvas_instance_id; }104_FORCE_INLINE_ ObjectID get_canvas_instance_id() const { return canvas_instance_id; }105106void _shape_changed() override;107108_FORCE_INLINE_ Type get_type() const { return type; }109void add_shape(GodotShape2D *p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false);110void set_shape(int p_index, GodotShape2D *p_shape);111void set_shape_transform(int p_index, const Transform2D &p_transform);112113_FORCE_INLINE_ int get_shape_count() const { return shapes.size(); }114_FORCE_INLINE_ GodotShape2D *get_shape(int p_index) const {115CRASH_BAD_INDEX(p_index, shapes.size());116return shapes[p_index].shape;117}118_FORCE_INLINE_ const Transform2D &get_shape_transform(int p_index) const {119CRASH_BAD_INDEX(p_index, shapes.size());120return shapes[p_index].xform;121}122_FORCE_INLINE_ const Transform2D &get_shape_inv_transform(int p_index) const {123CRASH_BAD_INDEX(p_index, shapes.size());124return shapes[p_index].xform_inv;125}126_FORCE_INLINE_ const Rect2 &get_shape_aabb(int p_index) const {127CRASH_BAD_INDEX(p_index, shapes.size());128return shapes[p_index].aabb_cache;129}130131_FORCE_INLINE_ const Transform2D &get_transform() const { return transform; }132_FORCE_INLINE_ const Transform2D &get_inv_transform() const { return inv_transform; }133_FORCE_INLINE_ GodotSpace2D *get_space() const { return space; }134135void set_shape_disabled(int p_idx, bool p_disabled);136_FORCE_INLINE_ bool is_shape_disabled(int p_idx) const {137ERR_FAIL_INDEX_V(p_idx, shapes.size(), false);138return shapes[p_idx].disabled;139}140141_FORCE_INLINE_ void set_shape_as_one_way_collision(int p_idx, bool p_one_way_collision, real_t p_margin) {142CRASH_BAD_INDEX(p_idx, shapes.size());143shapes.write[p_idx].one_way_collision = p_one_way_collision;144shapes.write[p_idx].one_way_collision_margin = p_margin;145}146_FORCE_INLINE_ bool is_shape_set_as_one_way_collision(int p_idx) const {147CRASH_BAD_INDEX(p_idx, shapes.size());148return shapes[p_idx].one_way_collision;149}150151_FORCE_INLINE_ real_t get_shape_one_way_collision_margin(int p_idx) const {152CRASH_BAD_INDEX(p_idx, shapes.size());153return shapes[p_idx].one_way_collision_margin;154}155156void set_collision_mask(uint32_t p_mask) {157collision_mask = p_mask;158_shape_changed();159}160_FORCE_INLINE_ uint32_t get_collision_mask() const { return collision_mask; }161162void set_collision_layer(uint32_t p_layer) {163collision_layer = p_layer;164_shape_changed();165}166_FORCE_INLINE_ uint32_t get_collision_layer() const { return collision_layer; }167168_FORCE_INLINE_ void set_collision_priority(real_t p_priority) {169ERR_FAIL_COND_MSG(p_priority <= 0, "Priority must be greater than 0.");170collision_priority = p_priority;171_shape_changed();172}173_FORCE_INLINE_ real_t get_collision_priority() const { return collision_priority; }174175void remove_shape(GodotShape2D *p_shape) override;176void remove_shape(int p_index);177178virtual void set_space(GodotSpace2D *p_space) = 0;179180_FORCE_INLINE_ bool is_static() const { return _static; }181182void set_pickable(bool p_pickable) { pickable = p_pickable; }183_FORCE_INLINE_ bool is_pickable() const { return pickable; }184185_FORCE_INLINE_ bool collides_with(GodotCollisionObject2D *p_other) const {186return p_other->collision_layer & collision_mask;187}188189_FORCE_INLINE_ bool interacts_with(const GodotCollisionObject2D *p_other) const {190return collision_layer & p_other->collision_mask || p_other->collision_layer & collision_mask;191}192193virtual ~GodotCollisionObject2D() {}194};195196197