Hey Josh, this is really great stuff. I started messing around with it this evening and drummed up the following VR controller. It's not complete, but maybe it'll help others get started. I'm stumped on how to change the HMD's base rotation to match a parent entity's though (very end of the code). I was trying to implement snap-turning. Also, just as a heads up, I'm testing with a Fove and it works great. I haven't tried it on my Rift yet.
Script.TrackingSpace = 0 --choice "Tracking Type" "Seated, Roomscale"
Script.CameraHeight = 1.75 --float "Camera Height"
-- Maybe changing the FOV will stop items popping in and out at the edges
Script.PlayerFOV = 90.0 --float "Player FOV"
Script.MovementSpeed = 2.0 --float "Movement Speed"
Script.SnapTurn = true --bool "Use Snap Turning"
-- This is used when SnapTurn is true.
Script.TurnAngle = 30 --int "Snap Turn Angle"
-- This is used when SnapTurn is false.
Script.TurnSpeed = 1.0 --float "Smooth Turn Speed"
Script.Camera = nil
local vr_enabled = true
local actions = {}
function Script:InitializeActions()
---------------------------------------
-- Set this to false if you use QWERTY
---------------------------------------
local dvorak = true
if dvorak then
actions.move_forward = Key.Comma
actions.move_back = Key.O
actions.strafe_left = Key.A
actions.strafe_right = Key.E
actions.turn_left = Key.Quotes
actions.turn_right = Key.Period
else -- Assume QWERTY
actions.move_forward = Key.W
actions.move_back = Key.S
actions.strafe_left = Key.A
actions.strafe_right = Key.D
actions.turn_left = Key.Q
actions.turn_right = Key.E
end
actions.center_view = Key.Space
end
function Script:Start()
self.InitializeActions()
-- Add basic character properties
self.entity:SetMass(1)
self.entity:SetCollisionType(Collision.Character)
self.entity:SetPhysicsMode(Entity.CharacterPhysics)
-- Create the VR camera
self.Camera = Camera:Create()
self.Camera:SetParent(self.entity)
self.Camera:SetFOV(self.PlayerFOV)
self.Camera:SetPosition(Vec3(0, self.CameraHeight, 0))
-- Enable VR
if vr_enabled then
VR:Enable()
VR:SetTrackingSpace(self.TrackingSpace)
end
end
function Script:UpdateWorld()
self:CharacterMovement()
end
function Script:CharacterMovement()
local forward = 0
local strafe = 0
local angle = 0
if window:KeyDown(actions.move_forward) then forward = forward + 1 end
if window:KeyDown(actions.move_back) then forward = forward - 1 end
if window:KeyDown(actions.strafe_left) then strafe = strafe - 1 end
if window:KeyDown(actions.strafe_right) then strafe = strafe + 1 end
if self.SnapTurn then
-- Use KeyHit() instead of KeyDown() to avoid continuous turning.
if window:KeyHit(actions.turn_left) then angle = angle - self.TurnAngle end
if window:KeyHit(actions.turn_right) then angle = angle + self.TurnAngle end
else
if window:KeyDown(actions.turn_left) then angle = angle - self.TurnSpeed end
if window:KeyDown(actions.turn_right) then angle = angle + self.TurnSpeed end
end
if window:KeyDown(actions.center_view) and vr_enabled then VR:CenterTracking() end
forward = forward * self.MovementSpeed
strafe = strafe * self.MovementSpeed
local turn_angle = self.entity:GetRotation(true).y + angle
self.entity:SetInput(turn_angle, forward, strafe)
if vr_enabled then
-- Set the HMD position to correspond to the parent entity.
VR:SetOffset(self.entity:GetPosition(true) + Vec3(0.0, self.CameraHeight, 0.0))
-- TODO: How do I adjust the HMD's base rotation? This doesn't seem to work.
if angle ~= 0 then
VR:CenterTracking()
end
end
end