Page 1 of 1
Camera following sprite.
Posted: Mon Apr 08, 2013 11:44 pm
by rbxkools
I have a basis problem that I can't seem to fix. I want to translate the 'camera' so the sprite is exactly in the middle no matter if the sprite is moving or jumping.
I am using love.physics.
Currently my sprite is a 30 by 30 block that can move to the right and the left as well as jump.
My screen is 850 by 650.
I conveniently have two variables, px and py being the sprite's x and y position.
I have tried:
love.graphics.translate(px, py) , to no avail
love.graphics.translate(px+850/2, py+650/2) to no avail
and
love.graphics.translate(px-850/2, py-650/2) to no avail
How can I make it so the sprite will always be in the middle. The .love I included had the love.graphics.translate(px, py) being used.
Thanks, rbxkools
Edit: Attached the .love, sorry.
Re: Camera following sprite.
Posted: Mon Apr 08, 2013 11:45 pm
by Jackim
Looks like you forgot to include your .love
Re: Camera following sprite.
Posted: Mon Apr 08, 2013 11:59 pm
by Ragzouken
Honestly, just use the HUMP library:
https://www.love2d.org/wiki/HUMP
It will save you so much time and effort; getting camera transforms right can be a massive pain in the ass. In HUMP you just set the camera x,y and do camera:attach() / camera:detach() when drawing non-ui stuff.
Re: Camera following sprite.
Posted: Tue Apr 09, 2013 12:17 am
by 10$man
Ragzouken wrote:Honestly, just use the HUMP library:
https://www.love2d.org/wiki/HUMP
It will save you so much time and effort; getting camera transforms right can be a massive pain in the ass. In HUMP you just set the camera x,y and do camera:attach() / camera:detach() when drawing non-ui stuff.
Try this one:
love.graphics.translate(SCREEN_WIDTH / 2 - (px - PLAYER_WIDTH / 2), SCREEN_HEIGHT / 2 - (py - PLAYER_HEIGHT / 2))
Hopefully obviously, just replace the capitalized variables with your variables. That should keep your character centered.
Of course, should you want to use HUMP, go for it.
Re: Camera following sprite.
Posted: Tue Apr 09, 2013 12:47 am
by markgo
Here's some math on how to center your character:
Your character's center is at position x,y and you want to center your character on point x2,y2.
You'll want to translate your character to the origin first:
Next you translate your character to point x2,y2:
or if you want to combine it in one step:
Code: Select all
love.graphics.translate(-x+x2,-y+y2)
Example: Your screen is 800x600, and you want to center your character at point 400,300. Your character is located at x,y. This is what you do:
Code: Select all
love.graphics.translate(-x+400,-y+300)
Re: Camera following sprite.
Posted: Tue Apr 09, 2013 2:42 am
by substitute541
I don't use love.translate very much, just some worldX, worldY, worldWidth, and worldHeight variables, and then adding those values to the coordinates of the stuff while drawing them using love.draw.
Re: Camera following sprite.
Posted: Tue Apr 09, 2013 9:11 am
by kikito