-Made one way collision work with 2D physics (rigidbody)
This commit is contained in:
parent
af06843982
commit
767f71a35e
6 changed files with 975 additions and 546 deletions
BIN
demos/2d/platformer/one_way_platform.png
Normal file
BIN
demos/2d/platformer/one_way_platform.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 2.2 KiB |
220
demos/2d/platformer/one_way_platform.xml
Normal file
220
demos/2d/platformer/one_way_platform.xml
Normal file
|
@ -0,0 +1,220 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<resource_file type="PackedScene" subresource_count="3" version="1.1" version_name="Godot Engine v1.1.rc1.custom_build">
|
||||||
|
<ext_resource path="res://one_way_platform.png" type="Texture"></ext_resource>
|
||||||
|
<resource type="RectangleShape2D" path="local://1">
|
||||||
|
<real name="custom_solver_bias"> 0 </real>
|
||||||
|
<vector2 name="extents"> 100, 10 </vector2>
|
||||||
|
|
||||||
|
</resource>
|
||||||
|
<main_resource>
|
||||||
|
<dictionary name="_bundled" shared="false">
|
||||||
|
<string> "conn_count" </string>
|
||||||
|
<int> 0 </int>
|
||||||
|
<string> "conns" </string>
|
||||||
|
<int_array len="0"> </int_array>
|
||||||
|
<string> "names" </string>
|
||||||
|
<string_array len="42">
|
||||||
|
<string> "one_way_platform" </string>
|
||||||
|
<string> "StaticBody2D" </string>
|
||||||
|
<string> "_import_path" </string>
|
||||||
|
<string> "visibility/visible" </string>
|
||||||
|
<string> "visibility/opacity" </string>
|
||||||
|
<string> "visibility/self_opacity" </string>
|
||||||
|
<string> "visibility/light_mask" </string>
|
||||||
|
<string> "transform/pos" </string>
|
||||||
|
<string> "transform/rot" </string>
|
||||||
|
<string> "transform/scale" </string>
|
||||||
|
<string> "z/z" </string>
|
||||||
|
<string> "z/relative" </string>
|
||||||
|
<string> "input/pickable" </string>
|
||||||
|
<string> "shape_count" </string>
|
||||||
|
<string> "shapes/0/shape" </string>
|
||||||
|
<string> "shapes/0/transform" </string>
|
||||||
|
<string> "shapes/0/trigger" </string>
|
||||||
|
<string> "collision/layers" </string>
|
||||||
|
<string> "collision/mask" </string>
|
||||||
|
<string> "one_way_collision/direction" </string>
|
||||||
|
<string> "one_way_collision/max_depth" </string>
|
||||||
|
<string> "constant_linear_velocity" </string>
|
||||||
|
<string> "constant_angular_velocity" </string>
|
||||||
|
<string> "friction" </string>
|
||||||
|
<string> "bounce" </string>
|
||||||
|
<string> "__meta__" </string>
|
||||||
|
<string> "sprite" </string>
|
||||||
|
<string> "Sprite" </string>
|
||||||
|
<string> "texture" </string>
|
||||||
|
<string> "centered" </string>
|
||||||
|
<string> "offset" </string>
|
||||||
|
<string> "flip_h" </string>
|
||||||
|
<string> "flip_v" </string>
|
||||||
|
<string> "vframes" </string>
|
||||||
|
<string> "hframes" </string>
|
||||||
|
<string> "frame" </string>
|
||||||
|
<string> "modulate" </string>
|
||||||
|
<string> "region" </string>
|
||||||
|
<string> "region_rect" </string>
|
||||||
|
<string> "CollisionShape2D" </string>
|
||||||
|
<string> "shape" </string>
|
||||||
|
<string> "trigger" </string>
|
||||||
|
</string_array>
|
||||||
|
<string> "node_count" </string>
|
||||||
|
<int> 3 </int>
|
||||||
|
<string> "nodes" </string>
|
||||||
|
<int_array len="135"> -1, -1, 1, 0, -1, 24, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 4, 8, 5, 9, 6, 10, 7, 11, 1, 12, 8, 13, 3, 14, 9, 15, 10, 16, 8, 17, 3, 18, 3, 19, 11, 20, 12, 21, 4, 22, 5, 23, 2, 24, 5, 25, 13, 0, 0, 0, 27, 26, -1, 21, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 4, 8, 5, 9, 6, 10, 7, 11, 1, 28, 14, 29, 1, 30, 4, 31, 8, 32, 8, 33, 3, 34, 3, 35, 7, 36, 15, 37, 8, 38, 16, 0, 0, 0, 39, 39, -1, 12, 2, 0, 3, 1, 4, 2, 5, 2, 6, 3, 7, 17, 8, 5, 9, 6, 10, 7, 11, 1, 40, 9, 41, 8, 0 </int_array>
|
||||||
|
<string> "variants" </string>
|
||||||
|
<array len="18" shared="false">
|
||||||
|
<node_path> "" </node_path>
|
||||||
|
<bool> True </bool>
|
||||||
|
<real> 1 </real>
|
||||||
|
<int> 1 </int>
|
||||||
|
<vector2> 0, 0 </vector2>
|
||||||
|
<real> 0 </real>
|
||||||
|
<vector2> 1, 1 </vector2>
|
||||||
|
<int> 0 </int>
|
||||||
|
<bool> False </bool>
|
||||||
|
<resource resource_type="Shape2D" path="local://1"> </resource>
|
||||||
|
<matrix32> 1, -0, 0, 1, 1.46304, -13.1672 </matrix32>
|
||||||
|
<vector2> 0, 1 </vector2>
|
||||||
|
<real> 20 </real>
|
||||||
|
<dictionary shared="false">
|
||||||
|
<string> "__editor_plugin_screen__" </string>
|
||||||
|
<string> "2D" </string>
|
||||||
|
<string> "__editor_plugin_states__" </string>
|
||||||
|
<dictionary shared="false">
|
||||||
|
<string> "2D" </string>
|
||||||
|
<dictionary shared="false">
|
||||||
|
<string> "ofs" </string>
|
||||||
|
<vector2> -133.699, -110.553 </vector2>
|
||||||
|
<string> "snap_grid" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "snap_offset" </string>
|
||||||
|
<vector2> 0, 0 </vector2>
|
||||||
|
<string> "snap_pixel" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "snap_relative" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "snap_rotation" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "snap_rotation_offset" </string>
|
||||||
|
<real> 0 </real>
|
||||||
|
<string> "snap_rotation_step" </string>
|
||||||
|
<real> 0.261799 </real>
|
||||||
|
<string> "snap_show_grid" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "snap_step" </string>
|
||||||
|
<vector2> 10, 10 </vector2>
|
||||||
|
<string> "zoom" </string>
|
||||||
|
<real> 2.050546 </real>
|
||||||
|
</dictionary>
|
||||||
|
<string> "3D" </string>
|
||||||
|
<dictionary shared="false">
|
||||||
|
<string> "ambient_light_color" </string>
|
||||||
|
<color> 0.15, 0.15, 0.15, 1 </color>
|
||||||
|
<string> "default_light" </string>
|
||||||
|
<bool> True </bool>
|
||||||
|
<string> "default_srgb" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "deflight_rot_x" </string>
|
||||||
|
<real> 0.942478 </real>
|
||||||
|
<string> "deflight_rot_y" </string>
|
||||||
|
<real> 0.628319 </real>
|
||||||
|
<string> "fov" </string>
|
||||||
|
<real> 45 </real>
|
||||||
|
<string> "show_grid" </string>
|
||||||
|
<bool> True </bool>
|
||||||
|
<string> "show_origin" </string>
|
||||||
|
<bool> True </bool>
|
||||||
|
<string> "viewport_mode" </string>
|
||||||
|
<int> 1 </int>
|
||||||
|
<string> "viewports" </string>
|
||||||
|
<array len="4" shared="false">
|
||||||
|
<dictionary shared="false">
|
||||||
|
<string> "distance" </string>
|
||||||
|
<real> 4 </real>
|
||||||
|
<string> "listener" </string>
|
||||||
|
<bool> True </bool>
|
||||||
|
<string> "pos" </string>
|
||||||
|
<vector3> 0, 0, 0 </vector3>
|
||||||
|
<string> "use_environment" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "use_orthogonal" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "x_rot" </string>
|
||||||
|
<real> 0 </real>
|
||||||
|
<string> "y_rot" </string>
|
||||||
|
<real> 0 </real>
|
||||||
|
</dictionary>
|
||||||
|
<dictionary shared="false">
|
||||||
|
<string> "distance" </string>
|
||||||
|
<real> 4 </real>
|
||||||
|
<string> "listener" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "pos" </string>
|
||||||
|
<vector3> 0, 0, 0 </vector3>
|
||||||
|
<string> "use_environment" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "use_orthogonal" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "x_rot" </string>
|
||||||
|
<real> 0 </real>
|
||||||
|
<string> "y_rot" </string>
|
||||||
|
<real> 0 </real>
|
||||||
|
</dictionary>
|
||||||
|
<dictionary shared="false">
|
||||||
|
<string> "distance" </string>
|
||||||
|
<real> 4 </real>
|
||||||
|
<string> "listener" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "pos" </string>
|
||||||
|
<vector3> 0, 0, 0 </vector3>
|
||||||
|
<string> "use_environment" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "use_orthogonal" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "x_rot" </string>
|
||||||
|
<real> 0 </real>
|
||||||
|
<string> "y_rot" </string>
|
||||||
|
<real> 0 </real>
|
||||||
|
</dictionary>
|
||||||
|
<dictionary shared="false">
|
||||||
|
<string> "distance" </string>
|
||||||
|
<real> 4 </real>
|
||||||
|
<string> "listener" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "pos" </string>
|
||||||
|
<vector3> 0, 0, 0 </vector3>
|
||||||
|
<string> "use_environment" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "use_orthogonal" </string>
|
||||||
|
<bool> False </bool>
|
||||||
|
<string> "x_rot" </string>
|
||||||
|
<real> 0 </real>
|
||||||
|
<string> "y_rot" </string>
|
||||||
|
<real> 0 </real>
|
||||||
|
</dictionary>
|
||||||
|
</array>
|
||||||
|
<string> "zfar" </string>
|
||||||
|
<real> 500 </real>
|
||||||
|
<string> "znear" </string>
|
||||||
|
<real> 0.1 </real>
|
||||||
|
</dictionary>
|
||||||
|
</dictionary>
|
||||||
|
<string> "__editor_run_settings__" </string>
|
||||||
|
<dictionary shared="false">
|
||||||
|
<string> "custom_args" </string>
|
||||||
|
<string> "-l $scene" </string>
|
||||||
|
<string> "run_mode" </string>
|
||||||
|
<int> 0 </int>
|
||||||
|
</dictionary>
|
||||||
|
</dictionary>
|
||||||
|
<resource resource_type="Texture" path="res://one_way_platform.png"> </resource>
|
||||||
|
<color> 1, 1, 1, 1 </color>
|
||||||
|
<rect2> 0, 0, 0, 0 </rect2>
|
||||||
|
<vector2> 1.46304, -13.1672 </vector2>
|
||||||
|
</array>
|
||||||
|
<string> "version" </string>
|
||||||
|
<int> 1 </int>
|
||||||
|
</dictionary>
|
||||||
|
|
||||||
|
</main_resource>
|
||||||
|
</resource_file>
|
File diff suppressed because one or more lines are too long
|
@ -657,6 +657,7 @@ Body2DSW::Body2DSW() : CollisionObject2DSW(TYPE_BODY), active_list(this), inerti
|
||||||
area_linear_damp=0;
|
area_linear_damp=0;
|
||||||
contact_count=0;
|
contact_count=0;
|
||||||
gravity_scale=1.0;
|
gravity_scale=1.0;
|
||||||
|
using_one_way_cache=false;
|
||||||
one_way_collision_max_depth=0.1;
|
one_way_collision_max_depth=0.1;
|
||||||
|
|
||||||
still_time=0;
|
still_time=0;
|
||||||
|
|
|
@ -81,6 +81,7 @@ class Body2DSW : public CollisionObject2DSW {
|
||||||
bool active;
|
bool active;
|
||||||
bool can_sleep;
|
bool can_sleep;
|
||||||
bool first_time_kinematic;
|
bool first_time_kinematic;
|
||||||
|
bool using_one_way_cache;
|
||||||
void _update_inertia();
|
void _update_inertia();
|
||||||
virtual void _shapes_changed();
|
virtual void _shapes_changed();
|
||||||
Matrix32 new_transform;
|
Matrix32 new_transform;
|
||||||
|
@ -229,12 +230,17 @@ public:
|
||||||
_FORCE_INLINE_ void set_continuous_collision_detection_mode(Physics2DServer::CCDMode p_mode) { continuous_cd_mode=p_mode; }
|
_FORCE_INLINE_ void set_continuous_collision_detection_mode(Physics2DServer::CCDMode p_mode) { continuous_cd_mode=p_mode; }
|
||||||
_FORCE_INLINE_ Physics2DServer::CCDMode get_continuous_collision_detection_mode() const { return continuous_cd_mode; }
|
_FORCE_INLINE_ Physics2DServer::CCDMode get_continuous_collision_detection_mode() const { return continuous_cd_mode; }
|
||||||
|
|
||||||
void set_one_way_collision_direction(const Vector2& p_dir) { one_way_collision_direction=p_dir; }
|
void set_one_way_collision_direction(const Vector2& p_dir) {
|
||||||
|
one_way_collision_direction=p_dir;
|
||||||
|
using_one_way_cache=one_way_collision_direction!=Vector2();
|
||||||
|
}
|
||||||
Vector2 get_one_way_collision_direction() const { return one_way_collision_direction; }
|
Vector2 get_one_way_collision_direction() const { return one_way_collision_direction; }
|
||||||
|
|
||||||
void set_one_way_collision_max_depth(float p_depth) { one_way_collision_max_depth=p_depth; }
|
void set_one_way_collision_max_depth(float p_depth) { one_way_collision_max_depth=p_depth; }
|
||||||
float get_one_way_collision_max_depth() const { return one_way_collision_max_depth; }
|
float get_one_way_collision_max_depth() const { return one_way_collision_max_depth; }
|
||||||
|
|
||||||
|
_FORCE_INLINE_ bool is_using_one_way_collision() const { return using_one_way_cache; }
|
||||||
|
|
||||||
void set_space(Space2DSW *p_space);
|
void set_space(Space2DSW *p_space);
|
||||||
|
|
||||||
void update_inertias();
|
void update_inertias();
|
||||||
|
|
|
@ -265,6 +265,8 @@ bool BodyPair2DSW::setup(float p_step) {
|
||||||
}
|
}
|
||||||
//faster to set than to check..
|
//faster to set than to check..
|
||||||
|
|
||||||
|
bool prev_collided=collided;
|
||||||
|
|
||||||
collided = CollisionSolver2DSW::solve(shape_A_ptr,xform_A,motion_A,shape_B_ptr,xform_B,motion_B,_add_contact,this,&sep_axis);
|
collided = CollisionSolver2DSW::solve(shape_A_ptr,xform_A,motion_A,shape_B_ptr,xform_B,motion_B,_add_contact,this,&sep_axis);
|
||||||
if (!collided) {
|
if (!collided) {
|
||||||
|
|
||||||
|
@ -285,6 +287,57 @@ bool BodyPair2DSW::setup(float p_step) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!prev_collided) {
|
||||||
|
|
||||||
|
if (A->is_using_one_way_collision()) {
|
||||||
|
Vector2 direction = A->get_one_way_collision_direction();
|
||||||
|
bool valid=false;
|
||||||
|
for(int i=0;i<contact_count;i++) {
|
||||||
|
Contact& c = contacts[i];
|
||||||
|
|
||||||
|
if (c.normal.dot(direction)<0)
|
||||||
|
continue;
|
||||||
|
if (B->get_linear_velocity().dot(direction)<0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!c.reused) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
valid=true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!valid) {
|
||||||
|
collided=false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (B->is_using_one_way_collision()) {
|
||||||
|
Vector2 direction = B->get_one_way_collision_direction();
|
||||||
|
bool valid=false;
|
||||||
|
for(int i=0;i<contact_count;i++) {
|
||||||
|
|
||||||
|
Contact& c = contacts[i];
|
||||||
|
|
||||||
|
if (c.normal.dot(direction)<0)
|
||||||
|
continue;
|
||||||
|
if (A->get_linear_velocity().dot(direction)<0)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!c.reused) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
valid=true;
|
||||||
|
}
|
||||||
|
if (!valid) {
|
||||||
|
collided=false;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
real_t max_penetration = space->get_contact_max_allowed_penetration();
|
real_t max_penetration = space->get_contact_max_allowed_penetration();
|
||||||
|
|
||||||
float bias = 0.3f;
|
float bias = 0.3f;
|
||||||
|
|
Loading…
Reference in a new issue