Camera / Scene shake effect (like earthquake)

  • #1, by AkcayKaraazmakSunday, 07. September 2014, 16:48 6 years ago
    Hi mates, anybody know how to shake the scene or just a scene object or the entire scene. Like an earthquake effect?

    Great Poster

    437 Posts


  • #2, by afrlmeSunday, 07. September 2014, 17:18 6 years ago
    There's actually an earthquake feature already which can be found under the miscellaneous action parts. I believe that it will be improved in the future so that you can control the force & speed of x & y independently.

    You could also use the new shader camera controls to make an earthquake effect also or if you wanted to shake an object then you could use the new move object action part or the lua script equivalent that can be found on the wiki under workflow functions on the script index page.

    Imperator

    6812 Posts

  • #3, by AkcayKaraazmakSunday, 07. September 2014, 17:22 6 years ago
    Thank you so much Lee. And how I could give a random value in a script?

    Great Poster

    437 Posts

  • #4, by AkcayKaraazmakSunday, 07. September 2014, 18:14 6 years ago
    Could you please write an example script that can shake an object in a loop with shader camera functions?

    Great Poster

    437 Posts

  • #5, by AkcayKaraazmakSunday, 07. September 2014, 18:18 6 years ago
    I've tried something like this but didnt work :\

    local _offset = 500
    local _delay = 5000
    local _easing = easeBounceInOut
    local _axis = "x"

    if getObject("Game.GameCurrentScene.SceneValues[Scene_01_flag]") == 1

    _offset=math.random(1,100)

    shaderPan(_offset, _delay, _easing, _axis)

    end

    -- Scene_01_flag is set with "at beginning of scene" action it will be 0 with the " at end of scene action"
    -- I think, I also cant get the value of an object in a scene.

    Great Poster

    437 Posts

  • #6, by afrlmeSunday, 07. September 2014, 19:29 6 years ago
    About the shader camera... like the earthquake feature it currently pans the camera off screen, so that you can see black on the outside. I'm not sure if you want it to do that or not, but I've asked Simon if he can add me a couple entries in the data structure at some point (when he can) which will allow me to prevent the camera from going off the screen by checking the current scroll position against the game resolution & the scene background width & height.

    quick example of random value using math.random function...
    math.random(100) -- will generate a random value between 0 & 100
    math.random(5, 20) -- generate a random value between 5 & 20 etc...
    1
    2
    3

    quick example of random earthquake using shaderPan() function...
    local pos -- variable which will be used to store current scroll position
    local tx = 0 -- variable which will store previous time value for x
    local ty = 0 -- variable which will store previous time value for y
    local dx = 0 -- variable which will be used to store random delay value for x pan
    local dy = 0 -- variable which will be used to store random delay value for y pan
    local rx, ry -- variables used to store random offset values for x & y

    function loopQuake()
    pos = game:getPoint(VGameScrollPosition)
    -- x --
    if tx == 0 or getTime() >= tx + dx then
    rx = math.random(-10, 10)
    dx = math.random(1000, 3000)
    shaderPan(pos + rx, -1, dx, easeBounceOut)
    tx = getTime()
    end
    -- y --
    if ty == 0 or getTime() >= ty + dy then
    ry = math.random(-10, 10)
    dy = math.random(1000, 3000)
    shaderPan(-1, pos + ry, dy, easeBounceOut)
    ty = getTime()
    end
    end

    function startQuake()
    registerEventHandler("mainLoop", "loopQuake")
    end

    function endQuake()
    unregisterEventHandler("mainLoop", "loopQuake")
    end
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33

    ...I've written this off the top of my head & have no clue if it will work or not. You will need to include the latest version of the shader toolkit script as I've just updated the shaderPan function.

    To start earthquake, just create an execute a script action part containing...
    startQuake()
    1
    2

    ...to end earthquake, same again but containing...
    endQuake()
    1
    2


    Quick explanation of the script I've written above: I've made it so that when you execute the startQuake() function it creates a loop (defined in the loopQuake function) which does all the random calculations & random delay values. You start & end the earthquake with the examples I provided above. The main script should be included as a definition script.

    Imperator

    6812 Posts

  • #7, by AkcayKaraazmakSunday, 07. September 2014, 20:24 6 years ago
    Thank you so much Lee! Much appreciated. Just one small question. So how I can just use this code to effect a scene object. My scene name is "scene_01" and object is "obj1" How I can take and also set the values of scene_01 and obj_01 (scene_01_val and obj_01_val)

    Thank you bro!

    Great Poster

    437 Posts

  • #8, by afrlmeSunday, 07. September 2014, 20:30 6 years ago
    you would need to use the moveObj() workflow function I created. But the code above needs adapting for that as it would be more beneficial to create a table so that it gets the default position of each object (top left corner) & create the delay & movement values for each one, thus only requiring one single loop handler for all of the created objects. To stop an object from shaking you would have to reset it back to its position & then remove it from the table.

    It's a bit complicated for me to try & type out a script example off the top of my head & expect it to work without testing it first.

    What would have been nice is, if the move object action part allowed us to set random values for x, y & delay independently. Unfortunately, it does not, thus my moveObj function is required to do this.

    Imperator

    6812 Posts

  • #9, by AkcayKaraazmakSunday, 07. September 2014, 21:55 6 years ago
    Cheers mate, I'll work with your examples and I hope I'll endup with good results and will report you back smile

    Thank you again for your help Lee!

    Great Poster

    437 Posts

  • #10, by SimonSSunday, 07. September 2014, 22:39 6 years ago
    Lee, with the new version you can do this:
    game.CurrentScene.SceneSprite:getSize()
    Will get the size of the background image as a point.

    Thread Captain

    1333 Posts

  • #11, by afrlmeSunday, 07. September 2014, 23:00 6 years ago
    Ah cheers Simon mate. smile

    So you managed to sort the short code & the scene width stuff? Nice one!

    I'll add that to the shaderPan function when the new version comes out with a boolean option to enable/disable it.

    P.S: will getSize() work for any image sprite or only for scene backgrounds?

    Imperator

    6812 Posts

Write post