Hey guys any help with this would be most appreciated.
I’m trying to make a charter that moves underwater with rotation and movement handled by the accelerometer. Now I have it working but it is a bit rough and I was wondering if anyone could take a glance at my code and offer any suggestions as to improvements.
I think the rotation needs to happen slightly before the movement and maybe the rotation needs to be slightly less precise to the degree. At the moment my character looks a bit like he’s had five cups of coffee and stayed up all night, i.e., very jumpy.
I’m using Matthew Pringles accelerometer remote library. So that is the below code
[lua]remote.xGravity
remote.yGravity[/lua]
Here is the full movement function. This runs on an enterFrame listener at 60fps
Cheers and please help!!! Any suggestions will be considered
[lua]local function playerMovement(event)
if accelerometerReady and gameIsActive then
local player = player
local pBlock = player.contentWidth/2
local cameraBounds = cameraBounds
local _yG = _yG
local _xG = _xG
local floor = math.floor
local atan2 = math.atan2
local pi = math.pi
– Set Calibrated X&Y Values (Reversed X&Y Sums due to device being in landscape mode)
local currentYValue = remote.xGravity - _xG
local currentXValue = remote.yGravity - _yG
–Store Current X/Y Pos
local playerX = player.x
local playerY = player.y
–Store New Pos
local newXPos = playerX + (floor(currentXValue * 20))
local newYPos = playerY + (floor(currentYValue * 20))
–Calc Rotation
–Take X&Y radian values and convert into degrees
local currentRotation = (floor(atan2(currentYValue , currentXValue) * ( 180 / pi)))
if currentRotation < 0 then currentRotation = 360 + currentRotation end
–Set Rotation
if player.mode == “attack” then
if currentXValue >= 0 then
player.rotation = 180
elseif currentXValue < 0 then
player.rotation = 0
end
elseif player.y >= (cameraBounds.yMin+(pBlock*2)) and player.mode == “swim” then
player.rotation = (floor(currentRotation))
elseif player.y < (cameraBounds.yMin+(pBlock*2)) and player.mode == “swim” then
if currentXValue >= 0 then
player.rotation = 0
elseif currentXValue < 0 then
player.rotation = 180
end
end
–Move Player To New Pos If In Bounds
if newXPos < (cameraBounds.xMin+pBlock) then
player.x = (cameraBounds.xMin+pBlock)
elseif newXPos > (cameraBounds.xMax-pBlock) then
player.x = (cameraBounds.xMax-pBlock)
else
player.x = newXPos
end
if newYPos < (cameraBounds.yMin+pBlock) then
player.y = (cameraBounds.yMin+pBlock)
elseif newYPos > (cameraBounds.yMax-pBlock) then
player.y = (cameraBounds.yMax-pBlock)
else
player.y = newYPos
end
–Camera:
–Make Camera Follow Player w Centred View. If within Camera Bounds
local pX = player.x
local pY = player.y
–Camera X
if pX >= (cameraBounds.xMin+_W/2) and pX <= (cameraBounds.xMax-_W/2) then
camera.x = -pX + _W/2
end
–Camera Y
if pY >= cameraBounds.yMin and pY <= (cameraBounds.yMax-_H/2) then
camera.y = -pY + _H/2
end
end
end[/lua]
[import]uid: 26289 topic_id: 28509 reply_id: 328509[/import]