本文整理汇总了C++中b2World::RayCast方法的典型用法代码示例。如果您正苦于以下问题:C++ b2World::RayCast方法的具体用法?C++ b2World::RayCast怎么用?C++ b2World::RayCast使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类b2World
的用法示例。
在下文中一共展示了b2World::RayCast方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: RaycastRender
void RaycastRender(b2World& world, sf::RenderTarget& target, Camera& camera)
{
const float ray_length = 15.0f; // How far rays will travel before they will stop.
const float view_angle = (3.14f * (0.25f * angle_modifier));
const b2Vec2 view_plane(-camera.fwd.y * angle_modifier, camera.fwd.x * angle_modifier);
const b2Vec2 raystart = camera.pos;
// Cast a ray for each horizontal pixel.
for (unsigned i = 0; i < target.getSize().x; ++i) {
// Determine the direction the ray should go in...
// [-1, 1] How far across the screen from left to right the current ray is.
float screenx = -1.0f + (2.0f * (i / (float)target.getSize().x));
// There are 2 ways to calculate the ray's direction.
b2Vec2 raydir = raydir_mode_toggle ?
// 1: Scale the view plane vector by screenx and add it to the camera's forward vector.
(camera.fwd) + (screenx * view_plane) :
// 2: Rotate the camera's forward vector by the viewing angle scaled by screenx.
RotateVec(camera.fwd, view_angle * screenx);
// Determine the end point of the ray in world space.
b2Vec2 rayend = camera.pos + ray_length * raydir;
RayCastCallback callback;
world.RayCast(&callback, raystart, rayend); // Cast the ray!
if (callback.m_fixture) { // If the ray hit something...
b2Vec2 ray = (callback.m_point - raystart);
// Use either the 1) actual distance or 2) perpendicular distance from the camera to the
// ray hit point.
float distance = distance_mode_toggle ? ray.Length() : DotProduct(ray, camera.fwd);
// Use this distance to figure out how tall a line to draw.
int line_height = abs(int(target.getSize().y / distance));
// Make far-away lines darker.
sf::Uint8 f = sf::Uint8((1.f - (distance / ray_length)) * 255.0f);
sf::Color c(f, f, f);
sf::Vertex line[2] =
{
sf::Vertex(sf::Vector2f(float(i+1), (float)(target.getSize().y / 2) - (line_height / 2)), c),
sf::Vertex(sf::Vector2f(float(i+1), (float)(target.getSize().y / 2) + (line_height / 2)), c)
};
target.draw(line, 2, sf::PrimitiveType::Lines);
}
}
}