skphysicsjoint无法将2条(或更多)绳索连接/固定在一起

时间:2019-01-11 08:43:59

标签: objective-c xcode sprite-kit game-development

我正在尝试使用不同的skphysicsjoint子类将两根绳索的尖端连接在一起,但是两根绳索之间仍然留有一些缝隙,无论我使用什么方法来缩小缝隙,这都会增加重量或增加物体的缝隙

enter image description here

代码中有3条绳索(绳索1-左白色绳索,绳索2-右白色绳索,绳索-黄色底部绳索)。

我已经通过使用skphysicsjoint类使用[SKConstraint distance ..]将每条绳索的所有段保持在一起,这些段之前也无法正常工作(拉伸)。

下面是我的代码,包括我也尝试过并注释掉的所有方法。我已经搜索了与该主题相关的现有Stack Overflow问题,但除以下链接以外,没有一个满足我的需求:Sprite Kit SKPhysicsJoint

有一个设计合理的代码篮,但是问题是代码中的SKAction段使应用程序无法响应诸如touchesBegan,touchesEnded函数之类的方法,这很奇怪,因为SKAction从未在我的其他游戏应用程序中阻止过touch方法。已经开发的。

我知道我的代码不干净且不规范,但是目前我的目标是实现我的目标,以后再清理代码。

#import "GameScene.h"
#import "Basketball.h"
#import "Basket.h"

@implementation GameScene {
    //SKShapeNode *_spinnyNode;
    //SKLabelNode *_label;

    SKShapeNode *ground;
    //SKShapeNode *shadow;
    SKSpriteNode *headboard1;
    SKSpriteNode *headboard2;
    SKSpriteNode *basketball;

    //Basketball *basketball;
    //Basket *basket;

    CGPoint firstLocation,secondLocation;

    SKSpriteNode *lHoopEdge;
    SKSpriteNode *rHoopEdge;

    NSMutableArray<SKSpriteNode *> *rope1;
    NSMutableArray<SKSpriteNode *> *rope2;

    NSMutableArray<SKSpriteNode *> *ropesLeft1;
}

static const uint32_t gravityCategory=0x1<<0;

