Page 1 of 1

What's wrong with this bounding box?

Posted: Sun Sep 30, 2012 6:57 pm
by luaz
I'm using Hardon Collider, but I think it's something in my code instead.

Here's my code, with function names nearby:

Code: Select all

	.load()
	player.colRect = col:addRectangle(player.x, player.y, player.width, player.height)
	-- .update(dt)
	-- I have to add player.width / 2 for some reason, otherwise the bounding rectangle is drawn not right under the sprite.
	player.colRect:moveTo(player.x + player.width / 2, player.y + player.height / 2)
The image below illustrates the problem I'm talking about.

Image

While the collision works, is detected, etc., when the key is held down, the sprite moves a bit into the sprite it's colliding with. It's explainable by the fact that the colliding box isn't moving, however it allows the sprite to move, even though it shouldn't, as it uses the same position variables as the sprite.

Just in case, here's what I do if the shapes collide: player.x = 128 - player.width (128 is the X position of the test sprite)

P.S. I think this can be solved without attaching .love.

Re: What's wrong with this bounding box?

Posted: Sun Sep 30, 2012 7:08 pm
by qaisjp
Only move the bounding box, and draw the bricks relative to the bounding box. This way if the bounding box doesn't move, then the texture brick won't.

Re: What's wrong with this bounding box?

Posted: Sun Sep 30, 2012 7:11 pm
by luaz
qaisjp wrote:Only move the bounding box, and draw the bricks relative to the bounding box. This way if the bounding box doesn't move, then the texture brick won't.
I move the bounding box as player.x, and I set the sprite's x to player.x as well. Technically, they are both moving, but if one can't move, so can't the other. I don't see a problem here. What exactly are you suggesting?

Re: What's wrong with this bounding box?

Posted: Sun Sep 30, 2012 7:12 pm
by qaisjp
luaz wrote:
qaisjp wrote:Only move the bounding box, and draw the bricks relative to the bounding box. This way if the bounding box doesn't move, then the texture brick won't.
I move the bounding box as player.x, and I set the sprite's x to player.x as well. Technically, they are both moving, but if one can't move, so can't the other. I don't see a problem here. What exactly are you suggesting?
Sorry, I misread.

Regardless it seems you are using Hardoncollider incorrectly.

Re: What's wrong with this bounding box?

Posted: Sun Sep 30, 2012 7:16 pm
by SudoCode
Try shape:move(x,y) and draw the sprite based on that.

Re: What's wrong with this bounding box?

Posted: Sun Sep 30, 2012 7:19 pm
by luaz
qaisjp wrote:Regardless it seems you are using Hardoncollider incorrectly.
That's what I thought. Could you give me an example on how I should be using it?
SudoCode wrote:Try shape:move(x,y) and draw the sprite based on that.
I just tried it before reading your post, same results. ;)

Re: What's wrong with this bounding box?

Posted: Sun Sep 30, 2012 7:21 pm
by SudoCode
You should post the entire .love then. If the sprite is being draw based on the position of the player shape, it should not desync at all.

Re: What's wrong with this bounding box?

Posted: Sun Sep 30, 2012 9:18 pm
by luaz
SudoCode wrote:You should post the entire .love then. If the sprite is being draw based on the position of the player shape, it should not desync at all.
It's syncing now, turns out update sequence matters.

Anyway, I'm still curious on how I should use HC if I'm using it wrong.

Re: What's wrong with this bounding box?

Posted: Mon Oct 01, 2012 6:42 pm
by vrld
luaz wrote:

Code: Select all

	.load()
	player.colRect = col:addRectangle(player.x, player.y, player.width, player.height)
	-- .update(dt)
	-- I have to add player.width / 2 for some reason, otherwise the bounding rectangle is drawn not right under the sprite.
	player.colRect:moveTo(player.x + player.width / 2, player.y + player.height / 2)
See the documentation for HC:addRectangle() and shape:moveTo():
function HC:addRectangle(x, y, w, h) wrote:Note: Shape transformations, e.g. shape:moveTo() and shape:rotate(), will be with respect to the rectangle center, not the upper left corner.
function shape:moveTo(x, y) wrote:Important: This function moves the shape's center to (x,y). [...]