Attach an image to a body???
-
- Prole
- Posts: 2
- Joined: Tue Apr 17, 2012 2:33 am
Attach an image to a body???
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 (:
Re: Attach an image to a body???
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.
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.
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)
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
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.
Code: Select all
function love.load()
guy = {
x = 0,
y = 0,
i = love.graphics.newImage("person.png")
end
end
Then in the love.draw you would put.
Code: Select all
love.graphics.draw(guy.i, guy.x, guy.y)
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
Hope that helps
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.
Re: Attach an image to a body???
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)
I think to get this to work for 8.0 you would have to do something like:
i haven't used 8.0 yet, so if that's wrong hopefully someone can correct it.
then in your draw function do:
good luck!
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")
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")
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)
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: Attach an image to a body???
Here's a complete 0.8.0 example:
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, 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
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
Who is online
Users browsing this forum: Bing [Bot] and 4 guests