I am trying to implement raycasting but it gives me an error asoon as it begins iterating through the shapes I am not even sure if I am doing it right.
Code:
void PlayState::CreateLaser(b2Vec2 position, float rotation, b2Vec2 direction, int ID)
{
b2Segment segment;
segment.p1 = position;
b2Vec2 endPoint;
endPoint.x = position.x + direction.x*10;
endPoint.y = position.y + direction.y*10;
segment.p2 = endPoint;
SpriteInstance *spriteInstance = new SpriteInstance(game->app->GetSprite("explosion"), position, rotation);
Object* obj = new ProjectileLaser(spriteInstance, NULL, 1, ID);
b2Shape *shapeArray;
int shapes = world->Raycast(segment, &shapeArray, 20, false, NULL);
for(int x = 0; x < shapes; x++){
((Object*)shapeArray[1].GetBody()->GetUserData())->HandleCollision(this, *obj, 0);
}
}
{
b2Segment segment;
segment.p1 = position;
b2Vec2 endPoint;
endPoint.x = position.x + direction.x*10;
endPoint.y = position.y + direction.y*10;
segment.p2 = endPoint;
SpriteInstance *spriteInstance = new SpriteInstance(game->app->GetSprite("explosion"), position, rotation);
Object* obj = new ProjectileLaser(spriteInstance, NULL, 1, ID);
b2Shape *shapeArray;
int shapes = world->Raycast(segment, &shapeArray, 20, false, NULL);
for(int x = 0; x < shapes; x++){
((Object*)shapeArray[1].GetBody()->GetUserData())->HandleCollision(this, *obj, 0);
}
}