首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >雪碧套件SKPhysicsJoint

雪碧套件SKPhysicsJoint
EN

Stack Overflow用户
提问于 2014-11-04 10:04:56
回答 1查看 526关注 0票数 0

我试着用雪碧套件的联合系统做实验。到目前为止,我连一根“绳子”/“绳子”都做不好。最终目标是制作一个像篮球网一样的东西,其中将有1根左绳(垂直),1根右绳(垂直)&至少有2根绳子连接左右绳子(水平)-4根绳子。然后,找出如何使两根水平绳索不与物体相撞(炮弹?)当它穿过它们时。有人知道怎么做这种事吗?我已经试了两个星期了,我的眼睛和手指都疼了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-11-11 19:12:23

你可以把几个物理物体和极限关节连接起来,形成一个绳子状的结构,它会受到与其他物体碰撞的影响。我的代码非常混乱,但是这里是一个sprite工具包项目的场景类。它有一个篮网,当你点击屏幕时,它会产生一个球。

这里有截图https://dl.dropboxusercontent.com/s/flixgbhfk2yysr8/screenshot.jpg?dl=0

编辑:很抱歉,代码现在在obj-C中。

编辑:将这些方法粘贴到SKScene子类中:

代码语言:javascript
复制
-(void)addBasketAtPos:(CGPoint)pos WithSize:(CGSize)size HoopThickness:(CGFloat)hoopThickness RopeThickness:(CGFloat)ropeThickness GapSize:(CGFloat)gapSize {

    int nSect=ceil(size.height/(ropeThickness*4));

    SKSpriteNode *hoop=[SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(size.width+hoopThickness, hoopThickness)];
    hoop.position=pos;
    //[self addChild:hoop];

    SKNode *lHoopEdge=[SKNode node];
    lHoopEdge.position=CGPointMake(pos.x-size.width/2, pos.y);
    lHoopEdge.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:hoopThickness/2];
    lHoopEdge.physicsBody.dynamic=false;
    lHoopEdge.physicsBody.restitution=0.9;
    [self addChild:lHoopEdge];
    SKNode *rHoopEdge=[SKNode node];
    rHoopEdge.position=CGPointMake(pos.x+size.width/2, pos.y);
    rHoopEdge.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:hoopThickness/2];
    rHoopEdge.physicsBody.dynamic=false;
    rHoopEdge.physicsBody.restitution=0.9;
    [self addChild:rHoopEdge];

    NSMutableArray *rope1=[self makeRopeAtPos:lHoopEdge.position node:lHoopEdge num: nSect gap:size.height/nSect width:ropeThickness];
    NSMutableArray *rope2=[self makeRopeAtPos:rHoopEdge.position node:lHoopEdge num: nSect gap:size.height/nSect width:ropeThickness];

    CGFloat insetStep=(size.width-gapSize)/2/nSect;
    for (int i=0;i<rope1.count;++i) {
        SKNode *a=[rope1 objectAtIndex:i];
        SKNode *b=[rope2 objectAtIndex:i];
        a.position=CGPointMake(a.position.x+i*insetStep, a.position.y);
        b.position=CGPointMake(b.position.x-i*insetStep, b.position.y);
    }

    for (int i=0;i<rope1.count;++i) {
        /*
        SKNode *n1=[rope1 objectAtIndex:i];
        SKNode *n2=[rope2 objectAtIndex:i];
        SKPhysicsJointLimit* joint=[self joinBodyA:n1.physicsBody bodyB:n2.physicsBody ropeThickness:ropeThickness];
        [self.physicsWorld addJoint:joint];
         */
        if (i>0) {
            [self.physicsWorld addJoint:[self joinBodyA:((SKNode*)[rope1 objectAtIndex:i]).physicsBody
                                                  bodyB:((SKNode*)[rope2 objectAtIndex:i-1]).physicsBody
                                          ropeThickness:ropeThickness]];
            [self.physicsWorld addJoint:[self joinBodyA:((SKNode*)[rope2 objectAtIndex:i]).physicsBody
                                                  bodyB:((SKNode*)[rope1 objectAtIndex:i-1]).physicsBody
                                          ropeThickness:ropeThickness]];
        }
        if (i==rope1.count-1) {
            [self.physicsWorld addJoint:[self joinBodyA:((SKNode*)[rope1 objectAtIndex:i]).physicsBody
                                                  bodyB:((SKNode*)[rope2 objectAtIndex:i]).physicsBody
                                          ropeThickness:ropeThickness]];
        }
    }

    [self addChild:hoop];
}

