vibe code differential movment

"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
"
This commit is contained in:
Travis Shears 2026-05-14 08:19:55 +01:00
parent 8e54b39b18
commit 9032f44830
Signed by: travisshears
GPG key ID: CB9BF1910F3F7469
4 changed files with 56 additions and 102 deletions

View file

@ -1,54 +1,54 @@
go.property("speed", 100)
go.property("rotation_speed", 3)
go.property("acceleration", 100)
go.property("deceleration", 200)
go.property("max_speed", 400)
go.property("wheel_base", 40) -- pixels between left and right wheels
local UP = vmath.vector3(0, 1, 0)
function init(self)
msg.post(".", "acquire_input_focus")
self.rotation = 0
self.rotating_left = false
self.rotating_right = false
self.moving_forward = false
msg.post(".", "acquire_input_focus")
self.left_speed = 0
self.right_speed = 0
self.left_input = false
self.right_input = false
end
function update(self, dt)
if self.rotating_left then
self.rotation = self.rotation + self.rotation_speed * dt
end
if self.rotating_right then
self.rotation = self.rotation - self.rotation_speed * dt
end
print("Rotation" .. self.rotation)
if self.moving_forward then
local vx = math.cos(self.rotation) * self.speed
local vy = math.sin(self.rotation) * self.speed
-- each wheel accelerates when its key is held, decelerates otherwise
if self.left_input then
self.left_speed = math.min(self.left_speed + self.acceleration * dt, self.max_speed)
else
self.left_speed = math.max(self.left_speed - self.deceleration * dt, 0)
end
local pos = go.get_position()
pos.x = pos.x + vx * dt
pos.y = pos.y + vy * dt
go.set_position(pos)
end
if self.right_input then
self.right_speed = math.min(self.right_speed + self.acceleration * dt, self.max_speed)
else
self.right_speed = math.max(self.right_speed - self.deceleration * dt, 0)
end
-- differential drive: speed difference creates rotation, average creates forward motion
local linear_speed = (self.left_speed + self.right_speed) / 2
local angular_vel = (self.right_speed - self.left_speed) / self.wheel_base
local rot = go.get_rotation()
rot = rot * vmath.quat_rotation_z(angular_vel * dt)
go.set_rotation(rot)
local p = go.get_position()
p = p + vmath.rotate(rot, UP * linear_speed * dt)
go.set_position(p)
-- reset each frame (on_input re-sets if key is still held)
self.left_input = false
self.right_input = false
end
function on_input(self, action_id, action)
-- Rotate with A/D keys
if action_id == hash("left_wheel_fwd") and action.pressed then
self.rotating_left = true
elseif action_id == hash("left_wheel_fwd") and action.released then
self.rotating_left = false
end
if action_id == hash("right_wheel_fwd") and action.pressed then
self.rotating_right = true
elseif action_id == hash("right_wheel_fwd") and action.released then
self.rotating_right = false
end
-- Move forward with W
if (action_id == hash("left_wheel_fwd") or action_id == hash("right_wheel_fwd")) and action.pressed then
self.moving_forward = true
elseif (action_id == hash("left_wheel_fwd") or action_id == hash("right_wheel_fwd")) and action.released then
self.moving_forward = false
end
if action_id == hash("a_key") then
self.left_input = true
elseif action_id == hash("d_key") then
self.right_input = true
end
end