Is there a way to see if a body is on the ground?
And, is there a way to see if a bullet or something has collided with it?
love.physics (isOnGround)
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: love.physics (isOnGround)
It's a bit complicated. The standard way is adding a collision callback to your physics world. That can be done with World:setCallbacks. You will also have to add some data to the objects that are involved on the collisions.
Give a look at the PhysicsCollisionTutorial
Give a look at the PhysicsCollisionTutorial
When I write def I mean function.
Re: love.physics (isOnGround)
Code: Select all
function fCon(a,b,coll)
end
function con(a,b,coll)
player.onGround = true
end
function lCon(a,b,coll)
end
function result(a,b,coll)
end
function love.load()
world = love.physics.newWorld(0,0,650,650)
world:setCallbacks(fCon,con,lCon,result)
world:setGravity(0,700)
world:setMeter(64)
bodies = {}
shapes = {}
bodies[0] = love.physics.newBody(world,650/2,625,0,0)
shapes[0] = love.physics.newRectangleShape(bodies[0],0,0,650,50,0)
bodies[1]= love.physics.newBody(world,650/2,650/2,15,0)
shapes[1] = love.physics.newCircleShape(bodies[1],0,0,20)
love.graphics.setBackgroundColor(104, 136, 248)
love.graphics.setMode(650, 650, false, true, 0)
player = {bodies[1],onGround=false}
end
function love.update(dt)
world:update(dt)
if love.keyboard.isDown("right") then
bodies[1]:applyForce(400, 0)
elseif love.keyboard.isDown("left") then
bodies[1]:applyForce(-400, 0)
end
if love.keyboard.isDown("up") and player.onGround == true then
bodies[1]:applyImpulse(0,-50,0,0)
player.onGround = false
end
if love.mouse.isDown("l") then
table.insert(bodies,love.physics.newBody(world,love.mouse.getX(),love.mouse.getY(),0,0))
local x = #bodies
table.insert(shapes,love.physics.newRectangleShape(bodies[x],0,0,100,20,0))
end
if love.mouse.isDown("r") then
table.insert(bodies,love.physics.newBody(world,bodies[1]:getX(),bodies[1]:getY(),0,0))
local x = #bodies
table.insert(shapes,love.physics.newCircleShape(bodies[x],0,0,3))
bodies[x]:setLinearVelocity(love.mouse.getX(),love.mouse.getY())
end
end
function love.draw()
local x1, y1, x2, y2, x3, y3, x4, y4 = shapes[0]:getBoundingBox()
local boxwidth = x3 - x2
local boxheight = y2 - y1
love.graphics.setColor(72, 160, 14)
love.graphics.rectangle("fill", bodies[0]:getX() - boxwidth/2, bodies[0]:getY() - boxheight/2, boxwidth, boxheight)
love.graphics.setColor(193, 47, 14)
love.graphics.circle("fill", bodies[1]:getX(), bodies[1]:getY(), shapes[1]:getRadius(), 20)
local i =2
while bodies[i] and shapes[i] do
local x = shapes[i]:getType()
if x == "polygon" then
local x1, y1, x2, y2, x3, y3, x4, y4 = shapes[i]:getBoundingBox()
local boxwidth = x3 - x2
local boxheight = y2 - y1
love.graphics.setColor(72, 160, 14)
love.graphics.rectangle("fill", bodies[i]:getX() - boxwidth/2, bodies[i]:getY() - boxheight/2, boxwidth, boxheight)
i=i+1
end
if x == "circle" then
love.graphics.setColor(193, 47, 14)
love.graphics.circle("fill", bodies[i]:getX(), bodies[i]:getY(), shapes[i]:getRadius(), 20)
end
end
end
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: love.physics (isOnGround)
Compare the x and y of the corresponding bodies?Raylin wrote:how would I make the player recognize which side of the block it is colliding with?
Help us help you: attach a .love.
Who is online
Users browsing this forum: No registered users and 7 guests