Advertisement
nezvers

RigidKinematicBody2d

Jan 18th, 2020
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. extends KinematicBody2D
  2. class_name RigidKinematicBody2D
  3.  
  4. export(Vector2) var linear_velocity = Vector2.ZERO
  5. export(Vector2) var gravity = Vector2(0, 9.8) setget set_gravity
  6. export(float) var dampening = 0.01
  7. export(float, 0.0, 1.0) var bounciness = 0.5
  8.  
  9. func set_gravity(value:Vector2)->void:
  10.     gravity = value
  11.  
  12. func set_linear_velocity(value:Vector2)->void:
  13.     linear_velocity = value
  14.  
  15. func _physics_process(delta)->void:
  16.     linear_velocity += gravity
  17.     var collision = move_and_collide(linear_velocity * delta) #apply physics
  18.     linear_velocity = linear_velocity * (1 - dampening) #reduce speed over time
  19.     if collision: #collision detected
  20.         var normal:Vector2 = collision.normal #surface normal
  21.         var strenght:float = normal.dot(linear_velocity)
  22.         linear_velocity -= normal * strenght * (1 - bounciness) #dampen velocity in floor direction
  23.         linear_velocity = (linear_velocity + collision.remainder).bounce(normal) #bounce off the surface
  24.     print(linear_velocity.x)
  25.  
  26. func apply_impulse(value:Vector2)->void:
  27.     linear_velocity += value
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement