Page 1 of 1

Experiancing camera shake using hump.lua

Posted: Sat Dec 23, 2023 1:54 pm
by MaxGamz
I am creating a new game and I'm having a problem with the camera shake while using hump.lua. For reference I am trying to do something with a rocket blasting off into space. I had the idea of making the map itself move but I feel like it would be too much of a headache since I have to change the physics of the tilemap itself. Should I use another library or tweak my code?

Re: Experiancing camera shake using hump.lua

Posted: Sat Dec 23, 2023 2:02 pm
by dusoft
Hello,

I am using this camera library that has a shake support:
https://github.com/a327ex/STALKER-X#shake

Re: Experiancing camera shake using hump.lua

Posted: Sun Dec 24, 2023 12:57 am
by MaxGamz
dusoft wrote: Sat Dec 23, 2023 2:02 pm Hello,

I am using this camera library that has a shake support:
https://github.com/a327ex/STALKER-X#shake
Oh thanks! This might be what I need

Re: Experiancing camera shake using hump.lua

Posted: Sun Dec 24, 2023 8:02 pm
by Azzla
In case you still want to use hump.camera, I happen to have an old implementation of screen shake using that library alongside hump.timer:

Code: Select all

local cameraFile = require('libs/hump/camera')
cam = cameraFile()
local camSmoothing = cam.smooth.damped(8)
local camTimer = globalTimer.new()
local cameraShaking = false
local magnitude = 1

function cameraHandler(dt, x, y, zoom)
  camTimer:update(dt)
  local wvw, wvh = screen_width/(2*zoom), screen_height/(2*zoom)
  cam:lockPosition(x, y, camSmoothing)
  
  --screen shake
  if cameraShaking then
    cam.x = cam.x + love.math.random(-magnitude,magnitude)
    cam.y = cam.y + love.math.random(-magnitude,magnitude)
  end  
  
  --lock camera to map boundaries
  cam.x = math.max(cam.x, wvw)
  cam.x = math.min(cam.x, map_width - wvw)
  cam.y = math.max(cam.y, wvh)
  cam.y = math.min(cam.y, map_height - wvh)
end

function screenShake(duration, mag)
  magnitude = mag
  cameraShaking = true
  camTimer:after(duration, function() cameraShaking = false end)
end
This code is old and not perfect but it works. Just call the 'screenShake' function with a duration and magnitude wherever you want it (e.g. when the player is damaged) and cameraHandler must be in your update callstack. You may have to do some patching on the variables and such to make it work in your game, of course.

Re: Experiancing camera shake using hump.lua

Posted: Mon Dec 25, 2023 1:06 pm
by MaxGamz
Azzla wrote: Sun Dec 24, 2023 8:02 pm In case you still want to use hump.camera, I happen to have an old implementation of screen shake using that library alongside hump.timer:

Code: Select all

local cameraFile = require('libs/hump/camera')
cam = cameraFile()
local camSmoothing = cam.smooth.damped(8)
local camTimer = globalTimer.new()
local cameraShaking = false
local magnitude = 1

function cameraHandler(dt, x, y, zoom)
  camTimer:update(dt)
  local wvw, wvh = screen_width/(2*zoom), screen_height/(2*zoom)
  cam:lockPosition(x, y, camSmoothing)
  
  --screen shake
  if cameraShaking then
    cam.x = cam.x + love.math.random(-magnitude,magnitude)
    cam.y = cam.y + love.math.random(-magnitude,magnitude)
  end  
  
  --lock camera to map boundaries
  cam.x = math.max(cam.x, wvw)
  cam.x = math.min(cam.x, map_width - wvw)
  cam.y = math.max(cam.y, wvh)
  cam.y = math.min(cam.y, map_height - wvh)
end

function screenShake(duration, mag)
  magnitude = mag
  cameraShaking = true
  camTimer:after(duration, function() cameraShaking = false end)
end
This code is old and not perfect but it works. Just call the 'screenShake' function with a duration and magnitude wherever you want it (e.g. when the player is damaged) and cameraHandler must be in your update callstack. You may have to do some patching on the variables and such to make it work in your game, of course.
Merry Christmas if you celebrate it! But anyways my problem is that I actually don't want camera shake. The camera is set to move the rocket using it's player.x and player.y positions, yet the rocket itself is shaking for some reason.

Here is my code for reference