我需要在一个iPad精灵游戏中产生8个随机的精灵对象。这些物体相当大,大小不一。他们不应该重叠。如果一个是产卵在现场覆盖,它将被移除(可选的,它移除底部的一个)。我一直在寻找一个像素完美的碰撞检测框架或帮助类的雪碧套件。到目前为止,我还没有找到类似的教程。大多数人使用正常的碰撞检测,这不会有任何帮助,因为我的对象很大。我测试了标准方法,但它创建了矩形,使精灵的面积在我的情况下更大。这是我的雪碧工具包模板测试项目:
#import "WBMMyScene.h"
static const uint32_t randomObjectCategory = 0x1 << 0;
@interface WBMMyScene () <SKPhysicsContactDelegate>
@end
@implementation WBMMyScene
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
/* Setup your scene here */
self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
self.physicsWorld.gravity = CGVectorMake(0, 0);
self.physicsWorld.contactDelegate = self;
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
for (UITouch *touch in touches)
{
CGPoint location = [touch locationInNode:self];
SKSpriteNode *spaceship = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
//SKSpriteNode *spaceship = [SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(50, 50)];
spaceship.position = location;
[spaceship setSize:CGSizeMake(50, 50)];
[spaceship.texture setFilteringMode:SKTextureFilteringNearest];
//spaceship.texture setFilteringMode
spaceship.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spaceship.size];
spaceship.physicsBody.dynamic = YES;
spaceship.physicsBody.categoryBitMask = randomObjectCategory;
spaceship.physicsBody.contactTestBitMask = randomObjectCategory;
spaceship.physicsBody.collisionBitMask = 0;
[self addChild:spaceship];
}
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
// 1
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
// 2
if (firstBody.categoryBitMask == secondBody.categoryBitMask)
{
[self projectile:(SKSpriteNode *) firstBody.node didColliteWithEachOther:(SKSpriteNode *) secondBody.node];
}
}
- (void)projectile:(SKSpriteNode *)object1 didColliteWithEachOther:(SKSpriteNode *)object2
{
NSLog(@"Hit");
[object1 removeFromParent];
[object2 removeFromParent];
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
@end谢谢你抽出时间:)
发布于 2014-06-27 10:09:07
据我所知,iOS7不具备每个像素的物理特性,这将与XCode 6一起出现,它目前正处于测试阶段,现在可供开发人员使用。请记住,它是一个测试版,并且确实有一些bug,它的完整版本很可能在9月份与iOS8和新的iPhone一起发布。
同时,您有两个选项,您可以下载XCode 6测试版,并在此范围内为每个像素物理。然而,就我个人而言,我有点厌倦了使用beta进行全面开发,而是回到了XCode 5。在iOS7和XCode 5中,您可以选择用路径定义physicsBody,这可以准确地描述精灵的物理形状。
我使用的工具是这里,它允许您拖放图像,然后以图形方式定义路径点并返回代码,这真的很方便。
我希望这能帮到你!
发布于 2015-04-29 00:12:47
这个解决方案不能给像素完美的检测,所以可能不是你(或其他人发现这个问题)想要的,但它是一个非常好的选择,对一些人来说是理想的。
它通过分析精灵的纹理,自动计算出基于多边形的物理体。它基本上是在非透明像素周围画出一条路径,并将其用于碰撞。
而不是这样:
spaceship.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:spaceship.size];用这个:
spaceship.physicaBody = [SKPhysicsBody bodyWithTexture:spaceship.texture alphaThreshold:0.5f size:spaceship.size];https://stackoverflow.com/questions/24448974
复制相似问题