gpt4 book ai didi

Swift SpriteKit 在PhysicsBody 内部进行接触检查

转载 作者:行者123 更新时间:2023-11-30 10:10:30 24 4
gpt4 key购买 nike

我正在研究PhysicsBody,但遇到了一些麻烦。首先,我现在真的不知道这是否可以通过PhysicsBody实现,或者只能通过像素检查实现。

问题是:接触检查工作得很好,但如果节点位于PhysicsBody 内部,它将无法查看它是否发生碰撞。在图片中,它得到了很好的解释。

Picture 1: This works

Picture 2: This won't

一些想法它是如何工作的?也许PhysicsBody(绿线)填充了SKNode?

这里有一些代码:(有人解释:如果人类在对象内移动并且触摸为假,则什么也没有发生,没关系,但如果人类在对象内移动并且触摸将为真,也不会发生任何事情,这是我的问题。)

    enum myContacts: UInt32 {
case None = 0
case All = 0xFFFFFFFF
case Object = 0b001
case Human = 0b010
}

class whatever...{
let Object = SKSpriteNode(imageNamed: "xyz")
Object.position = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame))

Object.physicsBody = SKPhysicsBody(circleOfRadius: Object.size.width/2)
Object.physicsBody?.dynamic = true
Object.physicsBody?.categoryBitMask = myContacts. Object.rawValue
Object.physicsBody?.contactTestBitMask = myContacts.Human.rawValue
Object.physicsBody?.collisionBitMask = 0
Object.physicsBody?.usesPreciseCollisionDetection = true
addChild(Object)

let Human = SKSpriteNode(imageNamed: "zyx")
Human.position = CGPointMake(CGRectGetMidX(frame), CGRectGetMidY(frame))

Human.physicsBody = SKPhysicsBody(circleOfRadius: Human.size.width/2)
Human.physicsBody?.dynamic = true
Human.physicsBody?.categoryBitMask = myContacts. Human.rawValue
Human.physicsBody?.contactTestBitMask = myContacts.Object.rawValue
Human.physicsBody?.collisionBitMask = 0
Human.physicsBody?.usesPreciseCollisionDetection = true
addChild(Human)
}

func didBeginContact(contact: SKPhysicsContact) {
if touched {
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case myContacts.Object.rawValue | myContacts.Human.rawValue:
print("Human touched")
default:
break
}
}
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
touched = true
}

override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
touched = false
}

最佳答案

不确定我是否完全理解您的要求,但如果您只想在节点在另一个节点“占据”的空间内生成时检测接触,则此代码将起作用:

#import "GameScene.h"

static const uint32_t playerCategory = 0x1 << 0;
static const uint32_t obstacleCategory = 0x1 << 1;

@interface GameScene()<SKPhysicsContactDelegate>

@property (nonatomic, strong) SKSpriteNode *player;

@property (nonatomic, strong) SKSpriteNode *obstacle;

@end

@implementation GameScene

-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */

self.physicsWorld.contactDelegate = self;

[self initializeNodes];


}


-(void)initializeNodes{


self.player = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(50,50)];

self.player.name = @"player";

self.player.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

self.player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.player.size];
self.player.physicsBody.collisionBitMask = 0;
self.player.physicsBody.categoryBitMask = playerCategory;
self.player.physicsBody.affectedByGravity = NO;
self.player.physicsBody.contactTestBitMask = obstacleCategory;

self.player.zPosition = 10;

[self addChild:self.player];


self.obstacle = [SKSpriteNode spriteNodeWithColor:[SKColor yellowColor] size:CGSizeMake(100,100)];

self.obstacle.name = @"obstacle";

self.obstacle.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));

self.obstacle.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.obstacle.size];
self.obstacle.physicsBody.collisionBitMask = 0;
self.obstacle.physicsBody.categoryBitMask = obstacleCategory;
self.obstacle.physicsBody.affectedByGravity = NO;
self.obstacle.physicsBody.contactTestBitMask = playerCategory;

self.obstacle.zPosition = 8;

[self addChild:self.obstacle];

}





-(void)didBeginContact:(SKPhysicsContact *)contact{
NSLog(@"Contact detected!");
}


-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}

@end

在这里,一切都非常简单:

  • 我创建两个节点并为其分配物理体
  • 我将这两个节点放在同一位置
  • 结果,检测到了联系人

如果您复制并粘贴此代码并运行它,您将在控制台中看到一条消息:“检测到联系人”。

我猜第二张图片的情况相同(您说未检测到接触)。

此外,如果您继续移动玩家(当玩家位于障碍物范围内时),将不会检测到进一步的接触。玩家必须先离开障碍物的边界。但是,您仍然可以检查 allContactBodies 的返回值方法返回当前 body (玩家的物理 body )所接触的 SKPhysicsBody 对象数组。

关于Swift SpriteKit 在PhysicsBody 内部进行接触检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33193113/

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