2023-01-05 13:25:55 +01:00
|
|
|
/**************************************************************************/
|
|
|
|
/* godot_collision_object_2d.h */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2018-01-05 00:50:27 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
#ifndef GODOT_COLLISION_OBJECT_2D_H
|
|
|
|
#define GODOT_COLLISION_OBJECT_2D_H
|
|
|
|
|
|
|
|
#include "godot_broad_phase_2d.h"
|
|
|
|
#include "godot_shape_2d.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2020-11-07 23:33:38 +01:00
|
|
|
#include "core/templates/self_list.h"
|
2020-03-27 19:21:27 +01:00
|
|
|
#include "servers/physics_server_2d.h"
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
class GodotSpace2D;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
class GodotCollisionObject2D : public GodotShapeOwner2D {
|
2014-02-10 02:10:30 +01:00
|
|
|
public:
|
|
|
|
enum Type {
|
|
|
|
TYPE_AREA,
|
|
|
|
TYPE_BODY
|
|
|
|
};
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
private:
|
2014-02-10 02:10:30 +01:00
|
|
|
Type type;
|
|
|
|
RID self;
|
|
|
|
ObjectID instance_id;
|
2018-08-25 00:03:26 +02:00
|
|
|
ObjectID canvas_instance_id;
|
2021-09-14 11:01:49 +02:00
|
|
|
bool pickable = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
struct Shape {
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D xform;
|
|
|
|
Transform2D xform_inv;
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotBroadPhase2D::ID bpid = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
Rect2 aabb_cache; //for rayqueries
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotShape2D *shape = nullptr;
|
2021-09-14 11:01:49 +02:00
|
|
|
bool disabled = false;
|
|
|
|
bool one_way_collision = false;
|
|
|
|
real_t one_way_collision_margin = 0.0;
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Shape> shapes;
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotSpace2D *space = nullptr;
|
2017-01-11 04:52:51 +01:00
|
|
|
Transform2D transform;
|
|
|
|
Transform2D inv_transform;
|
2021-09-14 11:01:49 +02:00
|
|
|
uint32_t collision_mask = 1;
|
|
|
|
uint32_t collision_layer = 1;
|
2022-08-10 18:45:36 +02:00
|
|
|
real_t collision_priority = 1.0;
|
2021-09-14 11:01:49 +02:00
|
|
|
bool _static = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
SelfList<GodotCollisionObject2D> pending_shape_update_list;
|
2019-06-21 13:33:01 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
void _update_shapes();
|
|
|
|
|
|
|
|
protected:
|
2017-03-05 16:44:50 +01:00
|
|
|
void _update_shapes_with_motion(const Vector2 &p_motion);
|
2014-02-10 02:10:30 +01:00
|
|
|
void _unregister_shapes();
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void _set_transform(const Transform2D &p_transform, bool p_update_shapes = true) {
|
|
|
|
transform = p_transform;
|
|
|
|
if (p_update_shapes) {
|
|
|
|
_update_shapes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ void _set_inv_transform(const Transform2D &p_transform) { inv_transform = p_transform; }
|
2014-02-10 02:10:30 +01:00
|
|
|
void _set_static(bool p_static);
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
virtual void _shapes_changed() = 0;
|
2021-10-18 21:24:30 +02:00
|
|
|
void _set_space(GodotSpace2D *p_space);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
GodotCollisionObject2D(Type p_type);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
public:
|
|
|
|
_FORCE_INLINE_ void set_self(const RID &p_self) { self = p_self; }
|
2014-02-10 02:10:30 +01:00
|
|
|
_FORCE_INLINE_ RID get_self() const { return self; }
|
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ void set_instance_id(const ObjectID &p_instance_id) { instance_id = p_instance_id; }
|
2014-02-10 02:10:30 +01:00
|
|
|
_FORCE_INLINE_ ObjectID get_instance_id() const { return instance_id; }
|
|
|
|
|
2018-08-25 00:03:26 +02:00
|
|
|
_FORCE_INLINE_ void set_canvas_instance_id(const ObjectID &p_canvas_instance_id) { canvas_instance_id = p_canvas_instance_id; }
|
|
|
|
_FORCE_INLINE_ ObjectID get_canvas_instance_id() const { return canvas_instance_id; }
|
|
|
|
|
2022-04-05 12:40:26 +02:00
|
|
|
void _shape_changed() override;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_FORCE_INLINE_ Type get_type() const { return type; }
|
2021-10-18 21:24:30 +02:00
|
|
|
void add_shape(GodotShape2D *p_shape, const Transform2D &p_transform = Transform2D(), bool p_disabled = false);
|
|
|
|
void set_shape(int p_index, GodotShape2D *p_shape);
|
2017-03-05 16:44:50 +01:00
|
|
|
void set_shape_transform(int p_index, const Transform2D &p_transform);
|
2014-10-16 05:06:34 +02:00
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
_FORCE_INLINE_ int get_shape_count() const { return shapes.size(); }
|
2021-10-18 21:24:30 +02:00
|
|
|
_FORCE_INLINE_ GodotShape2D *get_shape(int p_index) const {
|
2018-09-27 13:11:41 +02:00
|
|
|
CRASH_BAD_INDEX(p_index, shapes.size());
|
2017-10-11 17:19:42 +02:00
|
|
|
return shapes[p_index].shape;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ const Transform2D &get_shape_transform(int p_index) const {
|
2018-09-27 13:11:41 +02:00
|
|
|
CRASH_BAD_INDEX(p_index, shapes.size());
|
2017-10-11 17:19:42 +02:00
|
|
|
return shapes[p_index].xform;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ const Transform2D &get_shape_inv_transform(int p_index) const {
|
2018-09-27 13:11:41 +02:00
|
|
|
CRASH_BAD_INDEX(p_index, shapes.size());
|
2017-10-11 17:19:42 +02:00
|
|
|
return shapes[p_index].xform_inv;
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ const Rect2 &get_shape_aabb(int p_index) const {
|
2018-09-27 13:11:41 +02:00
|
|
|
CRASH_BAD_INDEX(p_index, shapes.size());
|
2017-10-11 17:19:42 +02:00
|
|
|
return shapes[p_index].aabb_cache;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-04-20 03:38:11 +02:00
|
|
|
_FORCE_INLINE_ const Transform2D &get_transform() const { return transform; }
|
|
|
|
_FORCE_INLINE_ const Transform2D &get_inv_transform() const { return inv_transform; }
|
2021-10-18 21:24:30 +02:00
|
|
|
_FORCE_INLINE_ GodotSpace2D *get_space() const { return space; }
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-06-23 01:36:43 +02:00
|
|
|
void set_shape_disabled(int p_idx, bool p_disabled);
|
|
|
|
_FORCE_INLINE_ bool is_shape_disabled(int p_idx) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_idx, shapes.size(), false);
|
2017-10-11 17:19:42 +02:00
|
|
|
return shapes[p_idx].disabled;
|
|
|
|
}
|
2017-06-24 04:30:43 +02:00
|
|
|
|
2021-01-28 07:34:26 +01:00
|
|
|
_FORCE_INLINE_ void set_shape_as_one_way_collision(int p_idx, bool p_one_way_collision, real_t p_margin) {
|
2018-09-27 13:11:41 +02:00
|
|
|
CRASH_BAD_INDEX(p_idx, shapes.size());
|
2018-07-25 03:11:03 +02:00
|
|
|
shapes.write[p_idx].one_way_collision = p_one_way_collision;
|
2019-01-18 18:15:05 +01:00
|
|
|
shapes.write[p_idx].one_way_collision_margin = p_margin;
|
2017-10-11 17:19:42 +02:00
|
|
|
}
|
|
|
|
_FORCE_INLINE_ bool is_shape_set_as_one_way_collision(int p_idx) const {
|
2018-09-27 13:11:41 +02:00
|
|
|
CRASH_BAD_INDEX(p_idx, shapes.size());
|
2017-10-11 17:19:42 +02:00
|
|
|
return shapes[p_idx].one_way_collision;
|
|
|
|
}
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2021-01-28 07:34:26 +01:00
|
|
|
_FORCE_INLINE_ real_t get_shape_one_way_collision_margin(int p_idx) const {
|
2019-01-18 18:15:05 +01:00
|
|
|
CRASH_BAD_INDEX(p_idx, shapes.size());
|
|
|
|
return shapes[p_idx].one_way_collision_margin;
|
|
|
|
}
|
|
|
|
|
2020-06-27 19:34:16 +02:00
|
|
|
void set_collision_mask(uint32_t p_mask) {
|
|
|
|
collision_mask = p_mask;
|
|
|
|
_shape_changed();
|
|
|
|
}
|
2015-05-03 21:47:21 +02:00
|
|
|
_FORCE_INLINE_ uint32_t get_collision_mask() const { return collision_mask; }
|
2014-02-19 15:57:14 +01:00
|
|
|
|
2020-06-27 19:34:16 +02:00
|
|
|
void set_collision_layer(uint32_t p_layer) {
|
|
|
|
collision_layer = p_layer;
|
|
|
|
_shape_changed();
|
|
|
|
}
|
2017-06-13 17:45:01 +02:00
|
|
|
_FORCE_INLINE_ uint32_t get_collision_layer() const { return collision_layer; }
|
2014-05-14 06:22:15 +02:00
|
|
|
|
2022-08-10 18:45:36 +02:00
|
|
|
_FORCE_INLINE_ void set_collision_priority(real_t p_priority) {
|
|
|
|
ERR_FAIL_COND_MSG(p_priority <= 0, "Priority must be greater than 0.");
|
|
|
|
collision_priority = p_priority;
|
|
|
|
_shape_changed();
|
|
|
|
}
|
|
|
|
_FORCE_INLINE_ real_t get_collision_priority() const { return collision_priority; }
|
|
|
|
|
2022-04-05 12:40:26 +02:00
|
|
|
void remove_shape(GodotShape2D *p_shape) override;
|
2014-02-10 02:10:30 +01:00
|
|
|
void remove_shape(int p_index);
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
virtual void set_space(GodotSpace2D *p_space) = 0;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
_FORCE_INLINE_ bool is_static() const { return _static; }
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-05 16:44:50 +01:00
|
|
|
void set_pickable(bool p_pickable) { pickable = p_pickable; }
|
2015-03-22 05:46:18 +01:00
|
|
|
_FORCE_INLINE_ bool is_pickable() const { return pickable; }
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
_FORCE_INLINE_ bool collides_with(GodotCollisionObject2D *p_other) const {
|
2021-07-15 21:38:48 +02:00
|
|
|
return p_other->collision_layer & collision_mask;
|
2020-10-08 13:45:03 +02:00
|
|
|
}
|
|
|
|
|
2022-02-04 17:46:10 +01:00
|
|
|
_FORCE_INLINE_ bool interacts_with(const GodotCollisionObject2D *p_other) const {
|
2017-06-13 17:45:01 +02:00
|
|
|
return collision_layer & p_other->collision_mask || p_other->collision_layer & collision_mask;
|
2015-05-03 21:47:21 +02:00
|
|
|
}
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
virtual ~GodotCollisionObject2D() {}
|
2014-02-10 02:10:30 +01:00
|
|
|
};
|
|
|
|
|
2021-10-18 21:24:30 +02:00
|
|
|
#endif // GODOT_COLLISION_OBJECT_2D_H
|