## 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

bobbyjones wrote:For room one you did self:whatever it should be room1:whatever.
Thanks
tomaki on IRC

alundaio
Prole
Posts: 7
Joined: Wed Mar 02, 2016 4:31 am

### 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.

ivan
Party member
Posts: 1703
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

### 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:

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)
Next you clamp the transformed viewport rect to the map bounds:

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)

alundaio
Prole
Posts: 7
Joined: Wed Mar 02, 2016 4:31 am

### 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:

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)
Thanks for the help, cheers.

murks
Party member
Posts: 185
Joined: Tue Jun 03, 2014 4:18 pm

### 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?

Code: Select all

Timer = require("lib/hump.timer")

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

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'


vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

### Re: HUMP - yet another set of helpers

Code: Select all

tmpt.every(ti, print("Timer: "..ti))
will not work, because print("Timer: "..ti) is not a function. The correct usage would be

Code: Select all

tmpt.every(ti, function() print("Timer: "..ti) end)
In any case, here is what you are doing right now:
• Every second, add a Timer instance, for which:
• Every two (three, four, ...) seconds, print the text "Timer: {number}" to the console
After one second, you will have one timer to add timers, and one timer that prints every two seconds. After two seconds, you will have one timer that adds timers and two timers that print to the console every two and three seconds. After three seconds, you will have one timer that adds timers and three timers that print to the console every two, three and four seconds, etc.

Is that what you intended to do?
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

murks
Party member
Posts: 185
Joined: Tue Jun 03, 2014 4:18 pm

### 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?

vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

### 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
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine

Rishavs
Party member
Posts: 103
Joined: Sat Oct 17, 2009 5:29 am
Contact:

### 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

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
and I am drawing the spite like this;

Code: Select all

    self.map:draw()

love.graphics.draw(img,  startPointX, startPointY, 0, 1, 1, imgWidth / 2, imgHeight / 2)
My code is at;
https://github.com/rishavs/RoguishLove
The relevant code is in the _state_Game.lua file around lines 61 and 117.

Kibita
Prole
Posts: 31
Joined: Tue Dec 29, 2015 7:46 pm

### 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:

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