godot/servers/physics_3d/area_3d_sw.cpp

297 lines
9.0 KiB
C++
Raw Normal View History

2014-02-10 09:10:30 +08:00
/*************************************************************************/
/* area_3d_sw.cpp */
2014-02-10 09:10:30 +08:00
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
2014-02-10 09:10:30 +08:00
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
2014-02-10 09:10:30 +08:00
/* */
/* 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. */
/*************************************************************************/
#include "area_3d_sw.h"
#include "body_3d_sw.h"
#include "space_3d_sw.h"
2014-02-10 09:10:30 +08:00
Area3DSW::BodyKey::BodyKey(Body3DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
rid = p_body->get_self();
instance_id = p_body->get_instance_id();
body_shape = p_body_shape;
area_shape = p_area_shape;
}
Area3DSW::BodyKey::BodyKey(Area3DSW *p_body, uint32_t p_body_shape, uint32_t p_area_shape) {
rid = p_body->get_self();
instance_id = p_body->get_instance_id();
body_shape = p_body_shape;
area_shape = p_area_shape;
}
2014-02-10 09:10:30 +08:00
void Area3DSW::_shapes_changed() {
if (!moved_list.in_list() && get_space()) {
get_space()->area_add_to_moved_list(&moved_list);
}
2014-02-10 09:10:30 +08:00
}
void Area3DSW::set_transform(const Transform3D &p_transform) {
if (!moved_list.in_list() && get_space()) {
2014-02-10 09:10:30 +08:00
get_space()->area_add_to_moved_list(&moved_list);
}
2014-02-10 09:10:30 +08:00
_set_transform(p_transform);
_set_inv_transform(p_transform.affine_inverse());
2014-02-10 09:10:30 +08:00
}
void Area3DSW::set_space(Space3DSW *p_space) {
2014-02-10 09:10:30 +08:00
if (get_space()) {
if (monitor_query_list.in_list()) {
2014-02-10 09:10:30 +08:00
get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
}
if (moved_list.in_list()) {
2014-02-10 09:10:30 +08:00
get_space()->area_remove_from_moved_list(&moved_list);
}
2014-02-10 09:10:30 +08:00
}
monitored_bodies.clear();
monitored_areas.clear();
2014-02-10 09:10:30 +08:00
_set_space(p_space);
}
void Area3DSW::set_monitor_callback(ObjectID p_id, const StringName &p_method) {
if (p_id == monitor_callback_id) {
monitor_callback_method = p_method;
2014-02-10 09:10:30 +08:00
return;
}
_unregister_shapes();
monitor_callback_id = p_id;
monitor_callback_method = p_method;
2014-02-10 09:10:30 +08:00
monitored_bodies.clear();
monitored_areas.clear();
2014-02-10 09:10:30 +08:00
_shape_changed();
if (!moved_list.in_list() && get_space()) {
2015-09-04 10:24:55 +08:00
get_space()->area_add_to_moved_list(&moved_list);
}
2014-02-10 09:10:30 +08:00
}
void Area3DSW::set_area_monitor_callback(ObjectID p_id, const StringName &p_method) {
if (p_id == area_monitor_callback_id) {
area_monitor_callback_method = p_method;
return;
}
_unregister_shapes();
area_monitor_callback_id = p_id;
area_monitor_callback_method = p_method;
monitored_bodies.clear();
monitored_areas.clear();
_shape_changed();
if (!moved_list.in_list() && get_space()) {
2015-09-04 10:24:55 +08:00
get_space()->area_add_to_moved_list(&moved_list);
}
}
2014-02-10 09:10:30 +08:00
void Area3DSW::set_space_override_mode(PhysicsServer3D::AreaSpaceOverrideMode p_mode) {
bool do_override = p_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;
if (do_override == (space_override_mode != PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED)) {
2014-02-10 09:10:30 +08:00
return;
}
2014-02-10 09:10:30 +08:00
_unregister_shapes();
space_override_mode = p_mode;
2014-02-10 09:10:30 +08:00
_shape_changed();
}
void Area3DSW::set_param(PhysicsServer3D::AreaParameter p_param, const Variant &p_value) {
switch (p_param) {
case PhysicsServer3D::AREA_PARAM_GRAVITY:
gravity = p_value;
break;
case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR:
gravity_vector = p_value;
break;
case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT:
gravity_is_point = p_value;
break;
case PhysicsServer3D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
gravity_distance_scale = p_value;
break;
case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
point_attenuation = p_value;
break;
case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP:
linear_damp = p_value;
break;
case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP:
angular_damp = p_value;
break;
case PhysicsServer3D::AREA_PARAM_PRIORITY:
priority = p_value;
break;
2014-02-10 09:10:30 +08:00
}
}
Variant Area3DSW::get_param(PhysicsServer3D::AreaParameter p_param) const {
switch (p_param) {
case PhysicsServer3D::AREA_PARAM_GRAVITY:
return gravity;
case PhysicsServer3D::AREA_PARAM_GRAVITY_VECTOR:
return gravity_vector;
case PhysicsServer3D::AREA_PARAM_GRAVITY_IS_POINT:
return gravity_is_point;
case PhysicsServer3D::AREA_PARAM_GRAVITY_DISTANCE_SCALE:
return gravity_distance_scale;
case PhysicsServer3D::AREA_PARAM_GRAVITY_POINT_ATTENUATION:
return point_attenuation;
case PhysicsServer3D::AREA_PARAM_LINEAR_DAMP:
return linear_damp;
case PhysicsServer3D::AREA_PARAM_ANGULAR_DAMP:
return angular_damp;
case PhysicsServer3D::AREA_PARAM_PRIORITY:
return priority;
2014-02-10 09:10:30 +08:00
}
return Variant();
}
void Area3DSW::_queue_monitor_update() {
2014-02-10 09:10:30 +08:00
ERR_FAIL_COND(!get_space());
if (!monitor_query_list.in_list()) {
2014-02-10 09:10:30 +08:00
get_space()->area_add_to_monitor_query_list(&monitor_query_list);
}
2014-02-10 09:10:30 +08:00
}
void Area3DSW::set_monitorable(bool p_monitorable) {
if (monitorable == p_monitorable) {
return;
}
monitorable = p_monitorable;
_set_static(!monitorable);
}
void Area3DSW::call_queries() {
2020-12-15 20:04:21 +08:00
if (monitor_callback_id.is_valid() && !monitored_bodies.is_empty()) {
2014-02-10 09:10:30 +08:00
Variant res[5];
Variant *resptr[5];
for (int i = 0; i < 5; i++) {
resptr[i] = &res[i];
}
2014-02-10 09:10:30 +08:00
Object *obj = ObjectDB::get_instance(monitor_callback_id);
if (!obj) {
monitored_bodies.clear();
monitor_callback_id = ObjectID();
2014-02-10 09:10:30 +08:00
return;
}
for (Map<BodyKey, BodyState>::Element *E = monitored_bodies.front(); E;) {
if (E->get().state == 0) { // Nothing happened
Map<BodyKey, BodyState>::Element *next = E->next();
monitored_bodies.erase(E);
E = next;
continue;
}
2014-02-10 09:10:30 +08:00
res[0] = E->get().state > 0 ? PhysicsServer3D::AREA_BODY_ADDED : PhysicsServer3D::AREA_BODY_REMOVED;
res[1] = E->key().rid;
res[2] = E->key().instance_id;
res[3] = E->key().body_shape;
res[4] = E->key().area_shape;
2014-02-10 09:10:30 +08:00
Map<BodyKey, BodyState>::Element *next = E->next();
monitored_bodies.erase(E);
E = next;
Callable::CallError ce;
obj->call(monitor_callback_method, (const Variant **)resptr, 5, ce);
2014-02-10 09:10:30 +08:00
}
}
2020-12-15 20:04:21 +08:00
if (area_monitor_callback_id.is_valid() && !monitored_areas.is_empty()) {
Variant res[5];
Variant *resptr[5];
for (int i = 0; i < 5; i++) {
resptr[i] = &res[i];
}
Object *obj = ObjectDB::get_instance(area_monitor_callback_id);
if (!obj) {
monitored_areas.clear();
area_monitor_callback_id = ObjectID();
return;
}
for (Map<BodyKey, BodyState>::Element *E = monitored_areas.front(); E;) {
if (E->get().state == 0) { // Nothing happened
Map<BodyKey, BodyState>::Element *next = E->next();
monitored_areas.erase(E);
E = next;
continue;
}
res[0] = E->get().state > 0 ? PhysicsServer3D::AREA_BODY_ADDED : PhysicsServer3D::AREA_BODY_REMOVED;
res[1] = E->key().rid;
res[2] = E->key().instance_id;
res[3] = E->key().body_shape;
res[4] = E->key().area_shape;
Map<BodyKey, BodyState>::Element *next = E->next();
monitored_areas.erase(E);
E = next;
Callable::CallError ce;
obj->call(area_monitor_callback_method, (const Variant **)resptr, 5, ce);
}
}
2014-02-10 09:10:30 +08:00
}
Area3DSW::Area3DSW() :
CollisionObject3DSW(TYPE_AREA),
monitor_query_list(this),
moved_list(this) {
2014-02-10 09:10:30 +08:00
_set_static(true); //areas are never active
space_override_mode = PhysicsServer3D::AREA_SPACE_OVERRIDE_DISABLED;
gravity = 9.80665;
gravity_vector = Vector3(0, -1, 0);
gravity_is_point = false;
gravity_distance_scale = 0;
point_attenuation = 1;
angular_damp = 0.1;
linear_damp = 0.1;
priority = 0;
Huge Amount of BugFix -=-=-=-=-=-=-=-=-=-=- -Fixes to Collada Exporter (avoid crash situtions) -Fixed to Collada Importer (Fixed Animation Optimizer Bugs) -Fixes to RigidBody/RigidBody2D body_enter/body_exit, was buggy -Fixed ability for RigidBody/RigidBody2D to get contacts reported and bodyin/out in Kinematic mode. -Added proper trigger support for 3D Physics shapes -Changed proper value for Z-Offset in OmniLight -Fixed spot attenuation bug in SpotLight -Fixed some 3D and 2D spatial soudn bugs related to distance attenuation. -Fixed bugs in EventPlayer (channels were muted by default) -Fix in ButtonGroup (get nodes in group are now returned in order) -Fixed Linear->SRGB Conversion, previous algo sucked, new algo works OK -Changed SRGB->Linear conversion to use hardware if supported, improves texture quality a lot -Fixed options for Y-Fov and X-Fov in camera, should be more intuitive. -Fixed bugs related to viewports and transparency Huge Amount of New Stuff: -=-=-=-=-=-=-=-==-=-=-=- -Ability to manually advance an AnimationPlayer that is inactive (with advance() function) -More work in WinRT platform -Added XY normalmap support, imports on this format by default. Reduces normlmap size and enables much nice compression using LATC -Added Anisotropic filter support to textures, can be specified on import -Added support for Non-Square, Isometric and Hexagonal tilemaps in TileMap. -Added Isometric Dungeon demo. -Added simple hexagonal map demo. -Added Truck-Town demo. Shows how most types of joints and vehicles are used. Please somebody make a nicer town, this one is too hardcore. -Added an Object-Picking API to both RigidBody and Area! (and relevant demo)
2014-10-03 11:10:51 +08:00
set_ray_pickable(false);
monitorable = false;
2014-02-10 09:10:30 +08:00
}
Area3DSW::~Area3DSW() {
2014-02-10 09:10:30 +08:00
}