diff options
author | yakun.zhang <Raphael10241024@gmail.com> | 2019-06-21 19:33:01 +0800 |
---|---|---|
committer | yakun.zhang <Raphael10241024@gmail.com> | 2019-06-21 19:33:01 +0800 |
commit | 434fd71d2a4b69f576362c3a9baa5b39db009d31 (patch) | |
tree | ee6b28619ac16adcf899aa0d631926b8507f4dad /servers/physics_2d | |
parent | 1e833cadbcf1403eec85fa85b290328bc1bf5179 (diff) |
add 2d physic optimization simlilar to 3d physics
Diffstat (limited to 'servers/physics_2d')
-rw-r--r-- | servers/physics_2d/collision_object_2d_sw.cpp | 45 | ||||
-rw-r--r-- | servers/physics_2d/collision_object_2d_sw.h | 2 | ||||
-rw-r--r-- | servers/physics_2d/physics_2d_server_sw.cpp | 20 | ||||
-rw-r--r-- | servers/physics_2d/physics_2d_server_sw.h | 3 |
4 files changed, 59 insertions, 11 deletions
diff --git a/servers/physics_2d/collision_object_2d_sw.cpp b/servers/physics_2d/collision_object_2d_sw.cpp index 445a2e0613..f556638db1 100644 --- a/servers/physics_2d/collision_object_2d_sw.cpp +++ b/servers/physics_2d/collision_object_2d_sw.cpp @@ -29,6 +29,7 @@ /*************************************************************************/ #include "collision_object_2d_sw.h" +#include "servers/physics_2d/physics_2d_server_sw.h" #include "space_2d_sw.h" void CollisionObject2DSW::add_shape(Shape2DSW *p_shape, const Transform2D &p_transform, bool p_disabled) { @@ -43,8 +44,12 @@ void CollisionObject2DSW::add_shape(Shape2DSW *p_shape, const Transform2D &p_tra s.one_way_collision_margin = 0; shapes.push_back(s); p_shape->add_owner(this); - _update_shapes(); - _shapes_changed(); + + if (!pending_shape_update_list.in_list()) { + Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list); + } + // _update_shapes(); + // _shapes_changed(); } void CollisionObject2DSW::set_shape(int p_index, Shape2DSW *p_shape) { @@ -54,8 +59,12 @@ void CollisionObject2DSW::set_shape(int p_index, Shape2DSW *p_shape) { shapes.write[p_index].shape = p_shape; p_shape->add_owner(this); - _update_shapes(); - _shapes_changed(); + + if (!pending_shape_update_list.in_list()) { + Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list); + } + // _update_shapes(); + // _shapes_changed(); } void CollisionObject2DSW::set_shape_metadata(int p_index, const Variant &p_metadata) { @@ -70,8 +79,12 @@ void CollisionObject2DSW::set_shape_transform(int p_index, const Transform2D &p_ shapes.write[p_index].xform = p_transform; shapes.write[p_index].xform_inv = p_transform.affine_inverse(); - _update_shapes(); - _shapes_changed(); + + if (!pending_shape_update_list.in_list()) { + Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list); + } + // _update_shapes(); + // _shapes_changed(); } void CollisionObject2DSW::set_shape_as_disabled(int p_idx, bool p_disabled) { @@ -89,9 +102,15 @@ void CollisionObject2DSW::set_shape_as_disabled(int p_idx, bool p_disabled) { if (p_disabled && shape.bpid != 0) { space->get_broadphase()->remove(shape.bpid); shape.bpid = 0; - _update_shapes(); + if (!pending_shape_update_list.in_list()) { + Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list); + } + //_update_shapes(); } else if (!p_disabled && shape.bpid == 0) { - _update_shapes(); // automatically adds shape with bpid == 0 + if (!pending_shape_update_list.in_list()) { + Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list); + } + //_update_shapes(); // automatically adds shape with bpid == 0 } } @@ -122,8 +141,11 @@ void CollisionObject2DSW::remove_shape(int p_index) { shapes[p_index].shape->remove_owner(this); shapes.remove(p_index); - _update_shapes(); - _shapes_changed(); + if (!pending_shape_update_list.in_list()) { + Physics2DServerSW::singletonsw->pending_shape_update_list.add(&pending_shape_update_list); + } + // _update_shapes(); + // _shapes_changed(); } void CollisionObject2DSW::_set_static(bool p_static) { @@ -239,7 +261,8 @@ void CollisionObject2DSW::_shape_changed() { _shapes_changed(); } -CollisionObject2DSW::CollisionObject2DSW(Type p_type) { +CollisionObject2DSW::CollisionObject2DSW(Type p_type) : + pending_shape_update_list(this) { _static = true; type = p_type; diff --git a/servers/physics_2d/collision_object_2d_sw.h b/servers/physics_2d/collision_object_2d_sw.h index fa18e61262..ed59469878 100644 --- a/servers/physics_2d/collision_object_2d_sw.h +++ b/servers/physics_2d/collision_object_2d_sw.h @@ -78,6 +78,8 @@ private: uint32_t collision_layer; bool _static; + SelfList<CollisionObject2DSW> pending_shape_update_list; + void _update_shapes(); protected: diff --git a/servers/physics_2d/physics_2d_server_sw.cpp b/servers/physics_2d/physics_2d_server_sw.cpp index 0efa15d43e..cc656d3b73 100644 --- a/servers/physics_2d/physics_2d_server_sw.cpp +++ b/servers/physics_2d/physics_2d_server_sw.cpp @@ -902,6 +902,8 @@ void Physics2DServerSW::body_apply_torque_impulse(RID p_body, real_t p_torque) { Body2DSW *body = body_owner.get(p_body); ERR_FAIL_COND(!body); + _update_shapes(); + body->apply_torque_impulse(p_torque); } @@ -910,6 +912,8 @@ void Physics2DServerSW::body_apply_impulse(RID p_body, const Vector2 &p_pos, con Body2DSW *body = body_owner.get(p_body); ERR_FAIL_COND(!body); + _update_shapes(); + body->apply_impulse(p_pos, p_impulse); body->wakeup(); }; @@ -944,6 +948,8 @@ void Physics2DServerSW::body_set_axis_velocity(RID p_body, const Vector2 &p_axis Body2DSW *body = body_owner.get(p_body); ERR_FAIL_COND(!body); + _update_shapes(); + Vector2 v = body->get_linear_velocity(); Vector2 axis = p_axis_velocity.normalized(); v -= axis * axis.dot(v); @@ -1052,6 +1058,8 @@ bool Physics2DServerSW::body_test_motion(RID p_body, const Transform2D &p_from, ERR_FAIL_COND_V(!body->get_space(), false); ERR_FAIL_COND_V(body->get_space()->is_locked(), false); + _update_shapes(); + return body->get_space()->test_body_motion(body, p_from, p_motion, p_infinite_inertia, p_margin, r_result, p_exclude_raycast_shapes); } @@ -1238,6 +1246,8 @@ Physics2DServer::JointType Physics2DServerSW::joint_get_type(RID p_joint) const void Physics2DServerSW::free(RID p_rid) { + _update_shapes(); // just in case + if (shape_owner.owns(p_rid)) { Shape2DSW *shape = shape_owner.get(p_rid); @@ -1335,6 +1345,8 @@ void Physics2DServerSW::step(real_t p_step) { if (!active) return; + _update_shapes(); + doing_sync = false; last_step = p_step; @@ -1418,6 +1430,14 @@ void Physics2DServerSW::finish() { memdelete(direct_state); }; +void Physics2DServerSW::_update_shapes() { + + while (pending_shape_update_list.first()) { + pending_shape_update_list.first()->self()->_shape_changed(); + pending_shape_update_list.remove(pending_shape_update_list.first()); + } +} + int Physics2DServerSW::get_process_info(ProcessInfo p_info) { switch (p_info) { diff --git a/servers/physics_2d/physics_2d_server_sw.h b/servers/physics_2d/physics_2d_server_sw.h index adc011af40..72625c397c 100644 --- a/servers/physics_2d/physics_2d_server_sw.h +++ b/servers/physics_2d/physics_2d_server_sw.h @@ -70,6 +70,9 @@ class Physics2DServerSW : public Physics2DServer { static Physics2DServerSW *singletonsw; //void _clear_query(Query2DSW *p_query); + friend class CollisionObject2DSW; + SelfList<CollisionObject2DSW>::List pending_shape_update_list; + void _update_shapes(); RID _shape_create(ShapeType p_shape); |