-(id)initWithSize:(CGSize)size
{
    if(self=[super initWithSize:size])
    {
        self.anchorPoint=CGPointMake(0.5,0.5);
        self.physicsWorld.contactDelegate=self;

        self.backgroundColor=[SKColor colorWithRed:0.28 green:0.82 blue:0.80 alpha:0.5];

        //basketball=[Basketball generateWithWorld:self];
        //[self addChild:basketball];

        //[basketball generate:ground.position.x height:ground.position.y];

        int w=self.scene.frame.size.width;
        int h=self.scene.frame.size.height;

        //[basketball removeFromParent];

        //basket=[Basket generateWithWorld:self];
        //[self addChild:basket];
        //[self addBasketAtPos:CGPointMake(self.size.width/2,self.size.height*0.7) WithSize:CGSizeMake(100,100) HoopThickness:10 RopeThickness:5 GapSize:80];
        //[self addBasketAtPos:CGPointMake(0,0) WithSize:CGSizeMake(100,100) HoopThickness:10 RopeThickness:5 GapSize:80];
        //[self addBasketAtPos:CGPointMake(0, headboard1.position.y-headboard1.size.height/3) WithSize:CGSizeMake(self.scene.frame.size.height/3.7,self.scene.frame.size.height/4.7) HoopThickness:self.scene.frame.size.height/60 RopeThickness:self.scene.frame.size.height/150 GapSize:self.scene.frame.size.height/6.7];
        NSLog(@"%f",floor(self.scene.frame.size.height/7.5));

        //[[self childNodeWithName:@"rope"]removeActionForKey:@"animation"];

        [self hoopEdgesWithSize:CGSizeMake(ceil(h/60),ceil(h/48)) PositionX:floor(h/7.5) PositionY:headboard1.position.y-headboard1.size.height/3];

        SKSpriteNode *prev1=lHoopEdge;
        SKSpriteNode *prev2=rHoopEdge;
        rope1=[[NSMutableArray alloc]init];
        rope2=[[NSMutableArray alloc]init];

        for(int i=0;i<40;i++)
        {
            rope1[i]=[SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(3, 3)];
            rope1[i].position=CGPointMake(prev1.position.x+1, prev1.position.y-(prev1.size.height/2));
            rope1[i].zRotation=M_PI/3;
            rope1[i].name=@"rope1";
            rope1[i].zPosition=100;
            //rope1[i].physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:rope1[i].size];
            rope1[i].physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:rope1[i].size.width/2];
            rope1[i].physicsBody.allowsRotation=NO;
            rope1[i].physicsBody.linearDamping=0.9;
            rope1[i].physicsBody.angularDamping=0.9;
            rope1[i].physicsBody.dynamic=YES;
            rope1[i].physicsBody.affectedByGravity=YES;
            //rope1[i].physicsBody.friction=0;
            rope1[i].physicsBody.resting=YES;
            rope1[i].physicsBody.restitution=0.01;
            rope1[i].physicsBody.mass=9999999999;
            rope1[i].constraints=@[[SKConstraint distance:[SKRange rangeWithLowerLimit:rope1[i].size.width/1.4 upperLimit:rope1[i].size.width/1.4] toNode:prev1]];
            [self addChild:rope1[i]];
            //[lHoopEdge addChild:rope1];

            SKPhysicsJointSpring *j1=[SKPhysicsJointSpring jointWithBodyA:prev1.physicsBody bodyB:rope1[i].physicsBody anchorA:prev1.position anchorB:rope1[i].position];
            //SKPhysicsJointLimit *j1=[SKPhysicsJointLimit jointWithBodyA:prev1.physicsBody bodyB:rope1.physicsBody anchorA:prev1.position anchorB:rope1.position];
            //SKPhysicsJointPin *j1=[SKPhysicsJointPin jointWithBodyA:prev1.physicsBody bodyB:rope1[i].physicsBody anchor:prev1.position];
            //SKPhysicsJointFixed *j1=[SKPhysicsJointFixed jointWithBodyA:prev1.physicsBody bodyB:rope1[i].physicsBody anchor:prev1.position];
            [self.physicsWorld addJoint:j1];

            prev1=rope1[i];

            rope2[i]=[SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(3, 3)];
            rope2[i].position=CGPointMake(prev2.position.x-1, prev2.position.y-(prev2.size.height/2));
            rope2[i].zRotation=M_PI/3;
            rope2[i].name=@"rope2";
            rope2[i].zPosition=100;
            //rope2[i].physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:rope2[i].size];
            rope2[i].physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:rope2[i].size.width/2];
            rope2[i].physicsBody.allowsRotation=NO;
            rope2[i].physicsBody.linearDamping=0.9;
            rope2[i].physicsBody.angularDamping=0.9;
            rope2[i].physicsBody.dynamic=YES;
            rope2[i].physicsBody.affectedByGravity=YES;
            //rope2[i].physicsBody.friction=0;
            rope2[i].physicsBody.resting=YES;
            rope2[i].physicsBody.restitution=0.01;
            rope2[i].physicsBody.mass=9999999999;
            rope2[i].constraints=@[[SKConstraint distance:[SKRange rangeWithLowerLimit:rope2[i].size.width/1.4 upperLimit:rope2[i].size.width/1.4] toNode:prev2]];
            if(i==40-1)
            {
                //SKFieldNode *gravity=[SKFieldNode radialGravityField];
                //gravity.strength=3;
                //gravity.categoryBitMask=gravityCategory;
                //gravity.region=[[SKRegion alloc]initWithRadius:3];
                //[rope2[i] addChild:gravity];
                //rope2[i].size=CGSizeMake(6,6);
            }
            //[self addChild:rope2];
            [self addChild:rope2[i]];

            SKPhysicsJointSpring *j2=[SKPhysicsJointSpring jointWithBodyA:prev2.physicsBody bodyB:rope2[i].physicsBody anchorA:prev2.position anchorB:rope2[i].position];
            //SKPhysicsJointLimit *j2=[SKPhysicsJointLimit jointWithBodyA:prev2.physicsBody bodyB:rope2.physicsBody anchorA:prev2.position anchorB:rope2.position];
            //SKPhysicsJointFixed *j2=[SKPhysicsJointFixed jointWithBodyA:prev2.physicsBody bodyB:rope2[i].physicsBody anchor:prev2.position];
            [self.physicsWorld addJoint:j2];

            prev2=rope2[i];
        }

        //rope1[39].position=CGPointMake(-25,0);
        //rope2[39].position=CGPointMake(25,0);

        //SKPhysicsJointSpring *j=[SKPhysicsJointSpring jointWithBodyA:rope1[39].physicsBody bodyB:rope2[39].physicsBody anchorA:rope1[39].position anchorB:rope2[39].position];
        //[self.physicsWorld addJoint:j];

        //SKPhysicsJointLimit *j=[SKPhysicsJointLimit jointWithBodyA:rope1.physicsBody bodyB:rope2.physicsBody anchorA:rope1.position anchorB:rope2.position];
        //[self.physicsWorld addJoint:j];

        //SKPhysicsJointPin *j=[SKPhysicsJointPin jointWithBodyA:rope1.physicsBody bodyB:rope2.physicsBody anchor:CGPointMake(0,0)];
        //[self.physicsWorld addJoint:j];

        /*rHoopEdge.physicsBody.dynamic=YES;
        rHoopEdge.physicsBody.affectedByGravity=YES;
        SKPhysicsJointSpring *j2=[SKPhysicsJointSpring jointWithBodyA:prev1.physicsBody bodyB:rHoopEdge.physicsBody anchorA:prev1.position anchorB:rHoopEdge.position];
        [self.physicsWorld addJoint:j2];*/

        ropesLeft1=[[NSMutableArray alloc]init];
        NSMutableArray<SKSpriteNode *> *ropesLeft2=[[NSMutableArray alloc]init];
        NSMutableArray<SKSpriteNode *> *ropesLeft3=[[NSMutableArray alloc]init];
        NSMutableArray<SKSpriteNode *> *ropesLeft4=[[NSMutableArray alloc]init];

        for(int i=0;i<20;i++)
        {
            ropesLeft1[i]=[SKSpriteNode spriteNodeWithColor:[UIColor yellowColor] size:CGSizeMake(3, 3)];
            ropesLeft1[i].position=CGPointMake(prev1.position.x+(prev1.size.width/2), prev1.position.y);
            ropesLeft1[i].zRotation=M_PI/3;
            ropesLeft1[i].name=@"ropesLeft1";
            ropesLeft1[i].zPosition=100;
            //ropesLeft1[i].anchorPoint=CGPointMake(ropesLeft1[i].position.x,ropesLeft1[i].position.y);
            //rope[i].constraints=[SKConstraint distance:[SKRange rangeWithUpperLimit:1] toNode:prev1];
            //ropesLeft1[i].physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:ropesLeft1[i].size];
            ropesLeft1[i].physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:ropesLeft1[i].size.width/2];
            ropesLeft1[i].physicsBody.allowsRotation=NO;
            ropesLeft1[i].physicsBody.linearDamping=0.9;
            ropesLeft1[i].physicsBody.angularDamping=0.9;
            ropesLeft1[i].physicsBody.dynamic=YES;
            ropesLeft1[i].physicsBody.affectedByGravity=YES;
            ropesLeft1[i].physicsBody.resting=YES;
            ropesLeft1[i].physicsBody.restitution=0.01;
            if(i==29)
            {
                //ropesLeft1[i].physicsBody.dynamic=NO;
            }
            ropesLeft1[i].physicsBody.mass=9999999999;
            //ropesLeft1[i].physicsBody.friction=0;
            if(i==(20-1))
            {
                //ropesLeft1[i].constraints=@[[SKConstraint distance:[SKRange rangeWithLowerLimit:ropesLeft1[i].size.width/1.4 upperLimit:ropesLeft1[i].size.width/1.4] toNode:rope2[39]]];

                //ropesLeft1[i].constraints=@[[SKConstraint distance:[SKRange rangeWithUpperLimit:ropesLeft1[i].size.width/1.4] toNode:prev1],[SKConstraint positionX:[SKRange rangeWithUpperLimit:rope2[39].position.x] Y:[SKRange rangeWithUpperLimit:rope2[39].position.y]]];
                //ropesLeft1[i].constraints=@[[SKConstraint distance:[SKRange rangeWithUpperLimit:ropesLeft1[i].size.width/1.4] toNode:prev1],[SKConstraint orientToNode:rope2[39] offset:[SKRange rangeWithUpperLimit:rope2[39].size.width/1.4]]];

                /*SKAction *keepLength=[SKAction runBlock:^{
                    ropesLeft1[29].position=CGPointMake(rope2[39].position.x,rope2[39].position.y);
                }];

                [ropesLeft1[29] runAction:[SKAction repeatActionForever:[SKAction sequence:@[keepLength,[SKAction waitForDuration:0.00001]]]]];*/

                //ropesLeft1[i].constraints=@[[SKConstraint distance:[SKRange rangeWithUpperLimit:ropesLeft1[i].size.width/1.4] toNode:prev1],[SKConstraint orientToPoint:CGPointMake(rope2[39].position.x, rope2[39].position.y) inNode:rope2[39] offset:[SKRange rangeWithUpperLimit:ropesLeft1[i].size.width/1.4]]];
                //ropesLeft1[i].constraints=[SKConstraint refer]

                //ropesLeft1[i].physicsBody.affectedByGravity=NO;
                //ropesLeft1[i].physicsBody.fieldBitMask=gravityCategory;
                //ropesLeft1[i].size=CGSizeMake(5,5);

                //SKAction *keepLength=[SKAction moveTo:CGPointMake(rope2[39].position.x,rope2[39].position.y) duration:0.0001];
                //[ropesLeft1[i] runAction:[SKAction sequence:@[keepLength,[SKAction waitForDuration:0.0001]]]];
            }
            //else
            //{
                ropesLeft1[i].constraints=@[[SKConstraint distance:[SKRange rangeWithLowerLimit:ropesLeft1[i].size.width/1.4 upperLimit:ropesLeft1[i].size.width/1.4] toNode:prev1]];
            //}
            [self addChild:ropesLeft1[i]];

            SKPhysicsJointSpring *j=[SKPhysicsJointSpring jointWithBodyA:prev1.physicsBody bodyB:ropesLeft1[i].physicsBody anchorA:prev1.position anchorB:ropesLeft1[i].position];
            //SKPhysicsJointLimit *j=[SKPhysicsJointLimit jointWithBodyA:prev1.physicsBody bodyB:rope[i].physicsBody anchorA:prev1.position anchorB:rope[i].position];
            //SKPhysicsJointPin *j1=[SKPhysicsJointPin jointWithBodyA:prev1.physicsBody bodyB:rope.physicsBody anchor:prev2.position];
            [self.physicsWorld addJoint:j];

            prev1=ropesLeft1[i];
        }

        //rope2[39].position=CGPointMake(30,0);
        ropesLeft1[19].position=CGPointMake(rope2[39].position.x-1,rope2[39].position.y/*-rope2[39].size.height/2*/);

        //ropesLeft1[29].constraints=@[[SKConstraint distance:[SKRange rangeWithUpperLimit:ropesLeft1[29].size.width/1.4] toPoint:CGPointMake(rope2[39].position.x,rope2[39].position.y) inNode:rope2[39]]];
        //ropesLeft1[29].anchorPoint=CGPointMake(rope2[39].position.x,rope2[39].position.y);
        //ropesLeft1[29].anchorPoint=CGPointMake(ropesLeft1[29].position.x,ropesLeft1[29].position.y);

        SKPhysicsJointSpring *j1=[SKPhysicsJointSpring jointWithBodyA:ropesLeft1[19].physicsBody bodyB:rope2[39].physicsBody anchorA:ropesLeft1[19].position anchorB:rope2[39].position];
        j1.frequency=30000;
        j1.damping=0;
        [self.physicsWorld addJoint:j1];

        NSLog(@"%f %f",rope2[39].position.x,rope2[39].position.y);
        NSLog(@"%f %f",ropesLeft1[19].position.x,ropesLeft1[19].position.y);

        //SKPhysicsJointLimit *j2=[SKPhysicsJointLimit jointWithBodyA:ropesLeft1[29].physicsBody bodyB:rope2[39].physicsBody anchorA:ropesLeft1[29].position anchorB:rope2[39].position];
        //[self.physicsWorld addJoint:j2];

        //SKPhysicsJointPin *j3=[SKPhysicsJointPin jointWithBodyA:ropesLeft1[19].physicsBody bodyB:rope2[39].physicsBody anchor:rope2[39].position];
        //j3.shouldEnableLimits=YES;
        //j3.lowerAngleLimit=0;
        //j3.upperAngleLimit=0;
        //j3.frictionTorque=0.1;
        //[self.physicsWorld addJoint:j3];

        //SKPhysicsJointFixed *j4=[SKPhysicsJointFixed jointWithBodyA:ropesLeft1[19].physicsBody bodyB:rope2[39].physicsBody anchor:rope2[39].position];
        //[self.physicsWorld addJoint:j4];

        /*rope2[29].position=CGPointMake(30,0);
        ropesLeft1[39].position=CGPointMake(rope2[29].position.x,rope2[29].position.y);

        SKPhysicsJointSpring *j1=[SKPhysicsJointSpring jointWithBodyA:ropesLeft1[39].physicsBody bodyB:rope2[29].physicsBody anchorA:ropesLeft1[39].position anchorB:rope2[29].position];
        [self.physicsWorld addJoint:j1];*/

        SKSpriteNode *hoop=[SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(109, 7)];
        //hoop.position=CGPointMake(0, -1);
        hoop.position=CGPointMake(0, lHoopEdge.position.y);
        hoop.zPosition=200;
        hoop.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:hoop.size];
        //hoop.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:hoop.size.height/3];
        hoop.physicsBody.affectedByGravity=NO;
        hoop.physicsBody.dynamic=NO;
        //hoop.physicsBody.mass=999999;
        hoop.physicsBody.restitution=0.1;
        hoop.physicsBody.friction=0.4;
        //hoop.physicsBody=nil;
        [self addChild:hoop];
    }
    return self;
}

