Я пытаюсь использовать разные подклассы skphysicsjoint, чтобы соединить кончики двух веревок, но между двумя веревками все еще остается небольшой разрыв, который увеличивается с добавлением веса или объектов независимо от того, что я пытаюсь использовать, чтобы закрыть разрыв .
В коде есть 3 веревки (веревка 1 - левая белая веревка, веревка 2 - правая белая веревка, веревки - левая желтая нижняя веревка).
Я использовал [SKConstraint distance ..], чтобы скрепить все сегменты каждой веревки, которые раньше тоже не работали должным образом (растяжение), используя классы skphysicsjoint.
Ниже приведены мои коды, включая все методы, которые я также попробовал и закомментировал. Я искал существующие вопросы переполнения стека, связанные с этой темой, но ни один из них не отвечал моим потребностям, за исключением этой ссылки: Sprite Kit SKPhysicsJoint
Существует код хорошо спроектированной корзины, но проблема в том, что сегмент SKAction внутри кода не позволяет приложению реагировать на такие методы, как touchesBegan, touchesEnded, что странно, потому что SKAction никогда ранее не блокировал сенсорные методы в других моих игровых приложениях. разработали.
Я знаю, что мои коды не являются чистыми и стандартными, но сейчас моя цель - достичь своей цели, а затем очистить коды.
#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];*/
}
На данный момент мой ожидаемый результат - соединить желтую веревку и правую белую веревку вместе, не оставляя пробелов. Но мой конечный продукт, фактический результат, заключается в создании корзины для игры в баскетбол, отличной от той, что указана в ссылке выше.