Experiancing camera shake using hump.lua

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

Experiancing camera shake using hump.lua

Post 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?
User avatar
dusoft
Party member
Posts: 510
Joined: Fri Nov 08, 2013 12:07 am
Location: Europe usually
Contact:

Re: Experiancing camera shake using hump.lua

Post by dusoft »

Hello,

I am using this camera library that has a shake support:
https://github.com/a327ex/STALKER-X#shake
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

Re: Experiancing camera shake using hump.lua

Post 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
User avatar
Azzla
Prole
Posts: 38
Joined: Sun Mar 29, 2020 2:23 am

Re: Experiancing camera shake using hump.lua

Post 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.
libraries: Stalkpile
electronic music stuff: https://soundcloud.com/azzlamusic
MaxGamz
Party member
Posts: 100
Joined: Fri Oct 28, 2022 3:09 am

Re: Experiancing camera shake using hump.lua

Post 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
Attachments
Game.love
(861.36 KiB) Downloaded 27 times
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 58 guests