-(void)hoopEdgesWithSize:(CGSize)size PositionX:(double)positionX PositionY:(double)positionY
{
    //lHoopEdge=[SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(7, 9)];
    lHoopEdge=[SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:size];
    lHoopEdge.position=CGPointMake(-positionX, positionY);
    lHoopEdge.zPosition=100;
    lHoopEdge.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:lHoopEdge.size];
    //lHoopEdge.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:lHoopEdge.size.width/3];
    lHoopEdge.zRotation=M_PI/3;
    lHoopEdge.physicsBody.affectedByGravity=NO;
    lHoopEdge.physicsBody.dynamic=NO;
    lHoopEdge.physicsBody.mass=999999999999999999;
    lHoopEdge.physicsBody.restitution=0.1;
    lHoopEdge.physicsBody.friction=0.4;
    [self addChild:lHoopEdge];

    //rHoopEdge=[SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:CGSizeMake(7, 9)];
    rHoopEdge=[SKSpriteNode spriteNodeWithColor:[UIColor redColor] size:size];
    rHoopEdge.position=CGPointMake(positionX, positionY);
    rHoopEdge.zPosition=100;
    rHoopEdge.physicsBody=[SKPhysicsBody bodyWithRectangleOfSize:rHoopEdge.size];
    //rHoopEdge.physicsBody=[SKPhysicsBody bodyWithCircleOfRadius:rHoopEdge.size.width/3];
    rHoopEdge.zRotation=-M_PI/3;
    rHoopEdge.physicsBody.affectedByGravity=NO;
    rHoopEdge.physicsBody.dynamic=NO;
    rHoopEdge.physicsBody.mass=999999999999999999;
    rHoopEdge.physicsBody.restitution=0.1;
    rHoopEdge.physicsBody.friction=0.4;
    [self addChild:rHoopEdge];

    /*SKPhysicsJointLimit *j=[SKPhysicsJointLimit jointWithBodyA:lHoopEdge.physicsBody bodyB:rHoopEdge.physicsBody anchorA:lHoopEdge.position anchorB:rHoopEdge.position];
     [self.physicsWorld addJoint:j];*/

    /*SKPhysicsJointPin *j=[SKPhysicsJointPin jointWithBodyA:lHoopEdge.physicsBody bodyB:rHoopEdge.physicsBody anchor:lHoopEdge.position];
     [self.physicsWorld addJoint:j];*/

    /*SKPhysicsJointSpring *j=[SKPhysicsJointSpring jointWithBodyA:lHoopEdge.physicsBody bodyB:rHoopEdge.physicsBody anchorA:lHoopEdge.position anchorB:rHoopEdge.position];
     [self.physicsWorld addJoint:j];*/
}

目前,我的预期输出是将黄色绳索和正确的白色绳索连接在一起,而不会留下任何缝隙。但是我的最终产品,即实际的输出,是为篮球设计一个不同于上面链接中的篮球篮。

0 个答案:

没有答案