Thanksbobbyjones wrote:For room one you did self:whatever it should be room1:whatever.
HUMP - yet another set of helpers
- HaftSwimmingly
- Prole
- Posts: 13
- Joined: Fri Dec 11, 2015 8:13 am
Re: HUMP - yet another set of helpers
tomaki on IRC
Re: HUMP - yet another set of helpers
Hello, I'm wondering if anyone can help me out. I'm using hump.camera and I want to know how I can lock the camera inside the level map boundary; I need a solution that also considers camera scale. Hump.camera has a lockWindow method but it does the opposite, it only moves the camera when the position (such as player) is outside of this bounding box. I want the camera bounding box to stay within the level x,y,w and h. I'm using STI for tiled maps, with a basic 64x64 grid.
I know I can just lock x and y when the player moves near the borders but this don't work when I scale the camera from 1x to 2x.
I know I can just lock x and y when the player moves near the borders but this don't work when I scale the camera from 1x to 2x.
Re: HUMP - yet another set of helpers
Hey Akundaio, the math behind it would be fairly simple.
You want to contain one rectangle (the camera's viewport) inside another rectangle (bounds of the map).
The easiest approach is to transform the camera rect in world coords.
Assuming the center of the camera is in world coords:
Next you clamp the transformed viewport rect to the map bounds:
You want to contain one rectangle (the camera's viewport) inside another rectangle (bounds of the map).
The easiest approach is to transform the camera rect in world coords.
Assuming the center of the camera is in world coords:
Code: Select all
local s = 1/camera.zoom -- zoom or scale of the camera
local vw, wh = camera.w, camera.h -- dimensions of the viewport (in screen coords)
local wvw, wvh = vw*s, vh*s -- dimensions of the viewport (in world coords)
Code: Select all
camera.x = math.max(camera.x, map.left + wvw)
camera.x = math.min(camera.x, map.right - wvw)
camera.y = math.max(camera.y, map.top + wvh)
camera.y = math.min(camera.y, map.bottom - wvh)
Re: HUMP - yet another set of helpers
Ah, I see what I was doing wrong after looking at your example. This is the solution that worked in my case:
Thanks for the help, cheers.
Code: Select all
local wvw, wvh = windowWidth/(2*cam.scale), windowHeight/(2*cam.scale)
cam.x = math.clamp(cam.x,0+wvw,1024-wvw)
cam.y = math.clamp(cam.y,0+wvh,map.height*64-wvh)
Re: HUMP - yet another set of helpers
Hi there.
I tried to create timed level progression using hump.timer but all I get is crashes. Any idea what's wrong?
I tried to create timed level progression using hump.timer but all I get is crashes. Any idea what's wrong?
Code: Select all
Timer = require("lib/hump.timer")
function love.load()
love.graphics.setColor(0,0,0)
love.graphics.setBackgroundColor(255,255,255)
TIMERS = {}
TI = 1
Timer.every(TI, function() addTimer(TI); TI = TI + 1 end)
end
function love.update(dt)
Timer.update(dt)
for k,v in pairs(TIMERS) do
TIMERS[k].update(dt)
end
end
function love.draw()
end
function addTimer(ti)
local tmpt = Timer.new()
tmpt.every(ti, print("Timer: "..ti))
table.insert(TIMERS, ti, tmpt)
end
Code: Select all
Timer: 1
Error: lib/hump/timer.lua:62: attempt to call upvalue 'func' (a nil value)
stack traceback:
lib/hump/timer.lua:62: in function 'after'
lib/hump/timer.lua:44: in function 'update'
main.lua:15: in function 'update'
[string "boot.lua"]:463: in function <[string "boot.lua"]:435>
[C]: in function 'xpcall'
Re: HUMP - yet another set of helpers
Code: Select all
tmpt.every(ti, print("Timer: "..ti))
Code: Select all
tmpt.every(ti, function() print("Timer: "..ti) end)
- Every second, add a Timer instance, for which:
- Every two (three, four, ...) seconds, print the text "Timer: {number}" to the console
Is that what you intended to do?
Re: HUMP - yet another set of helpers
Yes, I intended to spawn increasingly more enemies in a sort-of random time interval. This was the way I came up with (if you replace print with spawn, adjust time intervals to taste).
Is there a better way to do something like that?
Is there a better way to do something like that?
Re: HUMP - yet another set of helpers
Timer.script is great for things like this. For example:
Code: Select all
Timer.script(function(wait)
local time_passed = 0
while true do -- replace with terminating condition
for i = 1,getNumberOfEnemies(time_passed) do
spawnEnemy()
end
local time_to_wait = getWaitTime(time_passed)
wait(time_to_wait)
time_passed = time_passed + time_to_wait
end
end
Re: HUMP - yet another set of helpers
Hi
I need some help with camera coordinates.
I am using Hump for the camera and STI for the tilemap.
In Tiled I have added a object called startPoint (where i want my sprite to spawn). The x,y value that i get from Tiled is 320, 2880.
I am simply unable to use this value to spawn the spite. It always spawns somewhere else. I tried using self.cam:worldCoords but the value is again different from what I expected. Manually i can see I need to place the sprite at 708, 1602
Can someone please help me here? Is there a problem with using hump.Camera and STI together?
I am essentially doing this;
and I am drawing the spite like this;
My code is at;
https://github.com/rishavs/RoguishLove
The relevant code is in the _state_Game.lua file around lines 61 and 117.
I need some help with camera coordinates.
I am using Hump for the camera and STI for the tilemap.
In Tiled I have added a object called startPoint (where i want my sprite to spawn). The x,y value that i get from Tiled is 320, 2880.
I am simply unable to use this value to spawn the spite. It always spawns somewhere else. I tried using self.cam:worldCoords but the value is again different from what I expected. Manually i can see I need to place the sprite at 708, 1602
Can someone please help me here? Is there a problem with using hump.Camera and STI together?
I am essentially doing this;
Code: Select all
for k, object in pairs(self.map.objects) do
if object.name == "startPoint" then
startPointX, startPointY = self.cam:worldCoords(object.x, object.y)
print("startpoint", object.x, object.y)
elseif object.name == "endPoint" then
endPointX, endPointY = self.cam:cameraCoords(object.x, object.y)
-- print("endpoint", object.x, object.y)
end
end
Code: Select all
self.map:draw()
love.graphics.draw(img, startPointX, startPointY, 0, 1, 1, imgWidth / 2, imgHeight / 2)
https://github.com/rishavs/RoguishLove
The relevant code is in the _state_Game.lua file around lines 61 and 117.
Re: HUMP - yet another set of helpers
I dont know what's going on with my Timer, it should make my obstacles doing a smooth oscillation in the X axis, but they go crazy, oscillating very fast gradually. That's the code:
Should I post more code, or the entire .lua file?
Code: Select all
if self.type == "oscillator-x" then
obstacleTimer.script(function(wait)
while true do
obstacleTimer.tween(7, self, {distFromMid = 100}, "in-linear", function()
obstacleTimer.tween(7, self, {distFromMid = 400}, "in-linear") -- const.gameWidth / 2
end) -- self.radius
wait(1)
end
end)
end
Who is online
Users browsing this forum: No registered users and 3 guests