iOS SpriteKit - 为bobblehead创建物理?

时间:2014-05-23 02:31:03

标签: ios animation sprite-kit physics

我正在尝试创建一个逼真的bobblehead应用程序,并希望将物理学用于挖掘动画。我已经看到其他应用程序在那里做,从我收集的我需要使用SpriteKit。

我创建了一个SKScene,并使用SKPhysicsJointPin将头部固定在身体上,但它根本不会移动。有什么想法或建议吗?

更新后的代码(5/28):

现在在头上使用2个弹簧接头,它从左向右移动,但不是向上和向下移动。此外,快速敲击会导致头部最终走得足够远,直到#34;并且不在视野范围内。怪异。

有任何关于适当设置的想法,以允许它在远离中心位于它的起始位置并且保持在指定区域内的时间,向下,向左和向右,但它不会脱离身体看起来很有趣?

BobbleheadView是一个子类SKView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor =[UIColor clearColor];
        self.showsFPS = YES;
        self.showsNodeCount = YES;

        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                       initWithTarget:self
                                       action:@selector(animateBobble)];

        [self addGestureRecognizer:tap];

        SKScene *bobbleheadScene = [SKScene sceneWithSize:self.bounds.size];
        [self presentScene:bobbleheadScene];

        // 1. Body
        self.body = [SKSpriteNode spriteNodeWithImageNamed:@"Bobble-Body"];
        self.body.position = CGPointMake(self.frame.size.width/2, self.body.frame.size.height/2);
        self.body.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        [bobbleheadScene addChild:self.body];

        // 2. Head
        self.head = [SKSpriteNode spriteNodeWithImageNamed:@"Bobble-Head"];
        self.head.position = CGPointMake(self.center.x, self.body.frame.size.height);
        //self.head.physicsBody.affectedByGravity = YES;
//        self.head.physicsBody.dynamic = NO;
        //This bobbles head great, but head falls off body and out of view
        self.head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.head.size center:self.head.position];
        //End
        //self.head.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.head.frame];
        //self.head.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:self.head.size.height/2];
        [bobbleheadScene addChild:self.head];

        // 3. Ceiling
        self.ceiling = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(32, 32)];
        self.ceiling.position = CGPointMake(self.frame.origin.x+self.frame.size.width/2, self.frame.size.height);
        self.ceiling.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];
        [bobbleheadScene addChild:self.ceiling];



        //Spring Joint for Ceiling to Head
        SKPhysicsJointSpring *spring1 = [SKPhysicsJointSpring jointWithBodyA:self.ceiling.physicsBody bodyB:self.head.physicsBody anchorA:self.ceiling.position anchorB:CGPointMake(self.head.frame.origin.x+self.head.frame.size.width/2, 0)];

        spring1.frequency = 20.0; //gives the spring some elasticity.
        spring1.damping = 5.0; //Will remove damping to create the 'pendulum'
        [bobbleheadScene.physicsWorld addJoint:spring1];


        //Spring Joint for Head to Body
        SKPhysicsJointSpring *spring = [SKPhysicsJointSpring jointWithBodyA:self.body.physicsBody bodyB:self.head.physicsBody anchorA:CGPointMake(self.body.position.x+self.body.size.width/2, self.body.position.y) anchorB:CGPointMake(self.head.position.x+self.head.size.width/2, self.body.position.y-self.body.size.height/2)];

        spring.frequency = 10.0; //gives the spring some elasticity.
        spring.damping = 1.0;
        [bobbleheadScene.physicsWorld addJoint:spring];

    }
    return self;
}

-(void)animateBobble{
    NSLog(@"Did Tap Bobblehead!");
    [self.head.physicsBody applyImpulse:CGVectorMake(100, -200)];
    //[self.body.physicsBody applyImpulse:CGVectorMake(20, 10)];
}

1 个答案:

答案 0 :(得分:5)

让这个工作的唯一方法是制作一个“雪橇”。

所以:制作一个应用程序,在屏幕上有(比方说)六个滑块。

TBC我的意思是,当应用程序实际运行时,您会看到六个滑块。

TBC,这只是一个雪橇,它只适合作为开发者,不适合消费者应用。

(我相信“雪橇”这个词来自汽车行业;当他们制造第一个版本的底盘/发动机来测试它时。)

使一些滑块控制弹簧功率/阻尼(或者你在cocos中可用的任何因素)并且其他滑块可以轻推连接/弹簧长度的位置。

这是获得结果的唯一途径!请务必在屏幕上显示这些值,或者至少在控制台上更改时将它们打印出来。

这是游戏开发中的日常事务。 通常,雪橇比实际的消费者场景或功能更多的工作。如果没有“雪橇”,你将无法实现它。我希望它有所帮助!