Attach an image to a body???

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
npanagakis
Prole
Posts: 2
Joined: Tue Apr 17, 2012 2:33 am

Attach an image to a body???

Post by npanagakis »

Hey guys, this is my first post, and im sort of a noob. How do you attach an image to a body? I tried searching for answers but all i could find was attaching shapes to bodies. Thanks in advance (:
User avatar
Qcode
Party member
Posts: 170
Joined: Tue Jan 10, 2012 1:35 am

Re: Attach an image to a body???

Post by Qcode »

Well generally you would add a separate image to whatever body you making. So if you were to have the guy as a table like this.

Code: Select all

function love.load()
                     guy = {
                                     x = 0,
                                     y = 0,
                                     i = love.graphics.newImage("person.png")
                     end
end
All that does is attach an image into your guy table. (I simply use i for image feel free to do whatever you want)

Then in the love.draw you would put.

Code: Select all

love.graphics.draw(guy.i, guy.x, guy.y) 
This should simply attach your guy so when is x and y moves the guy moves with them.

If you're talking about bodies like in love.physics then I can't be exact with you but i think you would do something like this.(Assuming you've already made a body)

Code: Select all

x = Body:getX
y = Body:getY
Then in love.draw you would put the same thing except put x and y instead of guy.x and guy.y. This is because the guy is a body not a table in the second example and he would therefore not be described as a table.

Hope that helps :awesome:

Qcode

EDIT: P.S You might also want to post your problems in support and development instead of the general forum. General is meant for discussion and random stuff and support is to help everybody with all their questions.
User avatar
felix24
Party member
Posts: 163
Joined: Tue Jul 26, 2011 4:51 pm
Contact:

Re: Attach an image to a body???

Post by felix24 »

the only thing i can add to Qcode's reply is that if you want to draw the image over a physics body, you will want to give it the angle of the body as well. so that if you have a box bouncing around, or a ball rolling, the image will match the exact position and angle of the body.

For example you could set up your body as follows: (note: the below code is for love 7.2. To get it to work for 8.0 you may need to change it slightly)

Code: Select all

box = {}
box.body = love.physics.newBody(your_world,400, 300, 0, 0) --this is the physics body
box.shape = love.physics.newRectangleShape(box.body, 0, 0, 50, 50, 0) --the shape is for handling collisions
box.shape:setData(your_data)
box.body:setMassFromShapes()
box.image = love.graphics.newImage("box.png")
I think to get this to work for 8.0 you would have to do something like:

Code: Select all

box = {}
box.body = love.physics.newBody(your_world,400, 300, "dynamic") --this is the physics body
box.shape = love.physics.newRectangleShape(0, 0, 50, 50, 0) --the shape is for handling collisions
box.fixture = love.physics.newFixture( box.body, box.shape)
box.shape:setData(your_data)
box.body:setMassFromShapes()
box.image = love.graphics.newImage("box.png")
i haven't used 8.0 yet, so if that's wrong hopefully someone can correct it.

then in your draw function do:

Code: Select all

local b = box --faster
love.graphics.draw(b.image, b.body:getX(), b.body:getY(), b.body:getAngle(),  1, 1, b.image:getWidth()/2, b.image:getHeight()/2)
good luck!
User avatar
tentus
Inner party member
Posts: 1060
Joined: Sun Oct 31, 2010 7:56 pm
Location: Appalachia
Contact:

Re: Attach an image to a body???

Post by tentus »

Here's a complete 0.8.0 example:

Code: Select all

function love.load()
	world = love.physics.newWorld(0, 0, true)
	box = {}
	box.image = love.graphics.newImage("image.jpg")
	box.body = love.physics.newBody(world, 200, 200, "dynamic")
	box.shape = love.physics.newRectangleShape(box.image:getWidth(), box.image:getHeight())
	box.fixture = love.physics.newFixture(box.body, box.shape)
end

function love.update(dt)
	world:update(dt)
end

function love.draw()
	love.graphics.draw(box.image, box.body:getX(), box.body:getY(), box.body:getAngle(),  1, 1, box.image:getWidth()/2, box.image:getHeight()/2)
end
But this does nothing, which is boring.

Thus, this version might be a little more visually helpful:

Code: Select all

function love.load()
	world = love.physics.newWorld(0, 200, true)
	box = {}
	box.image = love.graphics.newImage("image.jpg")
	box.body = love.physics.newBody(world, 200, 200, "dynamic")
	box.shape = love.physics.newRectangleShape(box.image:getWidth(), box.image:getHeight())
	box.fixture = love.physics.newFixture(box.body, box.shape)
	ball = {}
	ball.body = love.physics.newBody(world, 180, 300, "static")
	ball.shape = love.physics.newCircleShape(20)
	ball.fixture = love.physics.newFixture(ball.body, ball.shape)
end


function love.update(dt)
	world:update(dt)
end

function love.draw()
	love.graphics.draw(box.image, box.body:getX(), box.body:getY(), box.body:getAngle(),  1, 1, box.image:getWidth()/2, box.image:getHeight()/2)
	love.graphics.circle("fill", ball.body:getX(), ball.body:getY(), 20)
end
Kurosuke needs beta testers
Post Reply

Who is online

Users browsing this forum: No registered users and 107 guests