## Camera Shake? / Collisions / Enemies...

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

### Re: Camera Shake?

I just tested the code you sent Robin, and its exactly what I wanted. I might as well ask while I'm here but one major issue that I'm hours away from facing is collision. I have done basic pixel collision but it seems really inefficient. Is there a way to create a sort of "hitbox" around the character and enemies? This is the corresponding code for love.load, love.update, and love.draw for how my characters are set up and drawn:

Code: Select all

function love.load()

player = {
hp = 100,
status = "ALIVE",
x = 100,
y = 100,

}

animsource = {
walkleft = love.graphics.newImage("assets/sprites/walk_left.png"),
walkright = love.graphics.newImage("assets/sprites/walk_right.png"),
walkup = love.graphics.newImage("assets/sprites/walk_up.png"),
walkdown = love.graphics.newImage("assets/sprites/walk_down.png"),
standleft = love.graphics.newImage("assets/sprites/stand_left.png"),
standright = love.graphics.newImage("assets/sprites/stand_right.png"),
standup = love.graphics.newImage("assets/sprites/stand_up.png"),
standdown = love.graphics.newImage("assets/sprites/stand_down.png"),
shotgun = love.graphics.newImage("assets/weapons/shotgun_anim.png"),
}

anim = {
walkleft = newAnimation(animsource.walkleft, 32, 32, 0.18, 0),
walkright = newAnimation(animsource.walkright, 32, 32, 0.18, 0),
walkup = newAnimation(animsource.walkup, 32, 32, 0.18, 0),
walkdown = newAnimation(animsource.walkdown, 32, 32, 0.18, 0),
standleft = newAnimation(animsource.standleft, 32, 32, 0.18, 0),
standright = newAnimation(animsource.standright, 32, 32, 0.18, 0),
standup = newAnimation(animsource.standup, 32, 32, 0.18, 0),
standdown = newAnimation(animsource.standdown, 32, 32, 0.18, 0),
shotgun = newAnimation(animsource.shotgun, 16, 6, 0.18, 0),
}

direction = "down"
end

function love.update()

anim.walkleft:update(dt)
anim.walkright:update(dt)
anim.walkup:update(dt)
anim.walkdown:update(dt)
anim.standleft:update(dt)
anim.standright:update(dt)
anim.standup:update(dt)
anim.standdown:update(dt)
anim.shotgun:update(dt)

state = anim.walkleft

if direction == "right" then
state = anim.standright
end

if direction == "left" then
state = anim.standleft
end

if direction == "up" then
state = anim.standup
end

if direction == "down" then
state = anim.standdown
end

-- mouse rotation
mouse_x = love.mouse.getX()
mouse_y = love.mouse.getY()

if mouse_x >= player.x then
state = anim.standright
elseif mouse_x <= player.x then
state = anim.standleft
end

if mouse_y >= player.y + 50 then
state = anim.standdown
elseif mouse_y <= player.y - 50 then
state = anim.standup
end

-- keys
if love.keyboard.isDown( "w" ) then
direction = "up"
state = anim.walkup
player.y = player.y - 1

elseif love.keyboard.isDown( "s" ) then
direction = "down"
state = anim.walkdown
player.y = player.y + 1

elseif love.keyboard.isDown( "d" ) then
direction = "right"
state = anim.walkright
player.x = player.x + 1

elseif love.keyboard.isDown( "a" ) then
direction = "left"
state = anim.walkleft
player.x = player.x - 1
end

end

function love.draw()

state:draw(player.x, player.y)

end

This also serves as a basic setup to force the sprite to look at the mouse cursor, that's where all of the mouse stuff comes in. I know its pretty sloppy but it works pretty well. Any help would be appreciated, thanks again guys!
@rynesaur
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

### Re: Camera Shake?

For hitbox collisions you normally define an x and a y (which you have, because you draw the image there) and a width and height, from there on you can use http://love2d.org/wiki/BoundingBox.lua. Do tell if you need more (for example for rotated boxes).
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: Camera Shake?

Per pixel collision is not fun unless you're dealing with large images that are oddly shaped.
You should probably do basic nonrotated box collisions if you don't have player rotation / are making an RPG adventure type game.

Edit: Ninja'd by bartbes. :/
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

### Re: Camera Shake?

bartbes wrote:For hitbox collisions you normally define an x and a y (which you have, because you draw the image there) and a width and height, from there on you can use http://love2d.org/wiki/BoundingBox.lua. Do tell if you need more (for example for rotated boxes).
Thanks for the reply, though I'm a little bit confused. So you're saying that I should add a width and height value to my current player and enemy table. Then use the bounding box code and change "box1x" etc. to player.x? Then the same for the y, w, and h values?

