summaryrefslogtreecommitdiff
path: root/servers/physics
diff options
context:
space:
mode:
authorJuan Linietsky <red@kyoko>2015-06-12 15:52:21 -0300
committerJuan Linietsky <red@kyoko>2015-06-12 15:52:21 -0300
commit89300b70e7217feaf0be15cfe395763f555bbafa (patch)
tree248a64464f44a5060045d9d9599517b36a7b6797 /servers/physics
parent8945670bc0657c342f3f5106f757a6ac360de4c4 (diff)
added ability for Area (3D) to detect other areas
Diffstat (limited to 'servers/physics')
-rw-r--r--servers/physics/area_pair_sw.cpp69
-rw-r--r--servers/physics/area_pair_sw.h18
-rw-r--r--servers/physics/area_sw.cpp69
-rw-r--r--servers/physics/area_sw.h37
-rw-r--r--servers/physics/physics_server_sw.cpp16
-rw-r--r--servers/physics/physics_server_sw.h3
-rw-r--r--servers/physics/space_sw.cpp15
7 files changed, 219 insertions, 8 deletions
diff --git a/servers/physics/area_pair_sw.cpp b/servers/physics/area_pair_sw.cpp
index e009bd52cb..3eb96fb681 100644
--- a/servers/physics/area_pair_sw.cpp
+++ b/servers/physics/area_pair_sw.cpp
@@ -93,3 +93,72 @@ AreaPairSW::~AreaPairSW() {
body->remove_constraint(this);
area->remove_constraint(this);
}
+
+////////////////////////////////////////////////////
+
+
+
+bool Area2PairSW::setup(float p_step) {
+
+// bool result = area_a->test_collision_mask(area_b) && CollisionSolverSW::solve(area_a->get_shape(shape_a),area_a->get_transform() * area_a->get_shape_transform(shape_a),Vector2(),area_b->get_shape(shape_b),area_b->get_transform() * area_b->get_shape_transform(shape_b),Vector2(),NULL,this);
+ bool result = CollisionSolverSW::solve_static(area_a->get_shape(shape_a),area_a->get_transform() * area_a->get_shape_transform(shape_a),area_b->get_shape(shape_b),area_b->get_transform() * area_b->get_shape_transform(shape_b),NULL,this);
+
+ if (result!=colliding) {
+
+ if (result) {
+
+ if (area_b->has_area_monitor_callback() && area_a->is_monitorable())
+ area_b->add_area_to_query(area_a,shape_a,shape_b);
+
+ if (area_a->has_area_monitor_callback() && area_b->is_monitorable())
+ area_a->add_area_to_query(area_b,shape_b,shape_a);
+
+ } else {
+
+ if (area_b->has_area_monitor_callback() && area_a->is_monitorable())
+ area_b->remove_area_from_query(area_a,shape_a,shape_b);
+
+ if (area_a->has_area_monitor_callback() && area_b->is_monitorable())
+ area_a->remove_area_from_query(area_b,shape_b,shape_a);
+ }
+
+ colliding=result;
+
+ }
+
+ return false; //never do any post solving
+}
+
+void Area2PairSW::solve(float p_step) {
+
+
+}
+
+
+Area2PairSW::Area2PairSW(AreaSW *p_area_a,int p_shape_a, AreaSW *p_area_b,int p_shape_b) {
+
+
+ area_a=p_area_a;
+ area_b=p_area_b;
+ shape_a=p_shape_a;
+ shape_b=p_shape_b;
+ colliding=false;
+ area_a->add_constraint(this);
+ area_b->add_constraint(this);
+
+}
+
+Area2PairSW::~Area2PairSW() {
+
+ if (colliding) {
+
+ if (area_b->has_area_monitor_callback() && area_a->is_monitorable())
+ area_b->remove_area_from_query(area_a,shape_a,shape_b);
+
+ if (area_a->has_area_monitor_callback() && area_b->is_monitorable())
+ area_a->remove_area_from_query(area_b,shape_b,shape_a);
+ }
+
+ area_a->remove_constraint(this);
+ area_b->remove_constraint(this);
+}
diff --git a/servers/physics/area_pair_sw.h b/servers/physics/area_pair_sw.h
index 1c219a05fd..4f8087280a 100644
--- a/servers/physics/area_pair_sw.h
+++ b/servers/physics/area_pair_sw.h
@@ -49,5 +49,23 @@ public:
~AreaPairSW();
};
+
+class Area2PairSW : public ConstraintSW {
+
+ AreaSW *area_a;
+ AreaSW *area_b;
+ int shape_a;
+ int shape_b;
+ bool colliding;
+public:
+
+ bool setup(float p_step);
+ void solve(float p_step);
+
+ Area2PairSW(AreaSW *p_area_a,int p_shape_a, AreaSW *p_area_b,int p_shape_b);
+ ~Area2PairSW();
+};
+
+
#endif // AREA_PAIR__SW_H
diff --git a/servers/physics/area_sw.cpp b/servers/physics/area_sw.cpp
index aa1c530221..83be80f816 100644
--- a/servers/physics/area_sw.cpp
+++ b/servers/physics/area_sw.cpp
@@ -31,6 +31,7 @@
#include "body_sw.h"
AreaSW::BodyKey::BodyKey(BodySW *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; }
+AreaSW::BodyKey::BodyKey(AreaSW *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; }
void AreaSW::_shapes_changed() {
@@ -57,6 +58,7 @@ void AreaSW::set_space(SpaceSW *p_space) {
}
monitored_bodies.clear();
+ monitored_areas.clear();
_set_space(p_space);
}
@@ -76,11 +78,32 @@ void AreaSW::set_monitor_callback(ObjectID p_id, const StringName& p_method) {
monitor_callback_method=p_method;
monitored_bodies.clear();
+ monitored_areas.clear();
+
_shape_changed();
}
+void AreaSW::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();
+
+}
void AreaSW::set_space_override_mode(PhysicsServer::AreaSpaceOverrideMode p_mode) {
@@ -134,6 +157,15 @@ void AreaSW::_queue_monitor_update() {
}
+void AreaSW::set_monitorable(bool p_monitorable) {
+
+ if (monitorable==p_monitorable)
+ return;
+
+ monitorable=p_monitorable;
+ _set_static(!monitorable);
+}
+
void AreaSW::call_queries() {
if (monitor_callback_id && !monitored_bodies.empty()) {
@@ -168,6 +200,41 @@ void AreaSW::call_queries() {
monitored_bodies.clear();
+ if (area_monitor_callback_id && !monitored_areas.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=0;
+ return;
+ }
+
+
+
+ for (Map<BodyKey,BodyState>::Element *E=monitored_areas.front();E;E=E->next()) {
+
+ if (E->get().state==0)
+ continue; //nothing happened
+
+ res[0]=E->get().state>0 ? PhysicsServer::AREA_BODY_ADDED : PhysicsServer::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;
+
+
+ Variant::CallError ce;
+ obj->call(area_monitor_callback_method,(const Variant**)resptr,5,ce);
+ }
+ }
+
+ monitored_areas.clear();
//get_space()->area_remove_from_monitor_query_list(&monitor_query_list);
}
@@ -185,6 +252,8 @@ AreaSW::AreaSW() : CollisionObjectSW(TYPE_AREA), monitor_query_list(this), move
priority=0;
set_ray_pickable(false);
monitor_callback_id=0;
+ area_monitor_callback_id=0;
+ monitorable=false;
}
diff --git a/servers/physics/area_sw.h b/servers/physics/area_sw.h
index a875472f6b..26d6a177db 100644
--- a/servers/physics/area_sw.h
+++ b/servers/physics/area_sw.h
@@ -49,10 +49,14 @@ class AreaSW : public CollisionObjectSW{
float point_attenuation;
float density;
int priority;
+ bool monitorable;
ObjectID monitor_callback_id;
StringName monitor_callback_method;
+ ObjectID area_monitor_callback_id;
+ StringName area_monitor_callback_method;
+
SelfList<AreaSW> monitor_query_list;
SelfList<AreaSW> moved_list;
@@ -79,6 +83,8 @@ class AreaSW : public CollisionObjectSW{
_FORCE_INLINE_ BodyKey() {}
BodyKey(BodySW *p_body, uint32_t p_body_shape,uint32_t p_area_shape);
+ BodyKey(AreaSW *p_body, uint32_t p_body_shape,uint32_t p_area_shape);
+
};
struct BodyState {
@@ -90,6 +96,7 @@ class AreaSW : public CollisionObjectSW{
};
Map<BodyKey,BodyState> monitored_bodies;
+ Map<BodyKey,BodyState> monitored_areas;
//virtual void shape_changed_notify(ShapeSW *p_shape);
//virtual void shape_deleted_notify(ShapeSW *p_shape);
@@ -108,9 +115,15 @@ public:
void set_monitor_callback(ObjectID p_id, const StringName& p_method);
_FORCE_INLINE_ bool has_monitor_callback() const { return monitor_callback_id; }
+ void set_area_monitor_callback(ObjectID p_id, const StringName& p_method);
+ _FORCE_INLINE_ bool has_area_monitor_callback() const { return area_monitor_callback_id; }
+
_FORCE_INLINE_ void add_body_to_query(BodySW *p_body, uint32_t p_body_shape,uint32_t p_area_shape);
_FORCE_INLINE_ void remove_body_from_query(BodySW *p_body, uint32_t p_body_shape,uint32_t p_area_shape);
+ _FORCE_INLINE_ void add_area_to_query(AreaSW *p_area, uint32_t p_area_shape,uint32_t p_self_shape);
+ _FORCE_INLINE_ void remove_area_from_query(AreaSW *p_area, uint32_t p_area_shape,uint32_t p_self_shape);
+
void set_param(PhysicsServer::AreaParameter p_param, const Variant& p_value);
Variant get_param(PhysicsServer::AreaParameter p_param) const;
@@ -142,8 +155,8 @@ public:
_FORCE_INLINE_ void remove_constraint( ConstraintSW* p_constraint) { constraints.erase(p_constraint); }
_FORCE_INLINE_ const Set<ConstraintSW*>& get_constraints() const { return constraints; }
-
-
+ void set_monitorable(bool p_monitorable);
+ _FORCE_INLINE_ bool is_monitorable() const { return monitorable; }
void set_transform(const Transform& p_transform);
@@ -172,6 +185,26 @@ void AreaSW::remove_body_from_query(BodySW *p_body, uint32_t p_body_shape,uint32
}
+void AreaSW::add_area_to_query(AreaSW *p_area, uint32_t p_area_shape,uint32_t p_self_shape) {
+
+
+ BodyKey bk(p_area,p_area_shape,p_self_shape);
+ monitored_areas[bk].inc();
+ if (!monitor_query_list.in_list())
+ _queue_monitor_update();
+
+
+}
+void AreaSW::remove_area_from_query(AreaSW *p_area, uint32_t p_area_shape,uint32_t p_self_shape) {
+
+
+ BodyKey bk(p_area,p_area_shape,p_self_shape);
+ monitored_areas[bk].dec();
+ if (!monitor_query_list.in_list())
+ _queue_monitor_update();
+}
+
+
diff --git a/servers/physics/physics_server_sw.cpp b/servers/physics/physics_server_sw.cpp
index 03d5b7afa1..521ffae0ea 100644
--- a/servers/physics/physics_server_sw.cpp
+++ b/servers/physics/physics_server_sw.cpp
@@ -394,6 +394,14 @@ Transform PhysicsServerSW::area_get_transform(RID p_area) const {
return area->get_transform();
};
+void PhysicsServerSW::area_set_monitorable(RID p_area,bool p_monitorable) {
+
+ AreaSW *area = area_owner.get(p_area);
+ ERR_FAIL_COND(!area);
+
+ area->set_monitorable(p_monitorable);
+}
+
void PhysicsServerSW::area_set_monitor_callback(RID p_area,Object *p_receiver,const StringName& p_method) {
AreaSW *area = area_owner.get(p_area);
@@ -423,6 +431,14 @@ bool PhysicsServerSW::area_is_ray_pickable(RID p_area) const{
}
+void PhysicsServerSW::area_set_area_monitor_callback(RID p_area,Object *p_receiver,const StringName& p_method) {
+
+
+ AreaSW *area = area_owner.get(p_area);
+ ERR_FAIL_COND(!area);
+
+ area->set_area_monitor_callback(p_receiver?p_receiver->get_instance_ID():0,p_method);
+}
/* BODY API */
diff --git a/servers/physics/physics_server_sw.h b/servers/physics/physics_server_sw.h
index de2a24a378..80007b8499 100644
--- a/servers/physics/physics_server_sw.h
+++ b/servers/physics/physics_server_sw.h
@@ -128,7 +128,10 @@ public:
virtual void area_set_ray_pickable(RID p_area,bool p_enable);
virtual bool area_is_ray_pickable(RID p_area) const;
+ virtual void area_set_monitorable(RID p_area,bool p_monitorable);
+
virtual void area_set_monitor_callback(RID p_area,Object *p_receiver,const StringName& p_method);
+ virtual void area_set_area_monitor_callback(RID p_area,Object *p_receiver,const StringName& p_method);
/* BODY API */
diff --git a/servers/physics/space_sw.cpp b/servers/physics/space_sw.cpp
index a7465c57b9..d329a10f04 100644
--- a/servers/physics/space_sw.cpp
+++ b/servers/physics/space_sw.cpp
@@ -503,15 +503,18 @@ void* SpaceSW::_broadphase_pair(CollisionObjectSW *A,int p_subindex_A,CollisionO
if (type_A==CollisionObjectSW::TYPE_AREA) {
-
- ERR_FAIL_COND_V(type_B!=CollisionObjectSW::TYPE_BODY,NULL);
AreaSW *area=static_cast<AreaSW*>(A);
- BodySW *body=static_cast<BodySW*>(B);
-
+ if (type_B==CollisionObjectSW::TYPE_AREA) {
- AreaPairSW *area_pair = memnew(AreaPairSW(body,p_subindex_B,area,p_subindex_A) );
+ AreaSW *area_b=static_cast<AreaSW*>(B);
+ Area2PairSW *area2_pair = memnew(Area2PairSW(area_b,p_subindex_B,area,p_subindex_A) );
+ return area2_pair;
+ } else {
- return area_pair;
+ BodySW *body=static_cast<BodySW*>(B);
+ AreaPairSW *area_pair = memnew(AreaPairSW(body,p_subindex_B,area,p_subindex_A) );
+ return area_pair;
+ }
} else {