Collision Polygon

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
childonline
Prole
Posts: 15
Joined: Thu Dec 30, 2010 11:06 am

Collision Polygon

Post by childonline »

So I made this little jelly-like square by joining 4 circle shaped bodies with distance joints and it worked out pretty ok.
Problem is, with this type of implementation collision detection occurs only for the corners of the square and not with the edges.

Is there a way to create a bounding polygon having the xy coordinates of the 4 bodies as vertices, such that if another body touches a jelly square edge the pivots react and compress?

Here is the code so far, it started out from the love.physics tutorial (arrow keys to move square around):

Code: Select all

function love.load()
	world = love.physics.newWorld(-650, -650, 650, 650)
	world:setGravity(0,700)
	world:setMeter(64)
  
	bodies = {}
	shapes = {}
	joints = {}
  
	bodies[0] = love.physics.newBody(world, 625/2, 625, 0, 0)
	shapes[0] = love.physics.newRectangleShape(bodies[0], 0, 0, 800, 50, 0)
  
	bodies[1] = love.physics.newBody(world, 100, 100, 10, 0)  
	bodies[2] = love.physics.newBody(world, 200, 100, 10, 0)
	bodies[3] = love.physics.newBody(world, 200, 200, 10, 0)
	bodies[4] = love.physics.newBody(world, 100, 200, 10, 0)
	
	for i = 1,4 do
		shapes[i] = love.physics.newCircleShape(bodies[i], 0, 0, .1)
	end
	
	joints[1] = love.physics.newDistanceJoint(bodies[1], bodies[2],100,100,200,100)
	joints[2] = love.physics.newDistanceJoint(bodies[2], bodies[3],200,100,200,200)
	joints[3] = love.physics.newDistanceJoint(bodies[3], bodies[4],200,200,100,200)
	joints[4] = love.physics.newDistanceJoint(bodies[4], bodies[1],100,200,100,100)
	joints[5] = love.physics.newDistanceJoint(bodies[1], bodies[3],100,100,200,200)
	joints[6] = love.physics.newDistanceJoint(bodies[2], bodies[4],200,100,100,200)
	
	for i = 1,6 do
		joints[i]:setFrequency(4)  
		joints[i]:setDamping(0.75)
	end
	
  	love.graphics.setBackgroundColor(104, 136, 248)
	love.graphics.setMode(650, 650, false, true, 0)
  
end

function love.update(dt)
	world:update(dt)
	for i = 1,4 do
		if love.keyboard.isDown("right") then
			bodies[i]:applyForce(300, 0)
		elseif love.keyboard.isDown("left") then
			bodies[i]:applyForce(-300, 0)
		elseif love.keyboard.isDown("up") then
			bodies[i]:applyForce(0,-300)
		end
	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.polygon('fill', bodies[1]:getX(), bodies[1]:getY(),bodies[2]:getX(), bodies[2]:getY(),bodies[3]:getX(), bodies[3]:getY(),bodies[4]:getX(), bodies[4]:getY())
end
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: Collision Polygon

Post by Taehl »

Instead of four circles for the corners, why not use four rectangles for the edges? Just set them to not collide with each other, and it should work quite well.
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Post Reply

Who is online

Users browsing this forum: No registered users and 63 guests