EDIT: I just assigned the proper variables to box1 and box2, That was a stupid question
@rynesaur
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: Camera Shake?

Ryne wrote:
bartbes wrote:For hitbox collisions you normally define an x and a y (which you have, because you draw the image there) and a width and height, from there on you can use http://love2d.org/wiki/BoundingBox.lua. Do tell if you need more (for example for rotated boxes).
Thanks for the reply, though I'm a little bit confused. So you're saying that I should add a width and height value to my current player and enemy table. Then use the bounding box code and change "box1x" etc. to player.x? Then the same for the y, w, and h values?
x,y=x,y of your point on one of the corners of your second box (x,y; x,y+h; x+w,y+h; and x+w,y)
bx1,by1=x,y of first box
bx2,by2=x,y of first box + w,h of first box
if x<bx2 and x>bx1 then
if y<by2 and y>by2 then
collision=true
end
end
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

### Re: Camera Shake?

zac352 wrote:
Ryne wrote:
bartbes wrote:For hitbox collisions you normally define an x and a y (which you have, because you draw the image there) and a width and height, from there on you can use http://love2d.org/wiki/BoundingBox.lua. Do tell if you need more (for example for rotated boxes).
Thanks for the reply, though I'm a little bit confused. So you're saying that I should add a width and height value to my current player and enemy table. Then use the bounding box code and change "box1x" etc. to player.x? Then the same for the y, w, and h values?
x,y=x,y of your point on one of the corners of your second box (x,y; x,y+h; x+w,y+h; and x+w,y)
bx1,by1=x,y of first box
bx2,by2=x,y of first box + w,h of first box
if x<bx2 and x>bx1 then
if y<by2 and y>by2 then
collision=true
end
end
Thanks for the reply, I'm still having issues getting this to work. I simply added this to my load function:

Code: Select all

	box1x = player.x
box1y = player.y
box1w = player.w
box1h = player.h

box2x = enemy.x
box2y = enemy.y
box2w = enemy.w
box2h = enemy.h

along with the bounding box code they still don't seem to collide. Any Ideas?
@rynesaur
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: Camera Shake?

Can you show us your full collision code? You may have a problem in your response code.
Ryne
Party member
Posts: 444
Joined: Fri Jan 29, 2010 11:10 am

### Re: Camera Shake?

zac352 wrote:Can you show us your full collision code? You may have a problem in your response code.

Code: Select all

	-- player variables
player = {
hp = 100,
status = "ALIVE",
x = 100,
y = 100,
w = 32,
h = 32,
}

-- enemy variables
enemy = {
hp = 100,
status = "ALIVE",
x = 150,
y = 150,
w = 32,
h = 32,
}

box1x = player.x
box1y = player.y
box1w = player.w
box1h = player.h

box2x = enemy.x
box2y = enemy.y
box2w = enemy.w
box2h = enemy.h


and the function is placed just after love.load and before love.update.

Code: Select all

function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)

if box1x > box2x + box2w - 1 or -- Is box1 on the right side of box2?
box1y > box2y + box2h - 1 or -- Is box1 under box2?
box2x > box1x + box1w - 1 or -- Is box2 on the right side of box1?
box2y > box1y + box1h - 1    -- Is b2 under b1?
then
return false              -- No collision. Yay!
else
return true           -- Yes collision. Ouch!
end
end

Perhaps it's set up right, I just couldn't get it to print if they were colliding or not. Maybe I messed up my print command? I'm confused since the function doesnt contain a variable that I could print. Such as "Colliding = yes".
@rynesaur
ninwa
Party member
Posts: 118
Joined: Tue Oct 12, 2010 1:21 am
Location: Metro Detroit
Contact:

### Re: Camera Shake?

Post removed in lieu of the fact that I feel as though I may just be further confusing a problem I probably misunderstood! - Ninwa
Last edited by ninwa on Sun Nov 07, 2010 1:09 am, edited 2 times in total.
zac352
Party member
Posts: 496
Joined: Sat Aug 28, 2010 8:13 pm
Contact:

### Re: Camera Shake?

Code: Select all

function CheckCollision(box1x, box1y, box1w, box1h, box2x, box2y, box2w, box2h)
if box2x<box1x+box1w and box2x>box1x and --top left
box2y<box1y+box1h and box2y>box1y or
box2x+box2w<box1x+box1w and box2x+box2w>box1x and --bottom right
box2y+box2h<box1y+box1h and box2y+box2h>box1y or
box2x<box1x+box1w and box2x>box1x and --bottom left
box2y+box2h<box1y+box1h and box2y+box2h>box1y or
box2x<box1x+box1w and box2x>box1x and --top right
box2y+box2h<box1y+box1h and box2y+box2h>box1y then
return true
else
return false
end
end

*rewrites statement* Augh. Braindeath.