动态锥形状根据障碍物而变化

编程入门 行业动态 更新时间:2024-10-27 23:20:49
动态锥形状根据障碍物而变化 - 参考第三眼犯罪应用程序(dynamic cone shape changes depending on obstacles - reference to Third Eye Crime app)

我看过“第三眼犯罪”的预告片。

你怎么能实现蓝色视锥的形状,使它的形状根据障碍物而变化?

我的尝试是投射光线直到出现障碍物然后我采取光线的终点来绘制圆锥形状。

我的方法的问题是锥体的精度取决于光线的数量。 除了投射的光线越多,性能越差。

在这里你可以看到光线:

在这里,您可以看到用光线的终点绘制的圆锥形状:

有更好的解决方案吗?

I've seen the Trailer of "Third Eye Crime".

How can you realise the blue field of view cone so that it's shape changes depending on obstacles?

My attempt was to cast rays till an obstacle occurs and then I take the end points of the rays to draw the cone shape.

The problem with my method is that the precision of the cone depends on the number of rays. Besides the more rays are casted the worse the performance.

Here you can see the rays:

Here you can see the cone shape drawn with the end points of the rays:

Are there better solutions?

最满意答案

你的问题一直困扰着我,但我无法找到一个完全正常的答案。 我的工作笔记太多了,我不能把它写成评论,所以我将其作为答案添加。

我的第一个问题是测试视野线和任何障碍物之间的接触。 不幸的是,SpriteKit只有contactBitMask,它可以完成这项工作并提供接触坐标。

我查看了SKNode的intersectsNode:但它的返回值只是一个BOOL,我们需要坐标。

我还看了Apple的Sprite Kit Programming Guide的Searching for Physics Bodies一节。 这涉及视线,障碍物等......这里使用的命令是bodyAlongRayStart:end: . 返回是它与之相交的第一个物理体,但没有提供实际接触点的坐标。

我最终得到的代码首先描绘了完整的视线锥。 接下来,任何与对象联系的线路都会使用contact.contactPoint获取联系点,然后删除有问题的线路。 到现在为止还挺好。 但是我试图将删除的线条绘制到新的终点(接触点)时遇到了麻烦。 由于一些莫名其妙的原因,只有一些被删除的行被重绘而不是全部。

请记住,这是粗糙的代码所以拍打它,拉它并将它扔在墙上几次。 例如,你并不需要数组。 我希望这会引导你朝着正确的方向前进,或者你可以发现一些我无法看到的东西。

旁注:我在模拟器iPhone(4英寸)横向上运行它。

#import "MyScene.h" typedef NS_OPTIONS(uint32_t, CNPhysicsCategory) { Category1 = 1 << 0, Category2 = 1 << 1, Category3 = 1 << 2, }; @interface MyScene()<SKPhysicsContactDelegate> @end @implementation MyScene { SKSpriteNode *player; SKSpriteNode *obstacle1; SKSpriteNode *obstacle2; SKSpriteNode *obstacle3; SKSpriteNode *obstacle4; NSMutableArray *beamArray; int beamCounter; } -(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { self.physicsWorld.contactDelegate = self; beamArray = [[NSMutableArray alloc] init]; beamCounter = 0; player = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(20, 20)]; player.position = CGPointMake(100, 150); [self addChild:player]; obstacle1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(200, 20)]; obstacle1.name = @"obstacle1"; obstacle1.position = CGPointMake(250, 100); obstacle1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle1.size]; obstacle1.physicsBody.affectedByGravity = NO; obstacle1.physicsBody.categoryBitMask = Category2; obstacle1.physicsBody.collisionBitMask = 0x00000000; [self addChild:obstacle1]; obstacle2 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(40, 40)]; obstacle2.name = @"obstacle2"; obstacle2.position = CGPointMake(400, 200); obstacle2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle2.size]; obstacle2.physicsBody.affectedByGravity = NO; obstacle2.physicsBody.categoryBitMask = Category2; obstacle2.physicsBody.collisionBitMask = 0x00000000; [self addChild:obstacle2]; obstacle3 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(50, 20)]; obstacle3.name = @"obstacle3"; obstacle3.position = CGPointMake(530, 130); obstacle3.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:obstacle3.size]; obstacle3.physicsBody.affectedByGravity = NO; obstacle3.physicsBody.categoryBitMask = Category2; obstacle3.physicsBody.collisionBitMask = 0x00000000; [self addChild:obstacle3]; for (int y = 0; y <= 320; y++) { SKShapeNode *beam1 = [SKShapeNode node]; beam1.name = [NSString stringWithFormat:@"%i",beamCounter++]; CGMutablePathRef pathToDraw = CGPathCreateMutable(); CGPathMoveToPoint(pathToDraw, NULL, player.position.x, player.position.y); CGPathAddLineToPoint(pathToDraw, NULL, 600, y); beam1.path = pathToDraw; [beam1 setStrokeColor:[UIColor yellowColor]]; [beam1 setLineWidth:2.0]; beam1.physicsBody = [SKPhysicsBody bodyWithEdgeFromPoint:CGPointMake(player.position.x, player.position.y) toPoint:CGPointMake(600, y)]; beam1.physicsBody.affectedByGravity = NO; beam1.physicsBody.categoryBitMask = Category1; beam1.physicsBody.contactTestBitMask = Category2; [self addChild:beam1]; [beamArray addObject:beam1]; } } return self; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { // } -(void)update:(CFTimeInterval)currentTime { // } - (void)didBeginContact:(SKPhysicsContact *)contact { NSLog(@"bodyA:%@ bodyB:%@",contact.bodyA.node.name, contact.bodyB.node.name); uint32_t collision = (contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask); if (collision == (Category1 | Category2)) { CGPoint newEndPoint = contact.contactPoint; [beamArray removeObject:contact.bodyA.node.name]; [contact.bodyA.node removeFromParent]; SKShapeNode *beam1 = [SKShapeNode node]; beam1.name = [NSString stringWithFormat:@"%i",beamCounter++]; CGMutablePathRef pathToDraw = CGPathCreateMutable(); CGPathMoveToPoint(pathToDraw, NULL, player.position.x, player.position.y); CGPathAddLineToPoint(pathToDraw, NULL, newEndPoint.x, newEndPoint.y); beam1.path = pathToDraw; [beam1 setStrokeColor:[UIColor greenColor]]; [beam1 setLineWidth:2.0]; [self addChild:beam1]; } } @end

去掉触摸线:

在此处输入图像描述

移除接触线并更换已移除的线:

在此处输入图像描述

Here I've found several answers:

https://code.google.com/p/straightedge/

http://www.redblobgames.com/articles/visibility/

更多推荐

本文发布于:2023-07-05 00:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1031219.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:锥形   障碍物   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!