Collision Problem

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.
User avatar
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Collision Problem

Post by Jack5500 »

I have this rotating object

Code: Select all

objects.enemy.two = {}
objects.enemy.two.body = love.physics.newBody(world, center_x+distance, center_y+distance, 1, 1) --non zero mass so it can move freely
objects.enemy.two.shape = love.physics.newRectangleShape (objects.enemy.two.body, 0, 0,32,32,0) 
objects.enemy.two.joint = love.physics.newRevoluteJoint( anchor, objects.enemy.two.body, center_x, center_y )
objects.enemy.two.joint:setMotorSpeed( -1 ) --set the motor speed
objects.enemy.two.joint:setMaxMotorTorque( 10000 ) --something ludicrously high, doesn't really matter
objects.enemy.two.joint:setMotorEnabled( true ) --turn the motor on
&

Code: Select all

love.graphics.circle('line', center_x, center_y, 32, 32)
local xenemy,yenemy = objects.enemy.two.body:getWorldPoint(distance,0)
love.graphics.rectangle("fill",xenemy,yenemy, 32, 32)
and I want it to end the game if it collides with the player (w = 20 , h = 70)

Code: Select all

function Play:update(dt)

world:update(dt)


--Calculate all other points
	xbotright,ybotright = objects.player1.body:getWorldPoint(20,70)	

	xbotleft = xbotright -20
	ybotleft = ybotright
	
	xtopleft = xbotleft
	ytopleft = ybotleft - 70
	
	xtopright = xbotright
	ytopright = ytopleft


	  
if objects.enemy.two.shape:testPoint (love.mouse.getX(),love.mouse.getY()) then
	dead_show = "true"
end
	 
if objects.enemy.two.shape:testPoint(xbotright,ybotright)then
		player1_dead = "true"
end	

if objects.enemy.two.shape:testPoint(xbotleft,ybotleft)then
 		player1_dead = "true"
end	

if objects.enemy.two.shape:testPoint(xtopright,ytopright) then
		player1_dead = "true"
end	

if objects.enemy.two.shape:testPoint(xtopleft,ytopleft)then
		player1_dead = "true"
end	
But instead of the wanted outcome I get this:

http://d.pr/PXf
(Ignore the black lines :) )

I have no idea why, or what could have caused it.
For the case that the above code is correct and the problem must lay in the rest of the code I'm going to attach it.
http://d.pr/Kie4
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Collision Problem

Post by The Burrito »

Your collision stuff looks fine, it's the drawing thats off.

Try drawing the "enemy" like this:

Code: Select all

love.graphics.polygon("fill",objects.enemy.two.shape:getPoints())
That will show you exactly what the physics shape looks like. I think your draw code was carried over from your previous problem, which is why it was off.

As a side note the reason your mouse death checker doesn't work is it needs to account for the camera offset, and reset itself:

Code: Select all

if objects.enemy.two.shape:testPoint (love.mouse.getX() + camera._x,love.mouse.getY() + camera._y) then
	dead_show = "true"
else
	dead_show = "false"
end
User avatar
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: Collision Problem

Post by Jack5500 »

Okay, the problem is that the body itself has kind of it's own rotation which makes it act different from the shape. What results in a different result as wanted. The shape has no rotation at all, but how can I set the body to be as "static" as the shape.

Live Example:
qY9QM.gif
qY9QM.gif (1.98 MiB) Viewed 306 times
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Collision Problem

Post by The Burrito »

It has a fixed orientation relative to the axle. if it's going to be a ball and insta kill the character the rotation won't matter (you can leave the rotation out of the sprite rendering).

Most of the methods I can think of to keep the shape axis aligned would be kind of hackish, but I suppose the best way would be to use another joint and make the shape attached to a third body (Think of it like an axle, a wheel, and then another pivot on the wheel like a piston).
User avatar
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: Collision Problem

Post by Jack5500 »

So maybe I should leave the shape out at all and just parent the image to the body, I guess. Is that possible?
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Collision Problem

Post by The Burrito »

Sure if you want, but then you'll have to write your own collision stuff for it, the extra joint thing is how I would do it if I were making a platform to stand on, if it's something thats going to kill you and there aren't any other objects for it to push around then playing nice with the physics engine isn't as important.
User avatar
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: Collision Problem

Post by Jack5500 »

Where would the third axis have to be located?
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Collision Problem

Post by The Burrito »

It would be wherever you want the "enemy" to pivot around relative to the wheel body, so basically the center of the shape, you could also think of it like a Ferris Wheel, you need a base, a wheel, and then a box thats free to rotate so it stays right side up.
User avatar
Jack5500
Party member
Posts: 149
Joined: Wed Dec 07, 2011 8:38 pm
Location: Hamburg, Germany

Re: Collision Problem

Post by Jack5500 »

But for that i would need to create a distance joint between the body and the shape of enemy.two !?
User avatar
The Burrito
Party member
Posts: 153
Joined: Mon Sep 21, 2009 12:14 am
Contact:

Re: Collision Problem

Post by The Burrito »

Yeah I guess, it's pretty simple, you have an anchor body, a joint connecting that to the wheel body (what you have right now).
Then instead of connecting a shape to the second body, make a third body and attach the shape to it. Then attach that to the wheel body.

So in the end you have body1 to body2 with RevoluteJoint body 2 to body 3 with DistanceJoint, and a shape on body3.
Post Reply

Who is online

Users browsing this forum: No registered users and 14 guests