Page 2 of 6

Re: Camera Shake?

Posted: Sat Nov 06, 2010 11:24 pm
by Ryne
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()

	-- animation updates
	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!

Re: Camera Shake?

Posted: Sat Nov 06, 2010 11:28 pm
by bartbes
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).

Re: Camera Shake?

Posted: Sat Nov 06, 2010 11:30 pm
by zac352
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. :/

Re: Camera Shake?

Posted: Sun Nov 07, 2010 12:14 am
by Ryne
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 :P

Re: Camera Shake?

Posted: Sun Nov 07, 2010 12:18 am
by zac352
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

Re: Camera Shake?

Posted: Sun Nov 07, 2010 12:45 am
by Ryne
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?

Re: Camera Shake?

Posted: Sun Nov 07, 2010 12:51 am
by zac352
Can you show us your full collision code? You may have a problem in your response code. :o:

Re: Camera Shake?

Posted: Sun Nov 07, 2010 12:53 am
by Ryne
zac352 wrote:Can you show us your full collision code? You may have a problem in your response code. :o:
This is love.load

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

Re: Camera Shake?

Posted: Sun Nov 07, 2010 1:02 am
by ninwa
Post removed in lieu of the fact that I feel as though I may just be further confusing a problem I probably misunderstood! - Ninwa

Re: Camera Shake?

Posted: Sun Nov 07, 2010 1:02 am
by zac352

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. :death: