Skip to content

Instantly share code, notes, and snippets.

@blah238
Created May 25, 2020 03:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blah238/69792aa4df686be0221a2e6a8a96ca08 to your computer and use it in GitHub Desktop.
Save blah238/69792aa4df686be0221a2e6a8a96ca08 to your computer and use it in GitHub Desktop.
Assetto Corsa CSP - Custom XY Chaser Camera
--------
-- Simple camera script with X and Y-axis rotation.
--------
-- Extra thing for looking around:
local lookXDirection = smoothing(0, 10)
local lookYDirection = smoothing(0, 10)
-- Will be called each frame:
-- Note: `dt` is time passed since last frame, `cameraIndex` is 1 or 2, depending on which camera is
-- chosen.
function update(dt, cameraIndex)
smoothing.setDT(dt)
-- Get AC camera parameters with some corrections to be somewhat compatible:
local cameraParameters = ac.getCameraParameters(cameraIndex)
local distance = cameraParameters.distance + 0.8
local height = cameraParameters.height
local pitchAngle = -cameraParameters.pitch
-- Get car position and vectors:
local carPos = ac.getCarPosition()
local carDir = ac.getCarDirection()
local carUp = ac.getCarUp()
local carRight = math.cross(carDir, carUp):normalize()
-- Extra thing for joystick support:
local joystickLook = ac.getJoystickLook()
lookXDirection:update(
(ac.looksLeft() and ac.looksRight() or ac.looksBehind()) and math.sign(lookXDirection.val) or
ac.looksLeft() and 0.5 or
ac.looksRight() and -0.5 or
joystickLook ~= nil and -joystickLook.x or 0)
local cameraXAngle = lookXDirection.val * math.pi
lookYDirection:update(
(
joystickLook ~= nil and -joystickLook.y or 0
)
)
-- cameraYAngle = cameraYAngle + lookYDirection.val * math.pi
local cameraYAngle = lookYDirection.val * math.pi
-- Sine and cosine for camera angle
local sinX, cosX = math.sin(cameraXAngle), math.cos(cameraXAngle)
-- Up direction for camera (could be used for horizon lock):
local cameraUp = (carUp + vec3(0, 3, 0)):normalize()
-- Set camera position:
ac.Camera.position = carPos
+ (carRight * sinX - carDir * cosX) * distance
+ (carUp * math.clamp(-cameraYAngle, -math.radians(90), math.pi))
+ vec3(0, height, 0)
-- Find camera look
local cameraLookPosOffset = carDir + carUp * (1 - math.abs(lookXDirection.val ))
local cameraLook = (carPos + cameraLookPosOffset - ac.Camera.position):normalize()
-- Use for `pitchAngle`:
cameraLook:rotate(quat.fromAngleAxis(math.radians(pitchAngle), carRight))
-- Set camera look:
ac.Camera.direction = cameraLook
-- Set other parameters:
ac.Camera.up = (carUp + vec3(0, 3, 0)):normalize()
ac.Camera.fov = 60
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment