Shake effect with Perspective/Camera

Greetings,

I’m using RoamingGamer’s shake effect code found here: 

http://roaminggamer.com/2014/06/24/camera-shake-for-corona-sdk/

It works great but I’m using the Perspective Library/code and camera focus to follow a character that moves across the screen and I have different perspective groups that move at different speeds when character moves.

If I remain at start, and trigger the corresponding code for Screen Shake - it works perfect as expected.

However, when I move the character and so the different perspective groups move to mimic the character running across the screen, then trigger the shake effect - and stop - the Groups are off-centered on the x-axis due to the camera/perspective being used.

How to edit this code to work with the Camera/Perspective library?  @roaminggamer

I thought it would have to to do with this part of the code: display.currentStage.x

I’ve tried using camera.x but that doesn’t seem to work either.

Hi, I’m the creator of Perspective. You should be able to shake the camera just by moving the camera’s master group, because all other groups are inside it. So instead of moving display.currentStage, just move camera.

[lua]
local function shake()
if(shakeCount % shakePeriod == 0 ) then
camera.x = camera.x0 + math.random(-xShake, xShake)
camera.y = camera.y0 + math.random(-yShake, yShake)
end
shakeCount = shakeCount + 1
end
[/lua]

I see you said “I’ve tried using camera.x but that doesn’t seem to work either”. How does it not work? The same problem that you mentioned with the X-axis being off-center?

  • Caleb

Right I did that… and the x-axis was off… meaning there’s a black gap, maybe the size of a finger width on left side of screen once the shake stops.

But if I don’t move anything initially and trigger the Shake, with camera.x method or display.currentStage, then it works as intended.

I’ll have to take a look at my code again… Now I think about it… I have a blue background that doesn’t need to be scrolled so I never put it in the camera group- I’ll have to double check make sure everything that needs to be is in that camera group.

If substituting the display.currentStage with camera should work then at least that gives me a good direction to start trouble shooting.

Thanks @ Caleb P

Also - I think it maybe cause of my anchor points.  For whatever reason, when I started this project, and to this point… I used different anchor points (0.5, 0.5 vs -1, 1) … I’m wondering if that is having any affect.  Been meaning to go back and standardize everything - guess this gives me a good excuse to do that.

So with some guidance, thanks again @Caleb P, I figured out the issue.  I’ll summarize so others experiencing similar can check:

First I had some anchor issues with objects and the camera not being the same (probably a terrible habit / code), and was using ‘camera.anchorChildren = true’ for whatever reason.  I have since turned that to false (default), and all objects to anchor 0, 0 (default).

Second, due to my game - there were instances where the shake screen would get triggered twice (chain reaction explosion) which messed up the camera.x0 position.  To resolve, just added a simple ‘if’ statement before launch of the shake function:

if shakeCount == 0 then            -- Start the shake     startShake() end

To prevent it getting called again while still shaking.

Woooo wooo  :smiley:

Glad you got it fixed :slight_smile:

  • Caleb

Hi, I’m the creator of Perspective. You should be able to shake the camera just by moving the camera’s master group, because all other groups are inside it. So instead of moving display.currentStage, just move camera.

[lua]
local function shake()
if(shakeCount % shakePeriod == 0 ) then
camera.x = camera.x0 + math.random(-xShake, xShake)
camera.y = camera.y0 + math.random(-yShake, yShake)
end
shakeCount = shakeCount + 1
end
[/lua]

I see you said “I’ve tried using camera.x but that doesn’t seem to work either”. How does it not work? The same problem that you mentioned with the X-axis being off-center?

  • Caleb

Right I did that… and the x-axis was off… meaning there’s a black gap, maybe the size of a finger width on left side of screen once the shake stops.

But if I don’t move anything initially and trigger the Shake, with camera.x method or display.currentStage, then it works as intended.

I’ll have to take a look at my code again… Now I think about it… I have a blue background that doesn’t need to be scrolled so I never put it in the camera group- I’ll have to double check make sure everything that needs to be is in that camera group.

If substituting the display.currentStage with camera should work then at least that gives me a good direction to start trouble shooting.

Thanks @ Caleb P

Also - I think it maybe cause of my anchor points.  For whatever reason, when I started this project, and to this point… I used different anchor points (0.5, 0.5 vs -1, 1) … I’m wondering if that is having any affect.  Been meaning to go back and standardize everything - guess this gives me a good excuse to do that.

So with some guidance, thanks again @Caleb P, I figured out the issue.  I’ll summarize so others experiencing similar can check:

First I had some anchor issues with objects and the camera not being the same (probably a terrible habit / code), and was using ‘camera.anchorChildren = true’ for whatever reason.  I have since turned that to false (default), and all objects to anchor 0, 0 (default).

Second, due to my game - there were instances where the shake screen would get triggered twice (chain reaction explosion) which messed up the camera.x0 position.  To resolve, just added a simple ‘if’ statement before launch of the shake function:

if shakeCount == 0 then            -- Start the shake     startShake() end

To prevent it getting called again while still shaking.

Woooo wooo  :smiley:

Glad you got it fixed :slight_smile:

  • Caleb