From 7ca29bfaa7a23d06374c2456e0360c911bd9aa3e Mon Sep 17 00:00:00 2001 From: Juan Linietsky Date: Sat, 22 Feb 2014 20:28:19 -0300 Subject: -added kinematic body -added kinematic body demos --- demos/2d/kinematic_char/colworld.gd | 17 +++++ demos/2d/kinematic_char/colworld.scn | Bin 0 -> 5065 bytes demos/2d/kinematic_char/engine.cfg | 13 ++++ demos/2d/kinematic_char/icon.png | Bin 0 -> 1513 bytes demos/2d/kinematic_char/obstacle.png | Bin 0 -> 490 bytes demos/2d/kinematic_char/player.gd | 116 +++++++++++++++++++++++++++++++++++ demos/2d/kinematic_char/player.png | Bin 0 -> 502 bytes demos/2d/kinematic_char/player.scn | Bin 0 -> 1511 bytes demos/2d/kinematic_char/princess.png | Bin 0 -> 504 bytes demos/2d/kinematic_col/colworld.scn | Bin 0 -> 2941 bytes demos/2d/kinematic_col/engine.cfg | 12 ++++ demos/2d/kinematic_col/icon.png | Bin 0 -> 1426 bytes demos/2d/kinematic_col/obstacle.png | Bin 0 -> 453 bytes demos/2d/kinematic_col/player.gd | 36 +++++++++++ demos/2d/kinematic_col/player.png | Bin 0 -> 502 bytes demos/2d/kinematic_col/player.scn | Bin 0 -> 1495 bytes 16 files changed, 194 insertions(+) create mode 100644 demos/2d/kinematic_char/colworld.gd create mode 100644 demos/2d/kinematic_char/colworld.scn create mode 100644 demos/2d/kinematic_char/engine.cfg create mode 100644 demos/2d/kinematic_char/icon.png create mode 100644 demos/2d/kinematic_char/obstacle.png create mode 100644 demos/2d/kinematic_char/player.gd create mode 100644 demos/2d/kinematic_char/player.png create mode 100644 demos/2d/kinematic_char/player.scn create mode 100644 demos/2d/kinematic_char/princess.png create mode 100644 demos/2d/kinematic_col/colworld.scn create mode 100644 demos/2d/kinematic_col/engine.cfg create mode 100644 demos/2d/kinematic_col/icon.png create mode 100644 demos/2d/kinematic_col/obstacle.png create mode 100644 demos/2d/kinematic_col/player.gd create mode 100644 demos/2d/kinematic_col/player.png create mode 100644 demos/2d/kinematic_col/player.scn (limited to 'demos') diff --git a/demos/2d/kinematic_char/colworld.gd b/demos/2d/kinematic_char/colworld.gd new file mode 100644 index 0000000000..efd1dab805 --- /dev/null +++ b/demos/2d/kinematic_char/colworld.gd @@ -0,0 +1,17 @@ + +extends Node2D + +# member variables here, example: +# var a=2 +# var b="textvar" + +func _ready(): + # Initalization here + pass + + + + +func _on_princess_body_enter( body ): + #the name of this editor-generated callback is unfortunate + get_node("youwin").show() diff --git a/demos/2d/kinematic_char/colworld.scn b/demos/2d/kinematic_char/colworld.scn new file mode 100644 index 0000000000..b1285b4811 Binary files /dev/null and b/demos/2d/kinematic_char/colworld.scn differ diff --git a/demos/2d/kinematic_char/engine.cfg b/demos/2d/kinematic_char/engine.cfg new file mode 100644 index 0000000000..0132442c18 --- /dev/null +++ b/demos/2d/kinematic_char/engine.cfg @@ -0,0 +1,13 @@ +[application] + +name="Kinematic Collision" +main_scene="res://colworld.scn" +icon="res://icon.png" + +[input] + +move_up=[key(Up)] +move_left=[key(Left)] +move_right=[key(Right)] +move_bottom=[key(Down)] +jump=[key(Space)] diff --git a/demos/2d/kinematic_char/icon.png b/demos/2d/kinematic_char/icon.png new file mode 100644 index 0000000000..bdca104c1f Binary files /dev/null and b/demos/2d/kinematic_char/icon.png differ diff --git a/demos/2d/kinematic_char/obstacle.png b/demos/2d/kinematic_char/obstacle.png new file mode 100644 index 0000000000..3ade3c3a52 Binary files /dev/null and b/demos/2d/kinematic_char/obstacle.png differ diff --git a/demos/2d/kinematic_char/player.gd b/demos/2d/kinematic_char/player.gd new file mode 100644 index 0000000000..b35bbfa693 --- /dev/null +++ b/demos/2d/kinematic_char/player.gd @@ -0,0 +1,116 @@ + +extends KinematicBody2D + +# This is a simple collision demo showing how +# the kinematic cotroller works. +# move() will allow to move the node, and will +# always move it to a non-colliding spot, +# as long as it starts from a non-colliding spot too. + + +#pixels / second +const GRAVITY = 500.0 + +# Angle in degrees towards either side that the player can +# consider "floor". +const FLOOR_ANGLE_TOLERANCE = 40 +const WALK_FORCE = 600 +const WALK_MAX_SPEED = 200 +const STOP_FORCE = 1300 +const JUMP_SPEED = 200 +const JUMP_MAX_AIRBORNE_TIME=0.2 + +var velocity = Vector2() +var on_air_time=100 +var jumping=false + +var prev_jump_pressed=false + +func _fixed_process(delta): + + #create forces + var force = Vector2(0,GRAVITY) + + var stop = velocity.x!=0.0 + + var walk_left = Input.is_action_pressed("move_left") + var walk_right = Input.is_action_pressed("move_right") + var jump = Input.is_action_pressed("jump") + + var stop=true + + if (walk_left): + if (velocity.x<=0 and velocity.x > -WALK_MAX_SPEED): + force.x-=WALK_FORCE + stop=false + + elif (walk_right): + if (velocity.x>=0 and velocity.x < WALK_MAX_SPEED): + force.x+=WALK_FORCE + stop=false + + if (stop): + var vsign = sign(velocity.x) + var vlen = abs(velocity.x) + + vlen -= STOP_FORCE * delta + if (vlen<0): + vlen=0 + + velocity.x=vlen*vsign + + + + #integrate forces to velocity + velocity += force * delta + + #integrate velocity into motion and move + var motion = velocity * delta + + #move and consume motion +# + motion = move(motion) + + + var floor_velocity=Vector2() + + if (is_colliding()): + #ran against something, is it the floor? get normal + var n = get_collision_normal() + + if ( rad2deg(acos(n.dot( Vector2(0,-1)))) < FLOOR_ANGLE_TOLERANCE ): + #if angle to the "up" vectors is < angle tolerance + #char is on floor + on_air_time=0 + floor_velocity=get_collider_velocity() + #velocity.y=0 + + # But we were moving and our motion was interrupted, + # so try to complete the motion by "sliding" + # by the normal + motion = n.slide(motion) + velocity = n.slide(velocity) + + #then move again + move(motion) + + if (floor_velocity!=Vector2()): + #if floor moves, move with floor + move(floor_velocity*delta) + + if (jumping and velocity.y>0): + jumping=false + + if (on_air_time