Bug with testSegment ?

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
Damnae
Prole
Posts: 12
Joined: Sun Oct 05, 2008 12:33 pm

Bug with testSegment ?

Post by Damnae »

Hello,

I might have found a bug with testSegment:
When the segment is perpendicular to a segment of the shape tested,
it seems that it tests that segment of the polygon as if it was a infinite line (instead of a segment of that line).

Edit: I added a .love showing the bug
Attachments
testSegmentBug.love
(996 Bytes) Downloaded 252 times
User avatar
Damnae
Prole
Posts: 12
Joined: Sun Oct 05, 2008 12:33 pm

Re: Bug with testSegment ?

Post by Damnae »

It's not clear enough or nobody cares ?
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Bug with testSegment ?

Post by rude »

Sorry, I saw this, then completely forgot about it.

I've tried looking at the LOVE source and Box2D source, but I can't find any obvious errors. Here's the LOVE wrapper code, maybe someone else can spot an error.

Code: Select all

	int Shape::testSegment(lua_State * L)
	{
		love::luax_assert_argc(L, 4, 4);

		b2Segment s;

		s.p1.x = (float)lua_tonumber(L, 1);
		s.p1.y = (float)lua_tonumber(L, 2);
		s.p2.x = (float)lua_tonumber(L, 3);
		s.p2.y = (float)lua_tonumber(L, 4);

		float lambda;
		b2Vec2 normal;
		
		if(shape->TestSegment(shape->GetBody()->GetXForm(), &lambda, &normal, s, 1.0f))
		{
			lua_pushnumber(L, lambda);
			lua_pushnumber(L, normal.x);
			lua_pushnumber(L, normal.y);
			return 3;
		}

		return 0;
	}
Thanks for pointing this out, btw. I'll have to ask at the Box2D forums when I get the time.
User avatar
Damnae
Prole
Posts: 12
Joined: Sun Oct 05, 2008 12:33 pm

Re: Bug with testSegment ?

Post by Damnae »

Thanks for the reply =)

I'm trying to convert it to c++ with the testbed of Box2d to see if there is the same problem.
(I use the version 2.0.1 of Box2d, I think that LOVE use the same.)
User avatar
Damnae
Prole
Posts: 12
Joined: Sun Oct 05, 2008 12:33 pm

Re: Bug with testSegment ?

Post by Damnae »

So, the bug seems to come from Box2d, I found the same problem with this code:

Code: Select all

#ifndef TEST_SEGMENT_H
#define TEST_SEGMENT_H

class TestSegment : public Test
{
public:

	b2Body*		m_body;
	b2Vec2		m_mousePos;

	TestSegment()
	{
		{ 
			b2PolygonDef shape;
			shape.SetAsBox(5.0f, 1.0f);
			b2BodyDef body;
			body.position.Set(0.0f, 20.0f);

			m_body = m_world->CreateBody(&body);
			m_body->CreateShape(&shape);
		}
	}

	void MouseMove(const b2Vec2& p)
	{
		Test::MouseMove(p);
		m_mousePos = p;
	}

	void Step(Settings* settings)
	{
		Test::Step(settings);

		float lambda;
		b2Vec2 normal;
		b2Shape* shape = m_body->GetShapeList();
		b2Segment greenSegment, redSegment, blueSegment;
		greenSegment.p1 = b2Vec2(10, 10);
		greenSegment.p2 = m_mousePos;
		redSegment.p1 = b2Vec2(m_mousePos.x, 10);
		redSegment.p2 = m_mousePos;
		blueSegment.p1 = b2Vec2(10, m_mousePos.y);
		blueSegment.p2 = m_mousePos;

		if(shape->TestSegment(shape->GetBody()->GetXForm(), &lambda, &normal, greenSegment, 1.0f)) {
			DrawSegment(greenSegment.p1, greenSegment.p1 + lambda * (greenSegment.p2 - greenSegment.p1), b2Color(0, 255, 0));
		} else {
			DrawSegment(greenSegment.p1, greenSegment.p2, b2Color(0, 255, 0));
		}

		if(shape->TestSegment(shape->GetBody()->GetXForm(), &lambda, &normal, redSegment, 1.0f)) {
			DrawSegment(redSegment.p1, redSegment.p1 + lambda * (redSegment.p2 - redSegment.p1), b2Color(255, 0, 0));
		} else {
			DrawSegment(redSegment.p1, redSegment.p2, b2Color(255, 0, 0));
		}

		if(shape->TestSegment(shape->GetBody()->GetXForm(), &lambda, &normal, blueSegment, 1.0f)) {
			DrawSegment(blueSegment.p1, blueSegment.p1 + lambda * (blueSegment.p2 - blueSegment.p1), b2Color(0, 0, 255));
		} else {
			DrawSegment(blueSegment.p1, blueSegment.p2, b2Color(0, 0, 255));
		}

	}

	static Test* Create()
	{
		return new TestSegment;
	}

};

#endif
(Note: I had to add "virtual" to the line 144 of Test.h "void MouseMove(const b2Vec2& p);" so I can get the mouse position when the mouse is down,
add #include "TestSegment.h" and {"TestSegment", TestSegment::Create}, in TestEntries.cpp)

The red and blue line shouldn't stop because they aren't colliding with the shape:
testSegment.png
testSegment.png (4.49 KiB) Viewed 6744 times
User avatar
rude
Administrator
Posts: 1052
Joined: Mon Feb 04, 2008 3:58 pm
Location: Oslo, Norway

Re: Bug with testSegment ?

Post by rude »

Nice work ... we'll have to ask Box2D author Erin Catto about it then.
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests