I have taken the coding from the physics tutorial, and added to it as to make a shot shoot upwards. My only issue is that the shots will only fire from coordinates, not from the position of the ball. How do I Attach my shoot function to the physics body?
-----------------------------------
Code:
function love.load()
world = love.physics.newWorld(0, 0, 650, 650) --create a world for the bodies to exist in with width and height of 650
world:setGravity(0, 700) --the x component of the gravity will be 0, and the y component of the gravity will be 700
world:setMeter(64) --the height of a meter in this world will be 64px
objects = {} -- table to hold all our physical objects
--let's create the ground
objects.ground = {}
--we need to give the ground a mass of zero so that the ground wont move
objects.ground.body = love.physics.newBody(world, 650/2, 625, 0, 0) --remember, the body anchors from the center of the shape
objects.ground.shape = love.physics.newRectangleShape(objects.ground.body, 0, 0, 650, 50, 0) --anchor the shape to the body, and make it a width of 650 and a height of 50
--let's create a ball
objects.ball = {}
objects.ball.body = love.physics.newBody(world, 650/2, 650/2, 15, 0) --place the body in the center of the world, with a mass of 15
objects.ball.shape = love.physics.newCircleShape(objects.ball.body, 0, 0, 20) --the ball's shape has no offset from it's body and has a radius of 20
objects.ball.shots = {} -- holds shots
objects.ball.shots.x = 200 --coordinates of shots
objects.ball.shots.y = 200
--initial graphics setup
love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650
end
function love.update(dt)
world:update(dt) --this puts the world into motion
--here we are going to create some keyboard events
if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
objects.ball.body:applyForce(400, 0)
elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
objects.ball.body:applyForce(-400, 0)
elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
objects.ball.body:setY(650/2)
end
-- update the shots
for i,v in ipairs(objects.ball.shots) do
-- move them up up up
v.y = v.y - dt * 100
end
end
function love.draw()
love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground
love.graphics.polygon("fill", objects.ground.shape:getPoints()) -- draw a "filled in" polygon using the ground's coordinates
love.graphics.setColor(255,255,0) --set the drawing color to yellow for the ball
HappyFace = love.graphics.newImage("HappyFace.png")
local b = objects.ball.body
love.graphics.draw(HappyFace, b:getX(), b:getY(), b:getAngle(), 1, 1, HappyFace:getWidth()/2, HappyFace:getHeight()/2)
-- let's draw our balls shots
love.graphics.setColor(255,255,255,255)
for i,v in ipairs(objects.ball.shots) do
love.graphics.rectangle("fill", v.x, v.y, 2, 5)
end
end
function love.keyreleased(key)
if (key == " ") then
shoot()
end
end
function shoot()
local shot = {}
shot.x = objects.ball.shots.x
shot.y = objects.ball.shots.y
table.insert(objects.ball.shots, shot)
end
How do I Make an object shoot with physics?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
How do I Make an object shoot with physics?
Last edited by worldaway on Sun Sep 25, 2011 9:51 pm, edited 1 time in total.
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: How do I Make an object shoot with physics?
This probably belongs in the Support and Development forum, which has guidelines which will help you help us help you.
Anyhow, I imagine that Body:getPosition will help you out, from what you've described. A .love file would help tremendously.
Anyhow, I imagine that Body:getPosition will help you out, from what you've described. A .love file would help tremendously.
Kurosuke needs beta testers
Re: How do I Make an object shoot with physics?
Like tentus said, you need to replace objects.ball.shots.x and objects.ball.shots.y with coordinates from Body:getPosition.
Re: How do I Make an object shoot with physics?
I replacedthelinx wrote:Like tentus said, you need to replace objects.ball.shots.x and objects.ball.shots.y with coordinates from Body:getPosition.
objects.ball.shots.x = 200
objects.ball.shots.y = 200
with
objects.ball:getPosition()
still not working....
- tentus
- Inner party member
- Posts: 1060
- Joined: Sun Oct 31, 2010 7:56 pm
- Location: Appalachia
- Contact:
Re: How do I Make an object shoot with physics?
The way you had it set up was weird. This works for me (but I do not recommend it):
Also, newImage should be in love.load, not love.draw. Your game will crash and burn if you leave it in draw.
Code: Select all
function love.load()
world = love.physics.newWorld(0, 0, 650, 650) --create a world for the bodies to exist in with width and height of 650
world:setGravity(0, 700) --the x component of the gravity will be 0, and the y component of the gravity will be 700
world:setMeter(64) --the height of a meter in this world will be 64px
objects = {} -- table to hold all our physical objects
--let's create the ground
objects.ground = {}
--we need to give the ground a mass of zero so that the ground wont move
objects.ground.body = love.physics.newBody(world, 650/2, 625, 0, 0) --remember, the body anchors from the center of the shape
objects.ground.shape = love.physics.newRectangleShape(objects.ground.body, 0, 0, 650, 50, 0) --anchor the shape to the body, and make it a width of 650 and a height of 50
--let's create a ball
objects.ball = {}
objects.ball.body = love.physics.newBody(world, 650/2, 650/2, 15, 0) --place the body in the center of the world, with a mass of 15
objects.ball.shape = love.physics.newCircleShape(objects.ball.body, 0, 0, 20) --the ball's shape has no offset from it's body and has a radius of 20
objects.ball.shots = {} -- holds shots
--initial graphics setup
love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue
love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650
HappyFace = love.graphics.newImage("HappyFace.png")
end
function love.update(dt)
world:update(dt) --this puts the world into motion
--here we are going to create some keyboard events
if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right
objects.ball.body:applyForce(400, 0)
elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left
objects.ball.body:applyForce(-400, 0)
elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air
objects.ball.body:setY(650/2)
end
-- update the shots
for i,v in ipairs(objects.ball.shots) do
-- move them up up up
v.y = v.y - dt * 100
end
end
function love.draw()
love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground
love.graphics.polygon("fill", objects.ground.shape:getPoints()) -- draw a "filled in" polygon using the ground's coordinates
love.graphics.setColor(255,255,0) --set the drawing color to yellow for the ball
local b = objects.ball.body
love.graphics.draw(HappyFace, b:getX(), b:getY(), b:getAngle(), 1, 1, HappyFace:getWidth()/2, HappyFace:getHeight()/2)
-- let's draw our balls shots
love.graphics.setColor(255,255,255,255)
for i,v in ipairs(objects.ball.shots) do
love.graphics.rectangle("fill", v.x, v.y, 2, 5)
end
end
function love.keyreleased(key)
if key == " " then
shoot()
end
end
function shoot()
local shot = {}
shot.x, shot.y = objects.ball.body:getPosition()
table.insert(objects.ball.shots, shot)
end
Kurosuke needs beta testers
Re: How do I Make an object shoot with physics?
Thanks, That clears up a lot.tentus wrote:The way you had it set up was weird. This works for me (but I do not recommend it):
Also, newImage should be in love.load, not love.draw. Your game will crash and burn if you leave it in draw.Code: Select all
function love.load() world = love.physics.newWorld(0, 0, 650, 650) --create a world for the bodies to exist in with width and height of 650 world:setGravity(0, 700) --the x component of the gravity will be 0, and the y component of the gravity will be 700 world:setMeter(64) --the height of a meter in this world will be 64px objects = {} -- table to hold all our physical objects --let's create the ground objects.ground = {} --we need to give the ground a mass of zero so that the ground wont move objects.ground.body = love.physics.newBody(world, 650/2, 625, 0, 0) --remember, the body anchors from the center of the shape objects.ground.shape = love.physics.newRectangleShape(objects.ground.body, 0, 0, 650, 50, 0) --anchor the shape to the body, and make it a width of 650 and a height of 50 --let's create a ball objects.ball = {} objects.ball.body = love.physics.newBody(world, 650/2, 650/2, 15, 0) --place the body in the center of the world, with a mass of 15 objects.ball.shape = love.physics.newCircleShape(objects.ball.body, 0, 0, 20) --the ball's shape has no offset from it's body and has a radius of 20 objects.ball.shots = {} -- holds shots --initial graphics setup love.graphics.setBackgroundColor(104, 136, 248) --set the background color to a nice blue love.graphics.setMode(650, 650, false, true, 0) --set the window dimensions to 650 by 650 HappyFace = love.graphics.newImage("HappyFace.png") end function love.update(dt) world:update(dt) --this puts the world into motion --here we are going to create some keyboard events if love.keyboard.isDown("right") then --press the right arrow key to push the ball to the right objects.ball.body:applyForce(400, 0) elseif love.keyboard.isDown("left") then --press the left arrow key to push the ball to the left objects.ball.body:applyForce(-400, 0) elseif love.keyboard.isDown("up") then --press the up arrow key to set the ball in the air objects.ball.body:setY(650/2) end -- update the shots for i,v in ipairs(objects.ball.shots) do -- move them up up up v.y = v.y - dt * 100 end end function love.draw() love.graphics.setColor(72, 160, 14) -- set the drawing color to green for the ground love.graphics.polygon("fill", objects.ground.shape:getPoints()) -- draw a "filled in" polygon using the ground's coordinates love.graphics.setColor(255,255,0) --set the drawing color to yellow for the ball local b = objects.ball.body love.graphics.draw(HappyFace, b:getX(), b:getY(), b:getAngle(), 1, 1, HappyFace:getWidth()/2, HappyFace:getHeight()/2) -- let's draw our balls shots love.graphics.setColor(255,255,255,255) for i,v in ipairs(objects.ball.shots) do love.graphics.rectangle("fill", v.x, v.y, 2, 5) end end function love.keyreleased(key) if key == " " then shoot() end end function shoot() local shot = {} shot.x, shot.y = objects.ball.body:getPosition() table.insert(objects.ball.shots, shot) end
Who is online
Users browsing this forum: Bing [Bot] and 205 guests