gpt4 book ai didi

iphone - 如何在box2d iPhone中手动制作一个动态物体(球)进入盒子

转载 作者:行者123 更新时间:2023-12-03 19:21:21 26 4
gpt4 key购买 nike

由于我是 cocos2d 的新手,而且我很挣扎。任何人都可以建议我如何解决这个问题。

  1. 我有 3 个盒子(它们是运动体)

  2. 还有多个球(它们是动态物体),每个球都有一个标签值(盒子编号)。

  3. 我在射球位置和盒子之间有一些障碍物(运动体)。

场景是我将随机获得盒子编号,例如:2(第二个盒子),并将其设置为球的标签值。即使球撞到障碍物,球也应该准确地进入第二个盒子。如果我得到下一个球的盒子编号为 3,则球应该进入第三个盒子。

有人可以推荐我吗

谢谢你,莫尼什

最佳答案

这是一个简单的辅助类,用于创建球并返回。您可以使用类似的方法来制作盒子。

//
// Ball.h
// BouncingBall
//
// Created by Omar Hussain on 8/12/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"

class Ball : public CCSprite {
public:
CCSprite *sprite;
b2BodyDef ballBodyDef;
b2Body *ballBody;
b2FixtureDef ballShapeDef;
b2Fixture *fixture;
Ball(b2World *world);
b2Vec2 disp;
CCSprite *shadowBall;
b2World *world;
bool isAnimating;
void SetBallOverlap(bool willOverlap, int scale);
void Destroy();
};


#import "Ball.h"

#define Density 1.0f
#define Friction 1.0f
#define Restitution 0.8f
#define PTM_RATIO 32
#define SpriteRadius 40.5


Ball::Ball(b2World *world){
this->world = world;
this->sprite = [CCSprite spriteWithFile:@"5.png"];
[sprite setPosition:ccp(160,460)];
this->isAnimating = false;
//Set up sprite
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(160/PTM_RATIO, 460/PTM_RATIO);
ballBodyDef.userData = sprite;
this->ballBody = world->CreateBody(&ballBodyDef);

b2CircleShape circle;
circle.m_radius = SpriteRadius/PTM_RATIO;

ballShapeDef.shape = &circle;
ballShapeDef.density = Density;
ballShapeDef.friction =Friction;
ballShapeDef.restitution = Restitution;
this->fixture = this->ballBody->CreateFixture(&ballShapeDef);

//this->shadowBall = [TSprite spriteWithFile:@"ball_shadow.png"];


}
void Ball::SetBallSize(int scale){
this->ballBody->DestroyFixture(fixture);
this->world->DestroyBody(ballBody);

ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(160/PTM_RATIO, 460/PTM_RATIO);
ballBodyDef.userData = sprite;
this->ballBody = world->CreateBody(&ballBodyDef);
float correctedScale = scale/9.0;
b2CircleShape circle;
circle.m_radius = (correctedScale *SpriteRadius)/PTM_RATIO;

ballShapeDef.shape = &circle;
ballShapeDef.density = Density;
ballShapeDef.friction = Friction;
ballShapeDef.restitution = Restitution;
this->fixture = this->ballBody->CreateFixture(&ballShapeDef);
//this->shadowBall.scale = scale;
this->sprite.scale = correctedScale;
}
void Ball::SetBallOverlap(bool willOverlap, int scale){
this->world->DestroyBody(ballBody);

ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(160/PTM_RATIO, 460/PTM_RATIO);
ballBodyDef.userData = sprite;
this->ballBody = world->CreateBody(&ballBodyDef);
float correctedScale = scale/9.0;
b2CircleShape circle;
circle.m_radius = (correctedScale *SpriteRadius)/PTM_RATIO;

ballShapeDef.shape = &circle;
ballShapeDef.density = Density;
ballShapeDef.friction = Friction;
ballShapeDef.restitution = Restitution;
if(willOverlap)
ballShapeDef.filter.groupIndex = 1;
else
ballShapeDef.filter.groupIndex = -1;
this->ballBody->CreateFixture(&ballShapeDef);
}
void Ball::Destroy(){
this->ballBody->DestroyFixture(fixture);
this->world->DestroyBody(ballBody);
}

关于iphone - 如何在box2d iPhone中手动制作一个动态物体(球)进入盒子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14209191/

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