Hi,
I’m using box2d for the physics in my game.
I use ray cast to simulate lasers in the game, but the ray casts are inaccurate and I cant tell why.
see photo’s
the method:
b2RayCastInput input;
input.p1 = b2Vec2(beginPoint.x /SCALE_RATIO, beginPoint.y /SCALE_RATIO);
input.p2 = b2Vec2(endPoint.x /SCALE_RATIO, endPoint.y /SCALE_RATIO);
input.maxFraction = 1.0;
//check every fixture of every body to find closest
float closestFraction = 1.0; //start with end of line as p2
b2Vec2 intersectionNormal(0,0);
for (GameObjet *object : gameObjectsArray)
{
if (object->box2DBody != NULL && object !=this)
{
for (b2Fixture* f = object->box2DBody->GetFixtureList(); f; f = f->GetNext())
{
b2RayCastOutput output;
if ( ! f->RayCast( &output, input,0 ) )
continue;
if ( output.fraction < closestFraction )
{
closestFraction = output.fraction;
intersectionNormal = output.normal;
}
}
}
}
b2Vec2 intersectionPoint = b2Vec2(beginPoint.x , beginPoint.y ) + closestFraction * ( b2Vec2(endPoint.x , endPoint.y ) - b2Vec2(beginPoint.x , beginPoint.y ) );
drawNode->clear();
Color3B clr = Color3B::MAGENTA;
Color4F clrb = Color4F(clr);
drawNode->drawLine(Vec2(beginPoint.x , beginPoint.y),Vec2(intersectionPoint.x,intersectionPoint.y),clrb);
drawNode->drawDot(Vec2(intersectionPoint.x,intersectionPoint.y), 1.1, clrb);
dot->setPosition(Vec2(intersectionPoint.x,intersectionPoint.y));
I follow the function on http://www.iforce2d.net/b2dtut/raycasting
Hope someone has used raycast in box2d coco2d x that can help.
Regards
/////update///
the raycast works fine but converting the point to cocos2d x coordinates doesn’t
the white lines are from m_debugDraw->DrawSegment
m_debugDraw->DrawSegment(b2Vec2(innerLaser->beginPoint.x /SCALE_RATIO ,innerLaser->beginPoint.y /SCALE_RATIO ), b2Vec2(callback.m_point.x ,callback.m_point.y ) , b2Color(0.8f, 0.8f, 0.8f));
the purple dots are from drawNode->drawDot
drawNode->drawDot(Vec2( (callback.m_point.x *SCALE_RATIO) , (callback.m_point.y * SCALE_RATIO)), 2.1, clrb);
how is the debug draw correct and the draw dot incorrect?