gpt4 book ai didi

ios - 使用 SKPhysics(碰撞)拖动 SKSpriteNode(跟随你的手指移动)

转载 作者:可可西里 更新时间:2023-11-01 05:53:52 26 4
gpt4 key购买 nike

想要使用 SKPhysics(启用碰撞)拖动 SKSpriteNode(跟随您的手指移动)

2 失败的解决方案:

1)

a) 解决方案:改变SKSpriteNode的位置

b) 问题:当用户拖动 spriteNode 快速穿过墙壁时,SKSpriteNode 会“神奇地”穿过墙壁(Physics 似乎不起作用)

c)代码:

@property (nonatomic) CGPoint translationBy;

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

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPoint previousPosition = [touch previousLocationInNode:self];

CGPoint translation = CGPointMake(positionInScene.x - previousPosition.x, positionInScene.y - previousPosition.y);

[self panForTranslation:translation];
}

- (void)panForTranslation:(CGPoint)translation {
CGPoint position = [_selectedNode position];
if([[_selectedNode name] isEqualToString:PLAYER_NAME]) {
[_selectedNode setPosition:CGPointMake(position.x + translation.x, position.y + translation.y)];
}
}

2) 在这里搜索后找到了这个 advice by @LearnCocos2D “当你使用物理时,坚持通过力、脉冲或直接改变 body 速度来移动物理 body 。”所以我尝试了:

a) 解决方案:在 Sprite 节点上施加脉冲。

b) 问题:即使我没有拖动 Sprite 节点, Sprite 节点也会继续移动。 (表现得像一艘漂浮的船。期望它表现得像一辆汽车,一旦拖动停止, Sprite 节点将停止;一旦拖动开始,它将跟随手指。)

c) 代码:

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

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPoint previousPosition = [touch previousLocationInNode:self];

CGPoint translation = CGPointMake(positionInScene.x - previousPosition.x, positionInScene.y - previousPosition.y);

CGVector forceVector = CGVectorMake(translation.x, translation.y);
[self.player.physicsBody applyImpulse:forceVector];
//[self.player.physicsBody setVelocity:CGVectorMake(0, 0)];

}

-->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

更新:

来自评论:“i)您可以确定节点到手指位置的距离,然后在每次更新时设置速度:以便下一帧它准确地位于手指的位置。ii)如果手指和节点位置相同,这将取消避免任何移动,否则节点会在执行所有物理碰撞时粘在你的手指上,并在手指被阻挡的情况下继续插入自身和手指之间的物体。”

a) 问题:如何禁用轻弹?

b) 问题: Sprite 节点会因轻微(意外)轻弹而移动到很远的地方。

c)代码:

@property (nonatomic) CGPoint translationBy;

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
CGPoint previousPosition = [touch previousLocationInNode:self];
// 1) You could determine the distance of the node to the finger location,

CGPoint xyTranslation = CGPointMake(positionInScene.x - previousPosition.x, positionInScene.y - previousPosition.y);
if (fabsf(positionInScene.x - previousPosition.x) < kPlayerMovementThreshold) {
xyTranslation.x = 0;
}
if (fabsf(positionInScene.y - previousPosition.y) < kPlayerMovementThreshold) {
xyTranslation.y = 0;
}
self.translationBy = xyTranslation;
}

static const CGFloat kPlayerMovementSpeed = 55.0f;

static const CGFloat kPlayerMovementThreshold = 1.0f;

-(void)update:(CFTimeInterval)currentTime{
// ii) then set the velocity every update: so that it will exactly be on the finger's position the next frame.

CGFloat dx = self.translationBy.x*kPlayerMovementSpeed;
CGFloat dy = self.translationBy.y*kPlayerMovementSpeed;
CGVector velocityVector = CGVectorMake(dx,dy);
[self.player.physicsBody setVelocity:velocityVector];
}

最佳答案

我所做的是制作一个 SKNode,我们称它为“a”,并附有一个非常小的物理体。这个新节点通过 SKPhysicsJointSpring 连接到我要移动的节点,我们称之为“b”。新节点 a 跟踪我的手指移动, Spring 确保目标节点 b 跟随它。因为节点 b 使用物理定位,所以碰撞检测按预期工作。

关于ios - 使用 SKPhysics(碰撞)拖动 SKSpriteNode(跟随你的手指移动),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24762582/

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