【问题标题】:3D perspective view in iphone game using cocos2d使用 cocos2d 的 iphone 游戏中的 3D 透视图
【发布时间】:2012-04-09 22:24:01
【问题描述】:

我正在使用cocos2d为iphone开发'Paper Toss'&我想知道如何实现3D透视图,因为当我们把纸球扔到垃圾桶里时,我们必须得到3D 感觉。我正在附上我所做的代码,使用它我有一个直线运动。请帮助我..

*- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

// Choose one of the touches to work with
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];

// Set up initial location of projectile
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCSprite *projectile = [CCSprite spriteWithFile:@"ball.png" 
                                           rect:CGRectMake(0, 0, 40, 40)];
projectile.position = ccp(winSize.width/2,20);

// Determine offset of location to projectile
int offX = location.x - projectile.position.x;
int offY = location.y - projectile.position.y;

// Bail out if we are shooting down or backwards
if (offY <= 0) return;

// Ok to add now - we've double checked position
[self addChild:projectile];

// Determine where we wish to shoot the projectile to
int realY = winSize.height + (projectile.contentSize.width/2);
float ratio = (float) offX / (float) offY;
int realX = (realY * ratio) + projectile.position.x;
CGPoint realDest = ccp(realX, realY);

// Determine the length of how far we're shooting
int offRealX = realX + projectile.position.x;
int offRealY = realY + projectile.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;

// Move projectile to actual endpoint
[projectile runAction:[CCSequence actions:
                       [CCMoveTo actionWithDuration:realMoveDuration position:realDest],
                       [CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
                       nil]];
//add to the projectiles array
projectile.tag = 2;
[_projectiles addObject:projectile];

}*

【问题讨论】:

    标签: iphone cocos2d-iphone


    【解决方案1】:

    我终于用cocos2d完成了纸折腾。我实现了贝塞尔曲线,就是这样,

      // Bezier curve control points
      bezier.controlPoint_1 = ccp(location.x-CONTROL_POINT1_X, CONTROL_POINT1_Y);
      bezier.controlPoint_2 = ccp(location.x-CONTROL_POINT2_X, CONTROL_POINT2_Y);
      bezier.endPosition = ccp(location.x-CONTROL_POINT1_X,distance);
    
      // Motion along bezier curve and finally call a function
      [projectile runAction:[CCSequence actions:
                               [CCAutoBezier actionWithDuration:DEFAULT_ACTION_DURATION bezier:bezier],
                               [CCCallFuncN actionWithTarget:self selector:@selector(collisionCheck:)], nil]];
    

    【讨论】:

    • 我在 3 个关卡中添加了一个粉丝,现在我的游戏非常棒 :)
    • 你能告诉我更多关于 CONTROL_POINT 的信息
    • @TonyMac :- 我们可以使用贝塞尔曲线进行抛射运动。但是在折纸中,我看到纸张的大小也有所不同。你是怎么做到的?你也可以告诉我当纸张碰撞垃圾箱边缘时我们如何实现反弹效果?谢谢
    • 你能解释一下 CONTROL_POINT 吗?
    【解决方案2】:

    只需缩放您的精灵图像,使其“越远”越小。

    【讨论】:

    • “精确透视图”是什么意思?
    【解决方案3】:

    您可以在cocos2d 中将bezier curve 用于3d 透视图。

    bezier.controlPoint_1 = ccp(location.x-CONTROL_POINT1_X, CONTROL_POINT1_Y);
      bezier.controlPoint_2 = ccp(location.x-CONTROL_POINT2_X, CONTROL_POINT2_Y);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 2011-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多