gpt4 book ai didi

ios - SpriteKit 接头

转载 作者:行者123 更新时间:2023-12-01 19:03:49 28 4
gpt4 key购买 nike

我正在尝试制作一个动画角色,可以在不移动他的躯干的情况下伸手去拿屏幕上的东西。

如果他够不到,他的手应该朝这个方向移动最大量,然后被他的静态躯干的物理限制所抵消。

problem

所以在我的代码中,绿色矩形( hand )在我点击的地方移动,躯干随之移动。
我希望能够使红色矩形( 主体 )静止,并使绿色矩形“点”在触摸点位置。

我试图在场景和红体矩形之间应用一个固定的关节,但这似乎不起作用。

- (void) createSceneContent
{
SKSpriteNode *body = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(100, 100)];
body.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:body];

body.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:body.size];
body.physicsBody.affectedByGravity = NO;
body.physicsBody.dynamic = YES;

self.hand = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(150, 20)];
self.hand.position = CGPointMake(body.position.x + body.size.width / 2 + self.hand.size.width / 2, body.position.y);
[self addChild:self.hand];
self.hand.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.hand.size];
self.hand.physicsBody.dynamic = NO;

self.scene.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

[self.physicsWorld addJoint:[SKPhysicsJointPin jointWithBodyA:body.physicsBody bodyB:self.hand.physicsBody anchor:CGPointMake(body.position.x + body.size.width / 2, body.position.y)]];

[self.hand runAction:[SKAction moveByX:100 y:10 duration:0.1]];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];

CGPoint location = [touch locationInNode:self];
[self.hand runAction:[SKAction moveTo:location duration:0.1]];
}

最佳答案

首先,如果您只希望 ARM 指向触摸的位置,您实际上并不需要在这里使用物理。
只需更改 arm 节点的 anchorPoint到肩部(将销钉放在关节上的位置)并围绕该点旋转它(该点保存在辅助肩部节点中,以便稍后转换为它的坐标)。您可以使用 atan2f 计算旋转角度.这是代码:

- (void) createSceneContent
{
SKSpriteNode *body = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(100, 100)];
body.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:body];

self.hand = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(150, 20)];
self.hand.position = CGPointMake(body.position.x + body.size.width*0.5, body.position.y);
self.hand.anchorPoint = CGPointMake(0, 0.5);
[self addChild:self.hand];

SKNode *shoulderNode = [SKNode node];
shoulderNode.position = self.hand.position;
shoulderNode.name = @"shoulderNode";
[self addChild:shoulderNode];

}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];

CGPoint location = [touch locationInNode:self];
CGPoint locationConv = [self convertPoint:location toNode:[self childNodeWithName:@"shoulderNode"]];

self.hand.zRotation = (atan2f(locationConv.y, locationConv.x));
}

另一方面,如果您需要在这里使用物理实体,使用 SKActions 移动 ARM 可能不是一个好主意,您可能还需要设置 body.physicsBody.dynamic = NO使躯干静止。然后,确保正确添加销接头,并将其frictionTorque 属性设置为较高的值以减少悬空。使手点位于正确位置的方法之一是在更新方法中设置它的速度矢量 - 只需使用转换后的位置坐标(这里它们是相对于 body 节点的中心)。完整示例代码:
- (void) createSceneContent {
self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame];

body = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(100, 100)];
body.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[self addChild:body];

body.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:body.size];
body.physicsBody.dynamic = NO;
body.physicsBody.affectedByGravity = YES;

self.hand = [SKSpriteNode spriteNodeWithColor:[SKColor greenColor] size:CGSizeMake(20, 150)];
// experiment with the anchorPoint for different results
self.hand.anchorPoint = CGPointMake(0.5, 0);
self.hand.position = CGPointMake(body.position.x, body.position.y);
[self addChild:self.hand];

self.hand.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:self.hand.size];
self.hand.physicsBody.dynamic = YES;
self.hand.physicsBody.affectedByGravity = NO;

SKPhysicsJointPin *pinHand = [SKPhysicsJointPin jointWithBodyA:body.physicsBody bodyB:self.hand.physicsBody anchor:CGPointMake(body.position.x, body.position.y-5)];
[self.physicsWorld addJoint:pinHand];
// experiment with the friction torque value to achieve desired results
pinHand.frictionTorque = 1.0;
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
location = [touch locationInNode:self];
}

-(void)update:(CFTimeInterval)currentTime {
CGPoint locationConv = [self convertPoint:location toNode:body];
// experiment with the multiplier (here: 10) for faster/slower movement
self.hand.physicsBody.velocity = CGVectorMake(locationConv.x*10, locationConv.y*10);
}

希望有帮助!

关于ios - SpriteKit 接头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21124203/

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