瀏覽代碼

improved kinematic motion, improved demos for kinematic motion

Juan Linietsky 10 年之前
父節點
當前提交
802c0a5cd9
共有 4 個文件被更改,包括 24 次插入9 次删除
  1. 二進制
      2d/kinematic_char/circle.png
  2. 二進制
      2d/kinematic_char/colworld.scn
  3. 二進制
      2d/kinematic_char/long_obstacle.png
  4. 24 9
      2d/kinematic_char/player.gd

二進制
2d/kinematic_char/circle.png


二進制
2d/kinematic_char/colworld.scn


二進制
2d/kinematic_char/long_obstacle.png


+ 24 - 9
2d/kinematic_char/player.gd

@@ -21,6 +21,8 @@ const STOP_FORCE = 1300
 const JUMP_SPEED = 200
 const JUMP_MAX_AIRBORNE_TIME=0.2
 
+const SLIDE_STOP_VELOCITY=1.0 #one pixel per second
+const SLIDE_STOP_MIN_TRAVEL=1.0 #one pixel
 var velocity = Vector2()
 var on_air_time=100
 var jumping=false
@@ -86,16 +88,29 @@ func _fixed_process(delta):
 			#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 (on_air_time==0 and force.x==0 and get_travel().length() < SLIDE_STOP_MIN_TRAVEL and abs(velocity.x) < SLIDE_STOP_VELOCITY and get_collider_velocity()==Vector2()):
+			#Since this formula will always slide the character around, 
+			#a special case must be considered to to stop it from moving 
+			#if standing on an inclined floor. Conditions are:
+			# 1) Standin on floor (on_air_time==0)
+			# 2) Did not move more than one pixel (get_travel().length() < SLIDE_STOP_MIN_TRAVEL)
+			# 3) Not moving horizontally (abs(velocity.x) < SLIDE_STOP_VELOCITY)
+			# 4) Collider is not moving
+						
+			revert_motion()
+			velocity.y=0.0
+
+		else:
+			#For every other case of motion,our motion was interrupted.
+			#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