summaryrefslogtreecommitdiff
path: root/servers/physics_2d
diff options
context:
space:
mode:
authorJuan Linietsky <reduzio@gmail.com>2015-04-18 14:44:59 -0300
committerJuan Linietsky <reduzio@gmail.com>2015-04-18 14:44:59 -0300
commitf92a1fd6460ac7ad8c37737edd03a56c584a0ad3 (patch)
treebdb4ca42e9b93bdba3155faacba659080cd104b6 /servers/physics_2d
parentfdaa2920eb21fff3320a17e9239e04dfadecdb00 (diff)
parente1bfbcfd4230b3224d5700914f6457ea869214f0 (diff)
Merge pull request #1691 from Faless/area_combine_2d
Implement Area2D space_override_mode = COMBINE
Diffstat (limited to 'servers/physics_2d')
-rw-r--r--servers/physics_2d/body_2d_sw.cpp37
-rw-r--r--servers/physics_2d/body_2d_sw.h7
2 files changed, 26 insertions, 18 deletions
diff --git a/servers/physics_2d/body_2d_sw.cpp b/servers/physics_2d/body_2d_sw.cpp
index fe45a3e640..98dabb4d3c 100644
--- a/servers/physics_2d/body_2d_sw.cpp
+++ b/servers/physics_2d/body_2d_sw.cpp
@@ -379,13 +379,12 @@ void Body2DSW::_compute_area_gravity(const Area2DSW *p_area) {
if (p_area->is_gravity_point()) {
- gravity = (p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin()).normalized() * p_area->get_gravity();
+ gravity += (p_area->get_transform().xform(p_area->get_gravity_vector()) - get_transform().get_origin()).normalized() * p_area->get_gravity();
} else {
- gravity = p_area->get_gravity_vector() * p_area->get_gravity();
+ gravity += p_area->get_gravity_vector() * p_area->get_gravity();
}
- gravity*=gravity_scale;
}
void Body2DSW::integrate_forces(real_t p_step) {
@@ -393,32 +392,39 @@ void Body2DSW::integrate_forces(real_t p_step) {
if (mode==Physics2DServer::BODY_MODE_STATIC)
return;
- Area2DSW *current_area = get_space()->get_default_area();
- ERR_FAIL_COND(!current_area);
+ Area2DSW *def_area = get_space()->get_default_area();
+ Area2DSW *damp_area = def_area;
+ ERR_FAIL_COND(!def_area);
- int prio = current_area->get_priority();
int ac = areas.size();
+ bool replace = false;
+ gravity=Vector2(0,0);
if (ac) {
+ areas.sort();
const AreaCMP *aa = &areas[0];
- for(int i=0;i<ac;i++) {
- if (aa[i].area->get_priority() > prio) {
- current_area=aa[i].area;
- prio=current_area->get_priority();
+ damp_area = aa[ac-1].area;
+ for(int i=ac-1;i>=0;i--) {
+ _compute_area_gravity(aa[i].area);
+ if (aa[i].area->get_space_override_mode() == Physics2DServer::AREA_SPACE_OVERRIDE_REPLACE) {
+ replace = true;
+ break;
}
}
}
-
- _compute_area_gravity(current_area);
+ if( !replace ) {
+ _compute_area_gravity(def_area);
+ }
+ gravity*=gravity_scale;
if (angular_damp>=0)
area_angular_damp=angular_damp;
else
- area_angular_damp=current_area->get_angular_damp();
+ area_angular_damp=damp_area->get_angular_damp();
if (linear_damp>=0)
area_linear_damp=linear_damp;
else
- area_linear_damp=current_area->get_linear_damp();
+ area_linear_damp=damp_area->get_linear_damp();
Vector2 motion;
bool do_motion=false;
@@ -482,7 +488,8 @@ void Body2DSW::integrate_forces(real_t p_step) {
_update_shapes_with_motion(motion);
}
- current_area=NULL; // clear the area, so it is set in the next frame
+ damp_area=NULL; // clear the area, so it is set in the next frame
+ def_area=NULL; // clear the area, so it is set in the next frame
contact_count=0;
}
diff --git a/servers/physics_2d/body_2d_sw.h b/servers/physics_2d/body_2d_sw.h
index a6fc6fc97f..3b07401dd7 100644
--- a/servers/physics_2d/body_2d_sw.h
+++ b/servers/physics_2d/body_2d_sw.h
@@ -91,13 +91,14 @@ class Body2DSW : public CollisionObject2DSW {
struct AreaCMP {
Area2DSW *area;
- _FORCE_INLINE_ bool operator<(const AreaCMP& p_cmp) const { return area->get_self() < p_cmp.area->get_self() ; }
+ _FORCE_INLINE_ bool operator==(const AreaCMP& p_cmp) const { return area->get_self() == p_cmp.area->get_self();}
+ _FORCE_INLINE_ bool operator<(const AreaCMP& p_cmp) const { return area->get_priority() < p_cmp.area->get_priority();}
_FORCE_INLINE_ AreaCMP() {}
_FORCE_INLINE_ AreaCMP(Area2DSW *p_area) { area=p_area;}
};
- VSet<AreaCMP> areas;
+ Vector<AreaCMP> areas;
struct Contact {
@@ -140,7 +141,7 @@ public:
void set_force_integration_callback(ObjectID p_id, const StringName& p_method, const Variant &p_udata=Variant());
- _FORCE_INLINE_ void add_area(Area2DSW *p_area) { areas.insert(AreaCMP(p_area)); }
+ _FORCE_INLINE_ void add_area(Area2DSW *p_area) { areas.ordered_insert(AreaCMP(p_area)); }
_FORCE_INLINE_ void remove_area(Area2DSW *p_area) { areas.erase(AreaCMP(p_area)); }
_FORCE_INLINE_ void set_max_contacts_reported(int p_size) { contacts.resize(p_size); contact_count=0; if (mode==Physics2DServer::BODY_MODE_KINEMATIC && p_size) set_active(true);}