GameplayKit的障碍 [英] Obstacles With GameplayKit
问题描述
这是我的问题:
- 如何将
SKSpriteNode
数组转换为GKObstacles
的数组,以便代理可以使用goalToAvoidObstacles:(nonnull NSArray<GKObstacle *> *) maxPredictionTime:(NSTimeInterval)
/适当地避免这些障碍?
- How can I convert an
SKSpriteNode
array into an array ofGKObstacles
so that an agent can appropriately avoid these obstacles by using thegoalToAvoidObstacles:(nonnull NSArray<GKObstacle *> *) maxPredictionTime:(NSTimeInterval)
/?
无论我对目标有多大的重视,似乎我创建GKObstacle
数组的方式都不允许GKGoal
正确地将它们识别为障碍.
It seems that the way in which I am creating the GKObstacle
array is not allowing the GKGoal
to identify these as obstacles correctly, no matter what weight I give to the goal.
我目前有几个SKNode
已添加到SKScene
chapterScene 中.这些节点中的每一个都添加到我正在存储的名为 obstaclesArray 的数组中.我已经通过以下方式很好地进行了测试:
I have currently several SKNode
s that are added to an SKScene
chapterScene. Each of these nodes are added to an array that I am storing called obstaclesArray. I have set up a test in the following way that works quite well:
工作障碍
- (void)didMoveToView:(nonnull SKView *)view {
[super didMoveToView:view];
// Add three obstacles in a triangle formation around the center of the scene.
NSArray<GKObstacle *> *obstacles = @[
[self addObstacleAtPoint:CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame) + 150)],
[self addObstacleAtPoint:CGPointMake(CGRectGetMidX(self.frame) - 200,
CGRectGetMidY(self.frame) - 150)],
[self addObstacleAtPoint:CGPointMake(CGRectGetMidX(self.frame) + 200,
CGRectGetMidY(self.frame) - 150)],
];
// The player agent follows the tracking agent.
self.player = [[OPlayer alloc] initWithScene:self
radius:50
position:CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame))];
self.player.agent.behavior = [[GKBehavior alloc] init];
[self.agentSystem addComponent:self.player.agent];
// Create the seek goal, but add it to the behavior only in -setSeeking:.
self.seekGoal = [GKGoal goalToSeekAgent:self.character];
// Add an avoid-obstacles goal with a high weight to keep the agent from overlapping the obstacles.
[self.player.agent.behavior setWeight:100 forGoal:[GKGoal goalToAvoidObstacles:obstacles maxPredictionTime:1]];
}
- (GKObstacle *)addObstacleAtPoint:(CGPoint)point {
SKShapeNode *circleShape = [SKShapeNode shapeNodeWithCircleOfRadius:50];
circleShape.lineWidth = 2.5;
circleShape.fillColor = [SKColor grayColor];
circleShape.strokeColor = [SKColor redColor];
circleShape.zPosition = 1;
circleShape.position = point;
[self addChild:circleShape];
GKCircleObstacle *obstacle = [GKCircleObstacle obstacleWithRadius:50];
obstacle.position = (vector_float2){point.x, point.y};
return obstacle;
}
虽然这很好用,但我遇到的问题是无法获得识别作为障碍的SKNode
主体阵列的行为.我只能获得这些手动创建的GKCircleObstacle
项目,以注册为代理商可以避免的有效障碍.之所以成为问题,是因为我所依赖的障碍实际上不是简单的圆形,而是多边形结构,有些复杂,有些更加直接.但是,我正在尝试以下操作,但我的经纪人似乎并没有避免这种方式出现的任何障碍:
While this works just fine, the issue I am having is that I can not get the behavior to identify the SKNode
bodies array that I have as obstacles. I can only get these manually created GKCircleObstacle
items to register as valid obstacles that the agent avoids. The reason this is an issue is because I am relying on many obstacles that are not in fact simple circles, but polygon structures some complex, some more straightforward. Nevertheless, I was attempting the following but my agent did not seem to avoid any of the obstacles that are of SKNode
this way:
没有障碍
NSArray *obstacles = [SKNode obstaclesFromNodePhysicsBodies:obstaclesArray];
/*
The other code seen above
*/
// Add an avoid-obstacles goal with a high weight to keep the agent from overlapping the obstacles.
[self.player.agent.behavior setWeight:100 forGoal:[GKGoal goalToAvoidObstacles:obstacles maxPredictionTime:1]];
不幸的是,这似乎不起作用,因为无论我将体重加到多高,代理商都不会对避开障碍做出反应.这是我传递给它的数组的日志:
Unfortunately, this does not seem to work as no matter how high I set the weight to, the agent never responds to avoiding the obstacles. Here is a log of the array I am passing to it:
2016-07-24 22:32:24.907 <GAME>[1516:475581] obstacles: (
"<GKPolygonObstacle: 0x14d20d70>",
"<GKPolygonObstacle: 0x14d20e10>",
"<GKPolygonObstacle: 0x14d20ba0>",
"<GKPolygonObstacle: 0x14d20bb0>",
"<GKPolygonObstacle: 0x14d20bc0>",
"<GKPolygonObstacle: 0x14d20a60>",
"<GKPolygonObstacle: 0x14d208b0>",
"<GKPolygonObstacle: 0x14d207d0>",
"<GKPolygonObstacle: 0x14d20a70>"
)
其中的每一个都已被清楚,适当地初始化并添加到场景中.这些元素中的每个元素实际上都是通过SpriteKit
didBeginContact:(SKPhysicsContact *)contact
方法响应的,因此似乎节点已正确处理了它们应具有的边界.
Each of which have all been clearly and appropriately initialized and added to the scene. Each of these elements are in fact responsive with the SpriteKit
didBeginContact:(SKPhysicsContact *)contact
method, so it seems that the nodes are properly handing the boundaries that they should be having.
如果有人知道我做错了什么,或者将这些障碍节点"中的每一个转换成GKObstacle
的更好方法,我将不胜感激.预先感谢!
If someone knows what I am doing wrong or a better way to turn each of these 'obstacle nodes' into GKObstacle
's I would be much appreciative. Thanks in advance!
更新:这是一个我正在测试的具有物理物体的节点:
UPDATE: Here is a node with a physics body that I am testing:
SKSpriteNode *innerMap1 = [SKSpriteNode spriteNodeWithImageNamed:@"test"];
innerMap1.physicsBody = [SKPhysicsBody bodyWithTexture:[SKTexture textureWithImageNamed:@"test"] size:CGSizeMake(innerMap1.frame.size.width*0.92, innerMap1.frame.size.height*0.92)];
innerMap1.position = CGPointMake(-220, -140);
innerMap1.physicsBody.dynamic = NO;
[chapterSKSpriteNodes addObject:innerMap1];
这是skView.showsPhysics = YES;
的身体外观:
所以我知道物理身体就是我所需要的.但是,当我尝试从此主体创建GKObstacle
时,在将其分配到voidObstacles目标中时,它似乎并没有注册为障碍.
So I know that the physics body is what I need it to be. When I attempt to create the GKObstacle
from this body however, it does not seem to register as an obstacle when being assigned in the avoidObstacles goal.
推荐答案
这听起来像是个错误.您应该检查barriersFromNodePhysicsBodies的返回数组,并确保您实际上正在收回期望的障碍.我怀疑你不是.
This sounds like a bug. You should check the return array from obstaclesFromNodePhysicsBodies and make sure you are actually getting back the obstacles you expect. I suspect you are not.
您可以检查障碍物上的相关顶点,例如:
You can check the relevant vertices on your obstacles as such:
确保它们符合您的期望.
Make sure they are what you expect.
这篇关于GameplayKit的障碍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!