Experiancing camera shake using hump.lua
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Experiancing camera shake using hump.lua
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
Hello,
I am using this camera library that has a shake support:
https://github.com/a327ex/STALKER-X#shake
I am using this camera library that has a shake support:
https://github.com/a327ex/STALKER-X#shake
My boat driving game demo: https://dusoft.itch.io/captain-bradley- ... itius-demo
Re: Experiancing camera shake using hump.lua
Oh thanks! This might be what I needdusoft 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
Re: Experiancing camera shake using hump.lua
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:
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.
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
Re: Experiancing camera shake using hump.lua
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.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: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.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
Here is my code for reference
- Attachments
-
- Game.love
- (861.36 KiB) Downloaded 71 times
Who is online
Users browsing this forum: Ahrefs [Bot] and 3 guests