Add basic gamepad support
This commit is contained in:
parent
77242a9989
commit
f143c9b591
@ -177,6 +177,32 @@ get_bvh :: proc(assetman: ^Asset_Manager, path: cstring) -> Loaded_BVH {
|
|||||||
return assetman.bvhs[path]
|
return assetman.bvhs[path]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Curve_2D :: struct {
|
||||||
|
points: [][2]f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reads a two column comma separated csv file as a curve
|
||||||
|
// get_curve_2d :: proc(assetman: ^Asset_Manager, path: string) -> (curve: Curve_2D) {
|
||||||
|
// data, err := os2.read_entire_file_from_path(path, context.allocator)
|
||||||
|
// if err != nil {
|
||||||
|
// log.errorf("Failed to read curve: %s", path)
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
// defer delete(data)
|
||||||
|
//
|
||||||
|
// str := string(data)
|
||||||
|
// scan: scanner.Scanner
|
||||||
|
// scanner.init(&scan, str, path)
|
||||||
|
// scan.whitespace = scanner.Whitespace{' ', '\t', '\r'}
|
||||||
|
// scan.flags = scanner.Scan_Flags{.Scan_Ints, .Scan_Floats, .Scan_Idents}
|
||||||
|
//
|
||||||
|
// for tok := scanner.scan(&scan); tok != scanner.EOF; tok = scanner.scan(&scan) {
|
||||||
|
//
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// return
|
||||||
|
// }
|
||||||
|
|
||||||
get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_Convex) {
|
get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_Convex) {
|
||||||
bytes, err := os2.read_entire_file(string(path), context.temp_allocator)
|
bytes, err := os2.read_entire_file(string(path), context.temp_allocator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -462,6 +488,7 @@ get_convex :: proc(assetman: ^Asset_Manager, path: cstring) -> (result: Loaded_C
|
|||||||
return {mesh = mesh, center_of_mass = center_of_mass, inertia_tensor = inertia_tensor}
|
return {mesh = mesh, center_of_mass = center_of_mass, inertia_tensor = inertia_tensor}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: move convex stuff out of assets.odin
|
||||||
Tetrahedron :: struct {
|
Tetrahedron :: struct {
|
||||||
p: [4]rl.Vector3,
|
p: [4]rl.Vector3,
|
||||||
}
|
}
|
||||||
|
@ -399,48 +399,48 @@ update_runtime_world :: proc(runtime_world: ^Runtime_World, dt: f32) {
|
|||||||
// 68% front, 32% rear
|
// 68% front, 32% rear
|
||||||
BRAKE_BIAS :: f32(0.68)
|
BRAKE_BIAS :: f32(0.68)
|
||||||
|
|
||||||
|
gas_input := rl.GetGamepadAxisMovement(0, .RIGHT_TRIGGER) * 0.5 + 0.5
|
||||||
|
brake_input := rl.GetGamepadAxisMovement(0, .LEFT_TRIGGER) * 0.5 + 0.5
|
||||||
|
if rl.IsKeyDown(.S) && !g_mem.free_cam {
|
||||||
|
brake_input = 1
|
||||||
|
}
|
||||||
|
if rl.IsKeyDown(.W) && !g_mem.free_cam {
|
||||||
|
gas_input = 1
|
||||||
|
}
|
||||||
|
|
||||||
for wheel_handle in front_wheels {
|
for wheel_handle in front_wheels {
|
||||||
wheel := physics.get_suspension_constraint(sim_state, wheel_handle)
|
wheel := physics.get_suspension_constraint(sim_state, wheel_handle)
|
||||||
|
|
||||||
wheel.brake_impulse = 0
|
wheel.brake_impulse = brake_input * BRAKE_BIAS * BRAKE_IMPULSE
|
||||||
if (rl.IsKeyDown(.S)) {
|
|
||||||
wheel.brake_impulse = BRAKE_BIAS * BRAKE_IMPULSE
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for wheel_handle in back_wheels {
|
for wheel_handle in back_wheels {
|
||||||
wheel := physics.get_suspension_constraint(sim_state, wheel_handle)
|
wheel := physics.get_suspension_constraint(sim_state, wheel_handle)
|
||||||
|
|
||||||
wheel.brake_impulse = 0
|
wheel.brake_impulse = brake_input * (1.0 - BRAKE_BIAS) * BRAKE_IMPULSE
|
||||||
if (rl.IsKeyDown(.S)) {
|
|
||||||
wheel.brake_impulse = (1.0 - BRAKE_BIAS) * BRAKE_IMPULSE
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for wheel_handle in drive_wheels {
|
for wheel_handle in drive_wheels {
|
||||||
wheel := physics.get_suspension_constraint(sim_state, wheel_handle)
|
wheel := physics.get_suspension_constraint(sim_state, wheel_handle)
|
||||||
|
|
||||||
wheel.drive_impulse = 0
|
wheel.drive_impulse = gas_input * DRIVE_IMPULSE
|
||||||
|
|
||||||
if rl.IsKeyDown(.W) && !g_mem.free_cam {
|
|
||||||
wheel.drive_impulse = DRIVE_IMPULSE
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
car_body := physics.get_body(sim_state, runtime_world.car_handle)
|
car_body := physics.get_body(sim_state, runtime_world.car_handle)
|
||||||
turn_vel_correction := clamp(30.0 / linalg.length(car_body.v), 0, 1)
|
turn_vel_correction := clamp(30.0 / linalg.length(car_body.v), 0, 1)
|
||||||
|
|
||||||
|
turn_input := rl.GetGamepadAxisMovement(0, .LEFT_X)
|
||||||
|
if rl.IsKeyDown(.A) && !g_mem.free_cam {
|
||||||
|
turn_input = -1
|
||||||
|
}
|
||||||
|
if rl.IsKeyDown(.D) && !g_mem.free_cam {
|
||||||
|
turn_input = 1
|
||||||
|
}
|
||||||
|
|
||||||
for wheel_handle in turn_wheels {
|
for wheel_handle in turn_wheels {
|
||||||
wheel := physics.get_suspension_constraint(sim_state, wheel_handle)
|
wheel := physics.get_suspension_constraint(sim_state, wheel_handle)
|
||||||
wheel.turn_angle = 0
|
|
||||||
|
|
||||||
if rl.IsKeyDown(.A) && !g_mem.free_cam {
|
wheel.turn_angle = TURN_ANGLE * turn_vel_correction * turn_input
|
||||||
wheel.turn_angle += -TURN_ANGLE * turn_vel_correction
|
|
||||||
}
|
|
||||||
|
|
||||||
if rl.IsKeyDown(.D) && !g_mem.free_cam {
|
|
||||||
wheel.turn_angle += TURN_ANGLE * turn_vel_correction
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
runtime_world.dt = dt
|
runtime_world.dt = dt
|
||||||
@ -457,18 +457,43 @@ Orbit_Camera :: struct {
|
|||||||
distance: f32,
|
distance: f32,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GAMEPAD_DEADZONE :: f32(0.07)
|
||||||
|
|
||||||
orbit_camera_update :: proc(camera: ^Orbit_Camera) {
|
orbit_camera_update :: proc(camera: ^Orbit_Camera) {
|
||||||
camera.target =
|
camera.target =
|
||||||
physics.get_body(physics.get_sim_state(&get_runtime_world().world.physics_scene), get_runtime_world().car_handle).x
|
physics.get_body(physics.get_sim_state(&get_runtime_world().world.physics_scene), get_runtime_world().car_handle).x
|
||||||
|
|
||||||
|
gamepad_delta := rl.Vector2 {
|
||||||
|
rl.GetGamepadAxisMovement(0, .RIGHT_X),
|
||||||
|
rl.GetGamepadAxisMovement(0, .RIGHT_Y),
|
||||||
|
}
|
||||||
|
if abs(gamepad_delta.x) < GAMEPAD_DEADZONE {
|
||||||
|
gamepad_delta.x = 0
|
||||||
|
}
|
||||||
|
if abs(gamepad_delta.y) < GAMEPAD_DEADZONE {
|
||||||
|
gamepad_delta.y = 0
|
||||||
|
}
|
||||||
|
|
||||||
mouse_delta := rl.GetMouseDelta()
|
mouse_delta := rl.GetMouseDelta()
|
||||||
|
|
||||||
SENSE :: 0.01
|
MOUSE_SENSE :: 0.01
|
||||||
|
GAMEPAD_SENSE :: 1
|
||||||
|
|
||||||
|
final_sense: f32
|
||||||
|
delta: rl.Vector2
|
||||||
|
|
||||||
|
if linalg.length2(mouse_delta) > linalg.length2(gamepad_delta) {
|
||||||
|
final_sense = MOUSE_SENSE
|
||||||
|
delta = mouse_delta
|
||||||
|
} else {
|
||||||
|
final_sense = GAMEPAD_SENSE * rl.GetFrameTime()
|
||||||
|
delta = gamepad_delta
|
||||||
|
}
|
||||||
|
|
||||||
rl.HideCursor()
|
rl.HideCursor()
|
||||||
|
|
||||||
camera.yaw += mouse_delta.x * SENSE
|
camera.yaw += delta.x * final_sense
|
||||||
camera.pitch += mouse_delta.y * SENSE
|
camera.pitch += delta.y * final_sense
|
||||||
camera.pitch = math.clamp(camera.pitch, -math.PI / 2.0 + 0.0001, math.PI / 2.0 - 0.0001)
|
camera.pitch = math.clamp(camera.pitch, -math.PI / 2.0 + 0.0001, math.PI / 2.0 - 0.0001)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user