summaryrefslogtreecommitdiff
path: root/servers/physics_2d
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2015-05-03 16:47:21 -0300
committerJuan Linietsky <reduzio@gmail.com>2015-05-03 16:47:21 -0300
commitaf068439829a3fec3a76c4c5d4dd2dba71ab6c5f (patch)
treea50bea4ceb2ef7e70e86303148830377e19028ce /servers/physics_2d
parent5f2aac4bbe14d1256feebd4bb12d8b61e5b55f3d (diff)
-new collision layer & mask system for 2D, for more flexible collision masking
Diffstat (limited to 'servers/physics_2d')
-rw-r--r--servers/physics_2d/area_pair_2d_sw.cpp4
-rw-r--r--servers/physics_2d/body_pair_2d_sw.cpp2
-rw-r--r--servers/physics_2d/collision_object_2d_sw.cpp2
-rw-r--r--servers/physics_2d/collision_object_2d_sw.h11
-rw-r--r--servers/physics_2d/physics_2d_server_sw.cpp24
-rw-r--r--servers/physics_2d/physics_2d_server_sw.h6
-rw-r--r--servers/physics_2d/space_2d_sw.cpp2
7 files changed, 37 insertions, 14 deletions
diff --git a/servers/physics_2d/area_pair_2d_sw.cpp b/servers/physics_2d/area_pair_2d_sw.cpp
index f73fbb628b..3b1705bd56 100644
--- a/servers/physics_2d/area_pair_2d_sw.cpp
+++ b/servers/physics_2d/area_pair_2d_sw.cpp
@@ -32,7 +32,7 @@
bool AreaPair2DSW::setup(float p_step) {
- bool result = CollisionSolver2DSW::solve(body->get_shape(body_shape),body->get_transform() * body->get_shape_transform(body_shape),Vector2(),area->get_shape(area_shape),area->get_transform() * area->get_shape_transform(area_shape),Vector2(),NULL,this);
+ bool result = area->test_collision_mask(body) && CollisionSolver2DSW::solve(body->get_shape(body_shape),body->get_transform() * body->get_shape_transform(body_shape),Vector2(),area->get_shape(area_shape),area->get_transform() * area->get_shape_transform(area_shape),Vector2(),NULL,this);
if (result!=colliding) {
@@ -102,7 +102,7 @@ AreaPair2DSW::~AreaPair2DSW() {
bool Area2Pair2DSW::setup(float p_step) {
- bool result = CollisionSolver2DSW::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 = area_a->test_collision_mask(area_b) && CollisionSolver2DSW::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);
if (result!=colliding) {
diff --git a/servers/physics_2d/body_pair_2d_sw.cpp b/servers/physics_2d/body_pair_2d_sw.cpp
index a2402d1473..8913e396d2 100644
--- a/servers/physics_2d/body_pair_2d_sw.cpp
+++ b/servers/physics_2d/body_pair_2d_sw.cpp
@@ -234,7 +234,7 @@ bool BodyPair2DSW::setup(float p_step) {
//cannot collide
- if ((A->get_layer_mask()&B->get_layer_mask())==0 || A->has_exception(B->get_self()) || B->has_exception(A->get_self()) || (A->get_mode()<=Physics2DServer::BODY_MODE_KINEMATIC && B->get_mode()<=Physics2DServer::BODY_MODE_KINEMATIC && A->get_max_contacts_reported()==0 && B->get_max_contacts_reported()==0)) {
+ if (!A->test_collision_mask(B) || A->has_exception(B->get_self()) || B->has_exception(A->get_self()) || (A->get_mode()<=Physics2DServer::BODY_MODE_KINEMATIC && B->get_mode()<=Physics2DServer::BODY_MODE_KINEMATIC && A->get_max_contacts_reported()==0 && B->get_max_contacts_reported()==0)) {
collided=false;
return false;
}
diff --git a/servers/physics_2d/collision_object_2d_sw.cpp b/servers/physics_2d/collision_object_2d_sw.cpp
index 8160f22a31..7c8e223c57 100644
--- a/servers/physics_2d/collision_object_2d_sw.cpp
+++ b/servers/physics_2d/collision_object_2d_sw.cpp
@@ -226,7 +226,7 @@ CollisionObject2DSW::CollisionObject2DSW(Type p_type) {
type=p_type;
space=NULL;
instance_id=0;
- user_mask=0;
+ collision_mask=1;
layer_mask=1;
pickable=true;
}
diff --git a/servers/physics_2d/collision_object_2d_sw.h b/servers/physics_2d/collision_object_2d_sw.h
index 58dc3e9cd9..f3432060b9 100644
--- a/servers/physics_2d/collision_object_2d_sw.h
+++ b/servers/physics_2d/collision_object_2d_sw.h
@@ -65,7 +65,7 @@ private:
Space2DSW *space;
Matrix32 transform;
Matrix32 inv_transform;
- uint32_t user_mask;
+ uint32_t collision_mask;
uint32_t layer_mask;
bool _static;
@@ -117,8 +117,8 @@ public:
_FORCE_INLINE_ bool is_shape_set_as_trigger(int p_idx) const { return shapes[p_idx].trigger; }
- void set_user_mask(uint32_t p_mask) {user_mask=p_mask;}
- _FORCE_INLINE_ uint32_t get_user_mask() const { return user_mask; }
+ void set_collision_mask(uint32_t p_mask) {collision_mask=p_mask;}
+ _FORCE_INLINE_ uint32_t get_collision_mask() const { return collision_mask; }
void set_layer_mask(uint32_t p_mask) {layer_mask=p_mask;}
_FORCE_INLINE_ uint32_t get_layer_mask() const { return layer_mask; }
@@ -133,6 +133,11 @@ public:
void set_pickable(bool p_pickable) { pickable=p_pickable; }
_FORCE_INLINE_ bool is_pickable() const { return pickable; }
+ _FORCE_INLINE_ bool test_collision_mask(CollisionObject2DSW* p_other) const {
+
+ return layer_mask&p_other->collision_mask || p_other->layer_mask&collision_mask;
+ }
+
virtual ~CollisionObject2DSW() {}
};
diff --git a/servers/physics_2d/physics_2d_server_sw.cpp b/servers/physics_2d/physics_2d_server_sw.cpp
index d31606acfb..08d871be69 100644
--- a/servers/physics_2d/physics_2d_server_sw.cpp
+++ b/servers/physics_2d/physics_2d_server_sw.cpp
@@ -480,6 +480,22 @@ void Physics2DServerSW::area_set_monitorable(RID p_area,bool p_monitorable) {
}
+void Physics2DServerSW::area_set_collision_mask(RID p_area,uint32_t p_mask) {
+
+ Area2DSW *area = area_owner.get(p_area);
+ ERR_FAIL_COND(!area);
+
+ area->set_collision_mask(p_mask);
+}
+
+void Physics2DServerSW::area_set_layer_mask(RID p_area,uint32_t p_mask) {
+
+ Area2DSW *area = area_owner.get(p_area);
+ ERR_FAIL_COND(!area);
+
+ area->set_layer_mask(p_mask);
+}
+
void Physics2DServerSW::area_set_monitor_callback(RID p_area,Object *p_receiver,const StringName& p_method) {
@@ -726,20 +742,20 @@ uint32_t Physics2DServerSW::body_get_layer_mask(RID p_body, uint32_t p_flags) co
};
-void Physics2DServerSW::body_set_user_mask(RID p_body, uint32_t p_flags) {
+void Physics2DServerSW::body_set_collision_mask(RID p_body, uint32_t p_flags) {
Body2DSW *body = body_owner.get(p_body);
ERR_FAIL_COND(!body);
- body->set_user_mask(p_flags);
+ body->set_collision_mask(p_flags);
};
-uint32_t Physics2DServerSW::body_get_user_mask(RID p_body, uint32_t p_flags) const {
+uint32_t Physics2DServerSW::body_get_collision_mask(RID p_body, uint32_t p_flags) const {
Body2DSW *body = body_owner.get(p_body);
ERR_FAIL_COND_V(!body,0);
- return body->get_user_mask();
+ return body->get_collision_mask();
};
void Physics2DServerSW::body_set_param(RID p_body, BodyParameter p_param, float p_value) {
diff --git a/servers/physics_2d/physics_2d_server_sw.h b/servers/physics_2d/physics_2d_server_sw.h
index 50675cbd09..341df2fdc9 100644
--- a/servers/physics_2d/physics_2d_server_sw.h
+++ b/servers/physics_2d/physics_2d_server_sw.h
@@ -134,6 +134,8 @@ public:
virtual Variant area_get_param(RID p_parea,AreaParameter p_param) const;
virtual Matrix32 area_get_transform(RID p_area) const;
virtual void area_set_monitorable(RID p_area,bool p_monitorable);
+ virtual void area_set_collision_mask(RID p_area,uint32_t p_mask);
+ virtual void area_set_layer_mask(RID p_area,uint32_t p_mask);
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);
@@ -179,8 +181,8 @@ public:
virtual void body_set_layer_mask(RID p_body, uint32_t p_mask);
virtual uint32_t body_get_layer_mask(RID p_body, uint32_t p_mask) const;
- virtual void body_set_user_mask(RID p_body, uint32_t p_mask);
- virtual uint32_t body_get_user_mask(RID p_body, uint32_t p_mask) const;
+ virtual void body_set_collision_mask(RID p_body, uint32_t p_mask);
+ virtual uint32_t body_get_collision_mask(RID p_body, uint32_t p_mask) const;
virtual void body_set_param(RID p_body, BodyParameter p_param, float p_value);
virtual float body_get_param(RID p_body, BodyParameter p_param) const;
diff --git a/servers/physics_2d/space_2d_sw.cpp b/servers/physics_2d/space_2d_sw.cpp
index 9a1b977bda..40e7b19f6f 100644
--- a/servers/physics_2d/space_2d_sw.cpp
+++ b/servers/physics_2d/space_2d_sw.cpp
@@ -596,7 +596,7 @@ bool Space2DSW::test_body_motion(Body2DSW *p_body,const Vector2&p_motion,float p
keep=false;
else if (intersection_query_results[i]->get_type()==CollisionObject2DSW::TYPE_AREA)
keep=false;
- else if ((static_cast<Body2DSW*>(intersection_query_results[i])->get_layer_mask()&p_body->get_layer_mask())==0)
+ else if ((static_cast<Body2DSW*>(intersection_query_results[i])->test_collision_mask(p_body))==0)
keep=false;
else if (static_cast<Body2DSW*>(intersection_query_results[i])->has_exception(p_body->get_self()) || p_body->has_exception(intersection_query_results[i]->get_self()))
keep=false;