gpt4 book ai didi

ios - 动态锥体形状根据障碍物而变化 - 引用 Third Eye Crime 应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:18 25 4
gpt4 key购买 nike

我看过 "Third Eye Crime" 的预告片.

如何实现蓝色视场锥体,使其形状根据障碍物发生变化?

enter image description here

我的尝试是转换光线直到出现障碍物,然后我用光线的终点绘制锥形。

enter image description here

我的方法的问题是圆锥体的精度取决于光线的数量。此外,转换的光线越多,性能越差。

在这里你可以看到光线:

enter image description here

在这里你可以看到用光线的端点绘制的锥形:

enter image description here

有没有更好的解决方案?

最佳答案

你的问题一直困扰着我一整天,但我找不到一个完全可行的答案。我的工作笔记太多了,我无法将其作为评论来写,所以我将其添加为答案。

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

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

我还查看了 Apple 的 Sprite Kit 编程指南的部分 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

删除接触线:

enter image description here

删除接触线并替换删除的线:

enter image description here

关于ios - 动态锥体形状根据障碍物而变化 - 引用 Third Eye Crime 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23291724/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com