box2d碰撞检测,代码

发布于 2025-01-05 01:54:15 字数 1298 浏览 0 评论 0原文

我是编程新手,我已经尝试过 ray wenderlich 教程,但我仍然很困惑。

我有一个名为“岩石”的精灵和一个名为“玩家”的精灵,我试图检测它们之间的碰撞。但我非常困惑。需要一些帮助。

-(void)addRock {

CCSprite *rock = [CCSprite spriteWithFile:@"rock.png" 
                                     rect:CGRectMake(0, 0, 27, 40)]; 

// Determine where to spawn the target along the X axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minX = rock.contentSize.width/2;
int maxX = winSize.width - rock.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;

// Create the target slightly off-screen along the right edge,
// and along a random position along the X axis as calculated above
rock.position = ccp(actualX, 500);
[self addChild:rock];

// Determine speed of the sprite
int actualDuration = 5;//speed of sprite

它们

- (id)init {

if ((self=[super init])) {

        CGSize winSize = [[CCDirector sharedDirector] winSize];

        CCSprite *player = [CCSprite spriteWithFile:@"Player.png" 
                                     rect:CGRectMake(0, 0, 27, 40)];

        player.position = ccp(winSize.width/2, winSize.height/4+15);

        [self addChild:player];

        [self schedule:@selector(gameLogicRock:) interval:0.2];

是两个精灵,它们生成并正确定位,我只需要检测碰撞

im new to programming, i have tried the ray wenderlich tutorial but im still confused.

i have a sprite called rock, and a sprite called player, im trying to detect collision between them. but im extremely confused. in need of some help.

-(void)addRock {

CCSprite *rock = [CCSprite spriteWithFile:@"rock.png" 
                                     rect:CGRectMake(0, 0, 27, 40)]; 

// Determine where to spawn the target along the X axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minX = rock.contentSize.width/2;
int maxX = winSize.width - rock.contentSize.width/2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;

// Create the target slightly off-screen along the right edge,
// and along a random position along the X axis as calculated above
rock.position = ccp(actualX, 500);
[self addChild:rock];

// Determine speed of the sprite
int actualDuration = 5;//speed of sprite

}

- (id)init {

if ((self=[super init])) {

        CGSize winSize = [[CCDirector sharedDirector] winSize];

        CCSprite *player = [CCSprite spriteWithFile:@"Player.png" 
                                     rect:CGRectMake(0, 0, 27, 40)];

        player.position = ccp(winSize.width/2, winSize.height/4+15);

        [self addChild:player];

        [self schedule:@selector(gameLogicRock:) interval:0.2];

they are the two sprites, they spawn and position correctly, i only need to detect collision

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空城旧梦 2025-01-12 01:54:15

您必须为碰撞实现一个新类,或者根据某些标准(如标签号)说接触侦听器。我有一个例子给你。

// ContactListener.h

#import "Box2D.h"

class ContactListener : public b2ContactListener 
{
private: 
    void BeginContact(b2Contact* contact);
    void EndContact(b2Contact* contact);
};

// ContactListener.mm

#import "ContactListener.h"
#import "cocos2d.h"
#import "BodyNode.h"
#import "GameScene.h"

void ContactListener::BeginContact(b2Contact* contact)
{
    b2Body* bodyA = contact->GetFixtureA()->GetBody();
    b2Body* bodyB = contact->GetFixtureB()->GetBody();
    if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
    {
        BodyNode* bNodeA = (BodyNode*)bodyA->GetUserData();
        BodyNode* bNodeB = (BodyNode*)bodyB->GetUserData();

        if ((bNodeA.tag == GameSceneNodeTagBall && bNodeB.tag == GameSceneNodeTagHole) || 
            (bNodeA.tag == GameSceneNodeTagHole && bNodeB.tag == GameSceneNodeTagBall)) 
        {
            switch (bNodeA.tag) {
                case GameSceneNodeTagBall:
                    if ([bNodeA isKindOfClass:[Ball class]]) {
                        Ball* ball = (Ball*)bNodeA;
                        ball.sprite.visible = NO;
                        [[GameScene sharedGameScene] gameOver];
                    }
                    break;
                case GameSceneNodeTagHole:
                    if ([bNodeB isKindOfClass:[Ball class]]) {
                        Ball* ball = (Ball*)bNodeB;
                        ball.sprite.visible = NO;
                        [[GameScene sharedGameScene] gameOver];
                    }
                    break;

                default:
                    break;
            }
        }
    }
}

这是球和洞碰撞的示例。您可以根据您的要求使用它。

希望这会对您有所帮助。

谢谢!

You have to implement a new class for the collision or say contact listener based on some criteria as tag no. i have an example for you.

// ContactListener.h

#import "Box2D.h"

class ContactListener : public b2ContactListener 
{
private: 
    void BeginContact(b2Contact* contact);
    void EndContact(b2Contact* contact);
};

// ContactListener.mm

#import "ContactListener.h"
#import "cocos2d.h"
#import "BodyNode.h"
#import "GameScene.h"

void ContactListener::BeginContact(b2Contact* contact)
{
    b2Body* bodyA = contact->GetFixtureA()->GetBody();
    b2Body* bodyB = contact->GetFixtureB()->GetBody();
    if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL)
    {
        BodyNode* bNodeA = (BodyNode*)bodyA->GetUserData();
        BodyNode* bNodeB = (BodyNode*)bodyB->GetUserData();

        if ((bNodeA.tag == GameSceneNodeTagBall && bNodeB.tag == GameSceneNodeTagHole) || 
            (bNodeA.tag == GameSceneNodeTagHole && bNodeB.tag == GameSceneNodeTagBall)) 
        {
            switch (bNodeA.tag) {
                case GameSceneNodeTagBall:
                    if ([bNodeA isKindOfClass:[Ball class]]) {
                        Ball* ball = (Ball*)bNodeA;
                        ball.sprite.visible = NO;
                        [[GameScene sharedGameScene] gameOver];
                    }
                    break;
                case GameSceneNodeTagHole:
                    if ([bNodeB isKindOfClass:[Ball class]]) {
                        Ball* ball = (Ball*)bNodeB;
                        ball.sprite.visible = NO;
                        [[GameScene sharedGameScene] gameOver];
                    }
                    break;

                default:
                    break;
            }
        }
    }
}

This is an example of ball and hole collision. You can use this as per your requirement.

Hope this will help you.

Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文