Camera following sprite.

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
rbxkools
Prole
Posts: 5
Joined: Thu Mar 28, 2013 9:45 pm

Camera following sprite.

Post 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.
Attachments
context.love
(903 Bytes) Downloaded 179 times
Last edited by rbxkools on Mon Apr 08, 2013 11:50 pm, edited 2 times in total.
Jackim
Prole
Posts: 12
Joined: Wed Apr 03, 2013 3:02 am
Location: Canada

Re: Camera following sprite.

Post by Jackim »

Looks like you forgot to include your .love
User avatar
Ragzouken
Citizen
Posts: 84
Joined: Fri Aug 10, 2012 7:59 am
Contact:

Re: Camera following sprite.

Post 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.
10$man
Citizen
Posts: 77
Joined: Sun Apr 22, 2012 10:40 pm

Re: Camera following sprite.

Post 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.
:ehem:
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.
User avatar
markgo
Party member
Posts: 189
Joined: Sat Jan 05, 2013 12:21 am
Location: USA

Re: Camera following sprite.

Post 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:

Code: Select all

love.graphics.translate(-x,-y)
Next you translate your character to point x2,y2:

Code: Select all

love.graphics.translate(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)
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Camera following sprite.

Post 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.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Camera following sprite.

Post by kikito »

There is also gamera: viewtopic.php?f=5&t=12033
When I write def I mean function.
Post Reply

Who is online

Users browsing this forum: No registered users and 47 guests