-(NSMutableArray*)makeRopeAtPos:(CGPoint)p node:(SKNode*)node num:(int)num gap:(CGFloat)gap width:(CGFloat)width {
    NSMutableArray *array=[[NSMutableArray alloc] init];
    SKNode *last=node;
    CGPoint pos=p;
    CGPoint anchor=pos;
    for (int i=0; i<num; ++i) {
        pos=CGPointMake(pos.x, pos.y-gap);
        last=[self makeRopeSegAtPos:pos prev:last anchor:anchor first:(i==0) width:width];
        anchor=pos;
        [array addObject:last];
    }
    return array;
}

-(SKNode*)makeRopeSegAtPos:(CGPoint)pos prev:(SKNode*)prev anchor:(CGPoint)anchor first:(BOOL)first width:(CGFloat)width {
    //SKNode *r=[SKNode node];
    SKSpriteNode *r=[SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(anchor.y-pos.y, width)];
    r.position=pos;
    r.anchorPoint=CGPointMake(0, 0.5);
    if (first) {
        //r.size=CGSizeMake(10, 20);
        r.constraints=@[[SKConstraint orientToPoint:anchor inNode:self offset:nil]];
    }else {
        r.constraints=@[[SKConstraint orientToNode:prev offset:nil]];
    }
    r.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:width];
    r.physicsBody.allowsRotation=NO;
    //r.physicsBody.density=0.2;
    r.physicsBody.linearDamping=0.8;
    [self addChild:r];
    SKPhysicsJointLimit *j=[SKPhysicsJointLimit jointWithBodyA:r.physicsBody bodyB:prev.physicsBody anchorA:r.position anchorB:anchor];
    [self.physicsWorld addJoint:j];
    return r;

}

-(SKPhysicsJoint*)joinBodyA:(SKPhysicsBody*)bodyA bodyB:(SKPhysicsBody*)bodyB ropeThickness:(CGFloat)ropeThickness {
SKPhysicsJointLimit *j=[SKPhysicsJointLimit jointWithBodyA:bodyA bodyB:bodyB anchorA:bodyA.node.position anchorB:bodyB.node.position];
SKSpriteNode *rope=[SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(j.maxLength, ropeThickness)];

rope.anchorPoint=CGPointMake(0, 0.5);
rope.constraints=@[[SKConstraint distance:[SKRange rangeWithUpperLimit:1] toNode:bodyA.node],[SKConstraint orientToNode:bodyB.node offset:nil]];
SKAction *keepLength=[SKAction repeatActionForever:[SKAction sequence:@[[SKAction waitForDuration:1/60],[SKAction runBlock:^{
    rope.size=CGSizeMake(sqrtf(powf(bodyA.node.position.x-bodyB.node.position.x, 2)+powf(bodyA.node.position.y-bodyB.node.position.y, 2)), rope.size.height);
}]]]];
[rope runAction:keepLength];
[self addChild:rope];
return j;

}

使用

代码语言:javascript
复制
[self addBasketAtPos: CGPointMake(self.size.width/2, self.size.height*0.7)
            WithSize: CGSizeMake(100, 100)
       HoopThickness: 10
       RopeThickness: 5
             GapSize: 80
];

加一个篮子

见新的屏幕截图:https://dl.dropboxusercontent.com/s/xe07ri70rgpxp47/screenshot%202.jpg?dl=0

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26732405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档