当前位置: 首页>>代码示例>>C++>>正文


C++ b2World::RayCast方法代码示例

本文整理汇总了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);
		}
	}

}
开发者ID:rachelnertia,项目名称:Box2D-Raycasting-Test,代码行数:46,代码来源:main.cpp


注:本文中的b2World::RayCast方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。