Path: blob/master/scene/2d/physics/collision_object_2d.h
21344 views
/**************************************************************************/1/* 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 "scene/2d/node_2d.h"33#include "scene/main/viewport.h"34#include "scene/resources/2d/shape_2d.h"35#include "servers/physics_2d/physics_server_2d.h"3637class CollisionObject2D : public Node2D {38GDCLASS(CollisionObject2D, Node2D);3940public:41static constexpr AncestralClass static_ancestral_class = AncestralClass::COLLISION_OBJECT_2D;4243enum DisableMode {44DISABLE_MODE_REMOVE,45DISABLE_MODE_MAKE_STATIC,46DISABLE_MODE_KEEP_ACTIVE,47};4849private:50uint32_t collision_layer = 1;51uint32_t collision_mask = 1;52real_t collision_priority = 1.0;5354bool area = false;55RID rid;56uint32_t callback_lock = 0;57bool pickable = false;5859DisableMode disable_mode = DISABLE_MODE_REMOVE;6061PhysicsServer2D::BodyMode body_mode = PhysicsServer2D::BODY_MODE_STATIC;6263struct ShapeData {64ObjectID owner_id;65Transform2D xform;66struct Shape {67Ref<Shape2D> shape;68int index = 0;69};7071Vector<Shape> shapes;7273bool disabled = false;74bool one_way_collision = false;75real_t one_way_collision_margin = 0.0;76Vector2 one_way_collision_direction = Vector2(0.0, 1.0);77};7879int total_subshapes = 0;8081RBMap<uint32_t, ShapeData> shapes;82bool only_update_transform_changes = false; // This is used for sync to physics.8384void _apply_disabled();85void _apply_enabled();8687protected:88_FORCE_INLINE_ void lock_callback() { callback_lock++; }89_FORCE_INLINE_ void unlock_callback() {90ERR_FAIL_COND(callback_lock == 0);91callback_lock--;92}9394CollisionObject2D(RID p_rid, bool p_area);9596void _notification(int p_what);97static void _bind_methods();9899void _update_pickable();100friend class Viewport;101void _input_event_call(Viewport *p_viewport, const Ref<InputEvent> &p_input_event, int p_shape);102void _mouse_enter();103void _mouse_exit();104105void _mouse_shape_enter(int p_shape);106void _mouse_shape_exit(int p_shape);107108void set_only_update_transform_changes(bool p_enable);109bool is_only_update_transform_changes_enabled() const;110111void set_body_mode(PhysicsServer2D::BodyMode p_mode);112113virtual void _space_changed(const RID &p_new_space);114115GDVIRTUAL3(_input_event, RequiredParam<Viewport>, RequiredParam<InputEvent>, int)116GDVIRTUAL0(_mouse_enter)117GDVIRTUAL0(_mouse_exit)118GDVIRTUAL1(_mouse_shape_enter, int)119GDVIRTUAL1(_mouse_shape_exit, int)120public:121void set_collision_layer(uint32_t p_layer);122uint32_t get_collision_layer() const;123124void set_collision_mask(uint32_t p_mask);125uint32_t get_collision_mask() const;126127void set_collision_layer_value(int p_layer_number, bool p_value);128bool get_collision_layer_value(int p_layer_number) const;129130void set_collision_mask_value(int p_layer_number, bool p_value);131bool get_collision_mask_value(int p_layer_number) const;132133void set_collision_priority(real_t p_priority);134real_t get_collision_priority() const;135136void set_disable_mode(DisableMode p_mode);137DisableMode get_disable_mode() const;138139uint32_t create_shape_owner(Object *p_owner);140void remove_shape_owner(uint32_t owner);141void get_shape_owners(List<uint32_t> *r_owners);142PackedInt32Array _get_shape_owners();143144void shape_owner_set_transform(uint32_t p_owner, const Transform2D &p_transform);145Transform2D shape_owner_get_transform(uint32_t p_owner) const;146Object *shape_owner_get_owner(uint32_t p_owner) const;147148void shape_owner_set_disabled(uint32_t p_owner, bool p_disabled);149bool is_shape_owner_disabled(uint32_t p_owner) const;150151void shape_owner_set_one_way_collision(uint32_t p_owner, bool p_enable);152bool is_shape_owner_one_way_collision_enabled(uint32_t p_owner) const;153154void shape_owner_set_one_way_collision_margin(uint32_t p_owner, real_t p_margin);155real_t get_shape_owner_one_way_collision_margin(uint32_t p_owner) const;156157void shape_owner_set_one_way_collision_direction(uint32_t p_owner, const Vector2 &p_direction);158Vector2 get_shape_owner_one_way_collision_direction(uint32_t p_owner) const;159160void shape_owner_add_shape(uint32_t p_owner, RequiredParam<Shape2D> rp_shape);161int shape_owner_get_shape_count(uint32_t p_owner) const;162Ref<Shape2D> shape_owner_get_shape(uint32_t p_owner, int p_shape) const;163int shape_owner_get_shape_index(uint32_t p_owner, int p_shape) const;164165void shape_owner_remove_shape(uint32_t p_owner, int p_shape);166void shape_owner_clear_shapes(uint32_t p_owner);167168uint32_t shape_find_owner(int p_shape_index) const;169170void set_pickable(bool p_enabled);171bool is_pickable() const;172173PackedStringArray get_configuration_warnings() const override;174175_FORCE_INLINE_ RID get_rid() const { return rid; }176177CollisionObject2D();178~CollisionObject2D();179};180181VARIANT_ENUM_CAST(CollisionObject2D::DisableMode);182183184