Hi, Ive got a MASSIVE PROBLEM when raycasting.
I have a ray, from a point (-75,0) in my world. Now, I have the ray at an angle of 90 degrees, however, it is NOT pointing at 90 degrees.
here is my code
for the Actual Raycast (I have my own function for raycasts, so that I can easily call them)
and for calling one. (the variable raycastAngle has already been defined as 90)
see attached screenshot for the problem
I have a ray, from a point (-75,0) in my world. Now, I have the ray at an angle of 90 degrees, however, it is NOT pointing at 90 degrees.
here is my code
for the Actual Raycast (I have my own function for raycasts, so that I can easily call them)
Code:
void SimpleRayCast(b2World* WORLD, b2Vec2 START, float DAngle,float Length)
{
float32 segmentLength = Length;
DAngle = -DAngle*degrees;
b2Vec2 ENDPOINT(START.x,START.y+Length);
b2Vec2 C1( -sin(DAngle),cos(DAngle)); b2Vec2 C2(cos(DAngle),sin(DAngle));
b2Mat22 ROTMATRIX(C2,C1);
ENDPOINT = b2Mul(ROTMATRIX,ENDPOINT);
b2Segment segment;
segment.p1 = START;
b2Vec2 END = ENDPOINT;
segment.p2 = END;
float32 lambda=1;
b2Vec2 normal;
b2Fixture* fixture = WORLD->RaycastOne(segment,&lambda,&normal,false,NULL);
b2Color laserColor(255,0,0);
if(fixture)
{
b2Vec2 p1 = segment.p1;
b2Vec2 p2 = (1-lambda)*segment.p1+lambda*segment.p2;
glBegin(GL_LINES);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glEnd();
b2Body* BODY = fixture->GetBody();
if(BODY->GetMass()!=0.0)
{
b2Vec2 FORCE((p1.x-p2.x)/-1,(p1.y-p2.y)/-1);
BODY->ApplyForce(FORCE, p2);
}
}
else
{
b2Vec2 p1 = segment.p1;
b2Vec2 p2 = segment.p2;
glBegin(GL_LINES);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glEnd(); } }
DAngle = -DAngle*degrees;
b2Vec2 ENDPOINT(START.x,START.y+Length);
b2Vec2 C1( -sin(DAngle),cos(DAngle)); b2Vec2 C2(cos(DAngle),sin(DAngle));
b2Mat22 ROTMATRIX(C2,C1);
ENDPOINT = b2Mul(ROTMATRIX,ENDPOINT);
b2Segment segment;
segment.p1 = START;
b2Vec2 END = ENDPOINT;
segment.p2 = END;
float32 lambda=1;
b2Vec2 normal;
b2Fixture* fixture = WORLD->RaycastOne(segment,&lambda,&normal,false,NULL);
b2Color laserColor(255,0,0);
if(fixture)
{
b2Vec2 p1 = segment.p1;
b2Vec2 p2 = (1-lambda)*segment.p1+lambda*segment.p2;
glBegin(GL_LINES);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glEnd();
b2Body* BODY = fixture->GetBody();
if(BODY->GetMass()!=0.0)
{
b2Vec2 FORCE((p1.x-p2.x)/-1,(p1.y-p2.y)/-1);
BODY->ApplyForce(FORCE, p2);
}
}
else
{
b2Vec2 p1 = segment.p1;
b2Vec2 p2 = segment.p2;
glBegin(GL_LINES);
glVertex2f(p1.x, p1.y);
glVertex2f(p2.x, p2.y);
glEnd(); } }
and for calling one. (the variable raycastAngle has already been defined as 90)
Code:
b2Vec2 START(-75,0);
SimpleRayCast(WORLD,START,raycastAngle,300);
see attached screenshot for the problem