Skip to content

Instantly share code, notes, and snippets.

@Kaiochao
Created September 9, 2015 09:02
Show Gist options
  • Save Kaiochao/9979fbaa79d785860c65 to your computer and use it in GitHub Desktop.
Save Kaiochao/9979fbaa79d785860c65 to your computer and use it in GitHub Desktop.
entity/topdown_ship
var
max_speed
acceleration
drag
max_turn_rate
angle = 0
tmp
velocity_x = 0
velocity_y = 0
Update()
var input_acceleration = client.IsKeyDown("w")
var input_turn = client.GetAxis("d", "a")
var new_velocity_x = velocity_x
var new_velocity_y = velocity_y
var new_angle = angle
if(input_acceleration)
new_velocity_x += acceleration * sin(angle)
new_velocity_y += acceleration * cos(angle)
// enforce speed limit
var new_speed = hypot(new_velocity_x, new_velocity_y)
if(new_speed > max_speed)
var s = max_speed / new_speed
new_velocity_x *= s
new_velocity_y *= s
if(input_turn)
new_angle += input_turn * max_turn_rate
// velocity changed
if(velocity_x != new_velocity_x || velocity_y != new_velocity_y)
velocity_x = new_velocity_x
velocity_y = new_velocity_y
// angle changed; rotate transform
if(angle != new_angle)
transform = turn(transform, new_angle - angle)
angle = new_angle
// velocity dead-zone: no movement at small velocities
if(abs(velocity_x) > 0.25 || abs(velocity_y) > 0.25)
Translate(velocity_x, velocity_y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment