Help please. Trying to make a object rotate within a limited axis

Hi i’m stuck here and could use some back up.

I’m trying to make my character rotate ever so slightly up or down, dependent on the direction he is moving. The control method is using the accelerometer. I’ve worked out how to do full rotation but really I only want him moving ten degrees up or down and this has to relate to the direction he is facing i e left or right (the direction is done by animation sprite sheets). This rotation also probably needs to be delayed too so as to not jerk around.

The best game example of this I can think of would be Hungry Shark

Any help please?

BEGIN CODE

[lua]
–Work Out Rotation
local currentRotation = (flr(atn2( currentYValue , currentXValue ) * ( 180 / pi ) ) )-- Take X&Y radian values and convert into degrees
if currentRotation < 0 then
currentRotation = 360 + currentRotation
end

local isWithinRightFacingLimits = currentRotation >= 0 and currentRotation <= 25 or currentRotation >= 325 and currentRotation <= 360
local isWithinLeftFacingLimits = currentRotation >= 155 and currentRotation <= 205
if currentXValue >= 0.2 then
– Facing Left
player.xScale = 1
if isWithinLeftFacingLimits then
player.rotation = currentRotation
end
elseif currentXValue < 0.2 then
– Facing Right
player.xScale = -1
if isWithinRightFacingLimits then
player.rotation = currentRotation
end
end
[/lua]
[import]uid: 26289 topic_id: 35884 reply_id: 335884[/import]

Can you reduce your code to the simplest problem and post the full code. Often a good solution starts by reducing code to the smallest workable piece. [import]uid: 8271 topic_id: 35884 reply_id: 142678[/import]

Couldn’t agree more Horace. I just can’t crack this nut though and can’t see how I can simplify this function anymore, other than a few performance related changes. Any help would be awesome

What you are looking at is the accelerometer event listener. It is one of the only constant event listeners in my game as I have eliminated the main game loop (among others) to increase FPS

[lua]
–Player Movement Function (Runtime Listener)
playerMovement = function(event)
if accelerometerReady then

local player = player
local LevelBoundsXmin = LevelBoundsXmin
local playerBoundsXMax = playerBoundsXMax
local playerBoundsYMin = playerBoundsYMin
local playerBoundsYMax = playerBoundsYMax
local _yG = _yG – This value is taken from the calibration button
local _xG = _xG – This value is taken from the calibration button
local newXPos
local newYPos

local flr = math.floor
local atn2 = math.atan2
local pi = math.pi

– Set User Calibration X&Y Values
local currentYValue = event.xGravity - _xG
local currentXValue = event.yGravity - _yG

–Set Speed Of Player
currentYValue = currentYValue * player.speed
currentXValue = currentXValue * player.speed

–Store New Pos
newXPos = player.x + currentXValue
newYPos = player.y + currentYValue

–Move Player To New Pos If In Level Bounds
if newXPos > playerBoundsXMin and newXPos < playerBoundsXMax then
player.x = newXPos
end

if newYPos > playerBoundsYMin and newYPos < playerBoundsYMax then
player.y = newYPos
end

–Work Out Rotation
– Take X&Y radian values and convert into degrees
local currentRotation = (flr(atn2( currentYValue , currentXValue ) * ( 180 / pi ) ) )
if currentRotation < 0 then
currentRotation = 360 + currentRotation
end

local isWithinRightFacingLimits = currentRotation >= 0 and currentRotation <= 25 or currentRotation >= 325 and currentRotation <= 360
local isWithinLeftFacingLimits = currentRotation >= 155 and currentRotation <= 205

if currentXValue >= 0.2 then
– Facing Left
player.xScale = 1 – Using this to reflect image for now. Will be replaved by left facing sprite
if isWithinLeftFacingLimits then
player.rotation = currentRotation
end
elseif currentXValue < 0.2 then
– Facing Right
player.xScale = -1 – Using this to reflect image for now. Will be replaved by right facing sprite
if isWithinRightFacingLimits then
player.rotation = currentRotation
end
end

