gpt4 book ai didi

ios - SpriteKit - 在关节之间绘制动态线

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:28:50 27 4
gpt4 key购买 nike

我正在尝试使用内置物理学的 iOS SpriteKit 以编程方式构建一个钟摆。

目前,我有摆枢轴、一个重物和一个允许重物摆动的有限关节……但是,我不知道如何在枢轴和重物之间编写一条线(杆)。

我假设用 SKShapeNode 画一条线会是一个开始......?

-(void)setupPendulum
{
pivot = [SKSpriteNode spriteNodeWithImageNamed:@"pivot.png"];
pivot.position = CGPointMake(self.size.width / 2, self.size.height / 2);
pivot.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:1.0];
pivot.physicsBody.dynamic = NO;
pivot.physicsBody.affectedByGravity = NO;
pivot.xScale = 0.25;
pivot.yScale = 0.25;
[self addChild:pivot];

weight = [SKSpriteNode spriteNodeWithImageNamed:@"weight.png"];
weight.position = CGPointMake(150, 512);
weight.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:100.0];
weight.physicsBody.dynamic = YES;
weight.physicsBody.affectedByGravity = YES;
weight.physicsBody.mass = 0.5;
weight.xScale = 0.75;
weight.yScale = 0.75;
[self addChild:weight];

SKPhysicsBody *ab = pivot.physicsBody;
SKPhysicsBody *bb = weight.physicsBody;
CGPoint ap = pivot.position;
CGPoint bp = weight.position;
SKPhysicsJointLimit *joints = [SKPhysicsJointLimit jointWithBodyA:ab
bodyB:bb
anchorA:ap
anchorB:bp];
[self.physicsWorld addJoint:joints];
}

最佳答案

我的项目需要 SKPhysicsJointLimit 而不是引脚,因此经过大量试验和错误后,我找到了以下解决方案。在 didSimulatePhysics 中移除并绘制该线。这条线连接着“卫星船”,因为它绕着“母舰”运行。我是这一切的新手,所以我很感激任何对此方法的反馈。

首先设置变量:

SKShapeNode *_lineNode;

现在在 didSimulatePhysics 中画线:

- (void)didSimulatePhysics {
if (_lineNode){
[_lineNode removeFromParent];
}
SKNode *satelliteShip = [self childNodeWithName:kSatelliteShipName];
CGMutablePathRef pathToDraw;
pathToDraw = CGPathCreateMutable();
CGPathMoveToPoint(pathToDraw, NULL, motherShip.position.x, motherShip.position.y);
CGPathAddLineToPoint(pathToDraw, NULL, satelliteShip.position.x, satelliteShip.position.y);
CGPathCloseSubpath(pathToDraw);

_lineNode = [SKShapeNode node];
_lineNode.path = pathToDraw;
CGPathRelease(pathToDraw);
_lineNode.strokeColor = [SKColor grayColor];
[self addChild:_lineNode];
}

关于ios - SpriteKit - 在关节之间绘制动态线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21417577/

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