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.
Camera following sprite.
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Camera following sprite.
- Attachments
-
- context.love
- (903 Bytes) Downloaded 189 times
Last edited by rbxkools on Mon Apr 08, 2013 11:50 pm, edited 2 times in total.
Re: Camera following sprite.
Looks like you forgot to include your .love
Re: Camera following sprite.
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.
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.
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.
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:
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:
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:
Code: Select all
love.graphics.translate(-x,-y)
Code: Select all
love.graphics.translate(x2,y2)
Code: Select all
love.graphics.translate(-x+x2,-y+y2)
Code: Select all
love.graphics.translate(-x+400,-y+300)
- substitute541
- Party member
- Posts: 484
- Joined: Fri Aug 24, 2012 9:04 am
- Location: Southern Leyte, Visayas, Philippines
- Contact:
Re: Camera following sprite.
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.
Currently designing themes for WordPress.
Sometimes lurks around the forum.
Sometimes lurks around the forum.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Camera following sprite.
There is also gamera: viewtopic.php?f=5&t=12033
When I write def I mean function.
Who is online
Users browsing this forum: No registered users and 5 guests