love.physics (isOnGround)

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
Raylin
Prole
Posts: 30
Joined: Thu Dec 30, 2010 8:48 am
Location: Chicago
Contact:

love.physics (isOnGround)

Post by Raylin »

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?
User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: love.physics (isOnGround)

Post by kikito »

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
When I write def I mean function.
User avatar
Raylin
Prole
Posts: 30
Joined: Thu Dec 30, 2010 8:48 am
Location: Chicago
Contact:

Re: love.physics (isOnGround)

Post by Raylin »

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
This works but, how would I make the player recognize which side of the block it is colliding with?
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: love.physics (isOnGround)

Post by Robin »

Raylin wrote:how would I make the player recognize which side of the block it is colliding with?
Compare the x and y of the corresponding bodies?
Help us help you: attach a .love.
Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests