SpriteKit:如何创建基本物理关节 [英] SpriteKit: How to create Basic Physics Joints

查看:123
本文介绍了SpriteKit:如何创建基本物理关节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在两个SKPhysicsBodies之间创建简单的关节。但是,他们表现得非常奇怪。
我很清楚锚点应该在场景坐标上。请查看附带的源代码。

I am trying to create simple Joints between two SKPhysicsBodies. But, they are acting weirdly. I am well aware of the fact the anchor points should be on scene coordinate. Please have a look at the Source code attached.

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

For Example this is how a fixed Joint results after attaching a small square on a rectangle.

-(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];

}

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

谢谢。

推荐答案

谢谢Smick ..将Smick的代码与我的比较后我发现了顺序这两行引起了这个问题。

Thank you Smick.. After Comparing Smick's code with mine I found out the order of these two lines are causing the issue.

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

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

When I set the position of the Sprite before Setting its physics body, everything started to work correctly.

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

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

Now I have attached Smick's code also to the full code and attached the link down here. Enjoy.

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

这篇关于SpriteKit:如何创建基本物理关节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