I've been working in a small program where the sun should illuminate plants to make then grow. I thought of using the raycasts to accomplish this, but when I was trying to use them they would miss shapes all the time, the problem was completely solved when I switched the raycasting to segment testing. I don't know if this is really a bug or me doing something stupid, but I couldn't figure why the raycasts wouldn't work.
Here's the code for the same thing using the two different methods:
RaycastOne:
Segment Testing:
Here's the code for the same thing using the two different methods:
RaycastOne:
Code:
for(int i=0; i<lightLength; i++)
{
light[i].p1 = b2Vec2 (i*1.0f - worldHalfSize, worldHalfSize);
light[i].p2 = b2Vec2 (i*1.0f - worldHalfSize, -worldHalfSize);
float lambda = 1;
b2Vec2 normal;
b2Shape* shape = world->RaycastOne(light[i],&lambda,&normal,true,NULL);
if(shape)
{
light[i].p2 = (1 - lambda) * light[i].p1 + lambda * light[i].p2;
}
}
{
light[i].p1 = b2Vec2 (i*1.0f - worldHalfSize, worldHalfSize);
light[i].p2 = b2Vec2 (i*1.0f - worldHalfSize, -worldHalfSize);
float lambda = 1;
b2Vec2 normal;
b2Shape* shape = world->RaycastOne(light[i],&lambda,&normal,true,NULL);
if(shape)
{
light[i].p2 = (1 - lambda) * light[i].p1 + lambda * light[i].p2;
}
}
Segment Testing:
Code:
for(int i=0; i<lightLength; i++)
{
light[i].p1 = b2Vec2 (i*1.0f - worldHalfSize, worldHalfSize);
light[i].p2 = b2Vec2 (i*1.0f - worldHalfSize, -worldHalfSize);
float lambda = 1;
b2Vec2 normal;
float menorLambda = 1;
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
for (b2Shape* s = b->GetShapeList(); s; s = s->GetNext())
{
bool hit = s->TestSegment( b->GetXForm(), &lambda, &normal, light[i], 1 );
if(lambda < menorLambda)
{
menorLambda = lambda;
}
}
}
light[i].p2 = (1 - menorLambda) * light[i].p1 + menorLambda * light[i].p2;
}
{
light[i].p1 = b2Vec2 (i*1.0f - worldHalfSize, worldHalfSize);
light[i].p2 = b2Vec2 (i*1.0f - worldHalfSize, -worldHalfSize);
float lambda = 1;
b2Vec2 normal;
float menorLambda = 1;
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
for (b2Shape* s = b->GetShapeList(); s; s = s->GetNext())
{
bool hit = s->TestSegment( b->GetXForm(), &lambda, &normal, light[i], 1 );
if(lambda < menorLambda)
{
menorLambda = lambda;
}
}
}
light[i].p2 = (1 - menorLambda) * light[i].p1 + menorLambda * light[i].p2;
}