–Camera Follow Player w Centred View. If within Camera Bounds
if gameIsActive then
local cameraBoundsXMin = cameraBoundsXMin
local cameraBoundsXMax = cameraBoundsXMax
local cameraBoundsYMin = cameraBoundsYMin
local cameraBoundsYMax = cameraBoundsYMax
–Camera X
if player.x >= cameraBoundsXMin and player.x <= cameraBoundsXMax then
camera.x = -player.x + _W/2
end
–Camera Y
if player.y >= cameraBoundsYMin and player.y <= cameraBoundsYMax then
camera.y = -player.y + _H/2
end
end
end
end
[/lua]

I’m thinking of using boolean flags when the player changes direction to change to limits to which it can be rotated. What do you think of this idea?

Thanks [import]uid: 26289 topic_id: 35884 reply_id: 142727[/import]

Ok, well, I’m not quite sure which visual effect you’re trying to achieve. Could you make an animated gif or some diagram as a jpg to illustrate? I’ll see if I can knock up some code from that. [import]uid: 8271 topic_id: 35884 reply_id: 142733[/import]

This should demonstrate the desired effect. Basically if the player moves up the image tilts slightly up and if they move down the same thing

Here is 20 degrees up and down
http://www.flickr.com/photos/93221208@N02/8475061344/

This sprite is facing right but when it faces left it will need the same effect

Massive thanks hope this makes it easier if not I’ll try again [import]uid: 26289 topic_id: 35884 reply_id: 142819[/import]

Have you taken a look at the SampleCode/Hardware/Accelerometer1 demo in the Corona sample directory? [import]uid: 8271 topic_id: 35884 reply_id: 142984[/import]

Yeah. Sorry I can’t see how that helps me here?

I know how to move an object using the accelerometer and I know how to move and rotate an object using the accelerometer. I just don’t know how to lock the rotation to a specific value and make the movement delayed.

Thanks [import]uid: 26289 topic_id: 35884 reply_id: 143143[/import]

If the code works and all you want is to restrict motion, the route to take is to play around and test the throttling values - the values you apply as a threshold.

To delay motion, simply don’t apply the value from the accelerometer directly, use it as a target value and climb your object’s values toward it.

I can’t really run your code locally to test this myself as it is not a complete listing, but it seems that you’ve got everything else that you should need in there. [import]uid: 8271 topic_id: 35884 reply_id: 143151[/import]

Can you reduce your code to the simplest problem and post the full code. Often a good solution starts by reducing code to the smallest workable piece. [import]uid: 8271 topic_id: 35884 reply_id: 142678[/import]

Couldn’t agree more Horace. I just can’t crack this nut though and can’t see how I can simplify this function anymore, other than a few performance related changes. Any help would be awesome

What you are looking at is the accelerometer event listener. It is one of the only constant event listeners in my game as I have eliminated the main game loop (among others) to increase FPS

[lua]
–Player Movement Function (Runtime Listener)
playerMovement = function(event)
if accelerometerReady then

local player = player
local LevelBoundsXmin = LevelBoundsXmin
local playerBoundsXMax = playerBoundsXMax
local playerBoundsYMin = playerBoundsYMin
local playerBoundsYMax = playerBoundsYMax
local _yG = _yG – This value is taken from the calibration button
local _xG = _xG – This value is taken from the calibration button
local newXPos
local newYPos

local flr = math.floor
local atn2 = math.atan2
local pi = math.pi

– Set User Calibration X&Y Values
local currentYValue = event.xGravity - _xG
local currentXValue = event.yGravity - _yG

–Set Speed Of Player
currentYValue = currentYValue * player.speed
currentXValue = currentXValue * player.speed

–Store New Pos
newXPos = player.x + currentXValue
newYPos = player.y + currentYValue

–Move Player To New Pos If In Level Bounds
if newXPos > playerBoundsXMin and newXPos < playerBoundsXMax then
player.x = newXPos
end

if newYPos > playerBoundsYMin and newYPos < playerBoundsYMax then
player.y = newYPos
end

–Work Out Rotation
– Take X&Y radian values and convert into degrees
local currentRotation = (flr(atn2( currentYValue , currentXValue ) * ( 180 / pi ) ) )
if currentRotation < 0 then
currentRotation = 360 + currentRotation
end

local isWithinRightFacingLimits = currentRotation >= 0 and currentRotation <= 25 or currentRotation >= 325 and currentRotation <= 360
local isWithinLeftFacingLimits = currentRotation >= 155 and currentRotation <= 205

if currentXValue >= 0.2 then
– Facing Left
player.xScale = 1 – Using this to reflect image for now. Will be replaved by left facing sprite
if isWithinLeftFacingLimits then
player.rotation = currentRotation
end
elseif currentXValue < 0.2 then
– Facing Right
player.xScale = -1 – Using this to reflect image for now. Will be replaved by right facing sprite
if isWithinRightFacingLimits then
player.rotation = currentRotation
end
end

–Camera Follow Player w Centred View. If within Camera Bounds
if gameIsActive then
local cameraBoundsXMin = cameraBoundsXMin
local cameraBoundsXMax = cameraBoundsXMax
local cameraBoundsYMin = cameraBoundsYMin
local cameraBoundsYMax = cameraBoundsYMax
–Camera X
if player.x >= cameraBoundsXMin and player.x <= cameraBoundsXMax then
camera.x = -player.x + _W/2
end
–Camera Y
if player.y >= cameraBoundsYMin and player.y <= cameraBoundsYMax then
camera.y = -player.y + _H/2
end
end
end
end
[/lua]

I’m thinking of using boolean flags when the player changes direction to change to limits to which it can be rotated. What do you think of this idea?

Thanks [import]uid: 26289 topic_id: 35884 reply_id: 142727[/import]

Ok, well, I’m not quite sure which visual effect you’re trying to achieve. Could you make an animated gif or some diagram as a jpg to illustrate? I’ll see if I can knock up some code from that. [import]uid: 8271 topic_id: 35884 reply_id: 142733[/import]

This should demonstrate the desired effect. Basically if the player moves up the image tilts slightly up and if they move down the same thing

Here is 20 degrees up and down
http://www.flickr.com/photos/93221208@N02/8475061344/

This sprite is facing right but when it faces left it will need the same effect

Massive thanks hope this makes it easier if not I’ll try again [import]uid: 26289 topic_id: 35884 reply_id: 142819[/import]

Have you taken a look at the SampleCode/Hardware/Accelerometer1 demo in the Corona sample directory? [import]uid: 8271 topic_id: 35884 reply_id: 142984[/import]

Yeah. Sorry I can’t see how that helps me here?

I know how to move an object using the accelerometer and I know how to move and rotate an object using the accelerometer. I just don’t know how to lock the rotation to a specific value and make the movement delayed.

Thanks [import]uid: 26289 topic_id: 35884 reply_id: 143143[/import]

If the code works and all you want is to restrict motion, the route to take is to play around and test the throttling values - the values you apply as a threshold.

To delay motion, simply don’t apply the value from the accelerometer directly, use it as a target value and climb your object’s values toward it.

I can’t really run your code locally to test this myself as it is not a complete listing, but it seems that you’ve got everything else that you should need in there. [import]uid: 8271 topic_id: 35884 reply_id: 143151[/import]

Can you reduce your code to the simplest problem and post the full code. Often a good solution starts by reducing code to the smallest workable piece. [import]uid: 8271 topic_id: 35884 reply_id: 142678[/import]

Couldn’t agree more Horace. I just can’t crack this nut though and can’t see how I can simplify this function anymore, other than a few performance related changes. Any help would be awesome

What you are looking at is the accelerometer event listener. It is one of the only constant event listeners in my game as I have eliminated the main game loop (among others) to increase FPS

[lua]
–Player Movement Function (Runtime Listener)
playerMovement = function(event)
if accelerometerReady then

local player = player
local LevelBoundsXmin = LevelBoundsXmin
local playerBoundsXMax = playerBoundsXMax
local playerBoundsYMin = playerBoundsYMin
local playerBoundsYMax = playerBoundsYMax
local _yG = _yG – This value is taken from the calibration button
local _xG = _xG – This value is taken from the calibration button
local newXPos
local newYPos

local flr = math.floor
local atn2 = math.atan2
local pi = math.pi

– Set User Calibration X&Y Values
local currentYValue = event.xGravity - _xG
local currentXValue = event.yGravity - _yG

–Set Speed Of Player
currentYValue = currentYValue * player.speed
currentXValue = currentXValue * player.speed

–Store New Pos
newXPos = player.x + currentXValue
newYPos = player.y + currentYValue

–Move Player To New Pos If In Level Bounds
if newXPos > playerBoundsXMin and newXPos < playerBoundsXMax then
player.x = newXPos
end

if newYPos > playerBoundsYMin and newYPos < playerBoundsYMax then
player.y = newYPos
end

–Work Out Rotation
– Take X&Y radian values and convert into degrees
local currentRotation = (flr(atn2( currentYValue , currentXValue ) * ( 180 / pi ) ) )
if currentRotation < 0 then
currentRotation = 360 + currentRotation
end

local isWithinRightFacingLimits = currentRotation >= 0 and currentRotation <= 25 or currentRotation >= 325 and currentRotation <= 360
local isWithinLeftFacingLimits = currentRotation >= 155 and currentRotation <= 205

if currentXValue >= 0.2 then
– Facing Left
player.xScale = 1 – Using this to reflect image for now. Will be replaved by left facing sprite
if isWithinLeftFacingLimits then
player.rotation = currentRotation
end
elseif currentXValue < 0.2 then
– Facing Right
player.xScale = -1 – Using this to reflect image for now. Will be replaved by right facing sprite
if isWithinRightFacingLimits then
player.rotation = currentRotation
end
end

–Camera Follow Player w Centred View. If within Camera Bounds
if gameIsActive then
local cameraBoundsXMin = cameraBoundsXMin
local cameraBoundsXMax = cameraBoundsXMax
local cameraBoundsYMin = cameraBoundsYMin
local cameraBoundsYMax = cameraBoundsYMax
–Camera X
if player.x >= cameraBoundsXMin and player.x <= cameraBoundsXMax then
camera.x = -player.x + _W/2
end
–Camera Y
if player.y >= cameraBoundsYMin and player.y <= cameraBoundsYMax then
camera.y = -player.y + _H/2
end
end
end
end
[/lua]

I’m thinking of using boolean flags when the player changes direction to change to limits to which it can be rotated. What do you think of this idea?

Thanks [import]uid: 26289 topic_id: 35884 reply_id: 142727[/import]

Ok, well, I’m not quite sure which visual effect you’re trying to achieve. Could you make an animated gif or some diagram as a jpg to illustrate? I’ll see if I can knock up some code from that. [import]uid: 8271 topic_id: 35884 reply_id: 142733[/import]

This should demonstrate the desired effect. Basically if the player moves up the image tilts slightly up and if they move down the same thing

Here is 20 degrees up and down
http://www.flickr.com/photos/93221208@N02/8475061344/

This sprite is facing right but when it faces left it will need the same effect

Massive thanks hope this makes it easier if not I’ll try again [import]uid: 26289 topic_id: 35884 reply_id: 142819[/import]

Have you taken a look at the SampleCode/Hardware/Accelerometer1 demo in the Corona sample directory? [import]uid: 8271 topic_id: 35884 reply_id: 142984[/import]