gpt4 book ai didi

objective-c - SpriteKit : How to create Basic Physics Joints

转载 作者:太空狗 更新时间:2023-10-30 03:26:09 25 4
gpt4 key购买 nike

我正在尝试在两个 SKPhysicsBodies 之间创建简单的关节。但是,他们的行为很奇怪。我很清楚 anchor 应该在场景坐标上。请查看随附的源代码。

例如,这是在矩形上附加一个小正方形后固定关节的结果。

-(void)createFixedJointOnScene:(SKScene*)scene

{

//Adding Rectangle

SKSpriteNode* backBone = [[SKSpriteNode alloc] initWithColor:[UIColor whiteColor] size:CGSizeMake(20, 200)];
backBone.position = CGPointMake(CGRectGetWidth(self.frame)/2.0, CGRectGetHeight(self.frame)/2.0);
backBone.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:backBone.size];
backBone.physicsBody.categoryBitMask = GFPhysicsCategoryRectangle;
backBone.physicsBody.collisionBitMask = GFPhysicsCategoryWorld;
[scene addChild:backBone];

//Adding Square
SKSpriteNode* head = [[SKSpriteNode alloc] initWithColor:[SKColor grayColor] size:CGSizeMake(40, 40)];
head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:head.size];
head.position = CGPointMake(backBone.position.x, backBone.position.y-40);
head.physicsBody.categoryBitMask = GFPhysicsCategorySquare;
head.physicsBody.collisionBitMask = GFPhysicsCategoryWorld;
[scene addChild:head];

//Pinning Rectangle and Square
NSLog(@"Head position %@", NSStringFromCGPoint(head.position));
SKPhysicsJointFixed* pin =[SKPhysicsJointFixed jointWithBodyA:backBone.physicsBody bodyB:head.physicsBody anchor:head.position];
[self.physicsWorld addJoint:pin];

enter image description here

https://dl.dropboxusercontent.com/u/62559842/PhysicsTest.zip

谢谢。

最佳答案

谢谢 Smick。将 Smick 的代码与我的代码进行比较后,我发现这两行的顺序导致了问题。

head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:head.size];
head.position = CGPointMake(backBone.position.x, backBone.position.y-40);

当我在设置其物理体之前设置 Sprite 的位置时,一切都开始正常工作。

head.position = CGPointMake(backBone.position.x, backBone.position.y-40);
head.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:head.size];

现在我已将 Smick 的代码也附加到完整代码中,并将链接附在此处。享受吧。

https://dl.dropboxusercontent.com/u/62559842/PhysicsTest_Final_Working.zip

关于objective-c - SpriteKit : How to create Basic Physics Joints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19343459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com