SpriteKit 物理应用方向的脉冲 [英] SpriteKit physics applyImpulse in direction

查看:54
本文介绍了SpriteKit 物理应用方向的脉冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 spriteKit-s 物理引擎.我在屏幕上有 2 个对象,(比如玩家和敌人).现在我正在尝试对敌人使用 applyImpulse 方法,该方法应该将其拉向玩家.

I am using the spriteKit-s physics engine. I have 2 objects on the screen, (say player and enemy). Now I am trying to use the applyImpulse method on enemy, which is supposed to pull it towards the player.

CGFloat radians = [Utilites pointPairToBearingDegrees:self.enemy.position 
                                          secondPoint:self.player.position];
CGVector vector = CGVectorMake(500*cosf(radians), 500*sinf(radians));
[self.enemy.physicsBody applyImpulse:vector];

然而,这会在奇怪的方向上施加力.根本不连贯.有人知道为什么吗?

However this applies force in wierd directions. Not consistent at all. Anyone have a clue why?

附言.弧度计算正确!!!

Ps. the radians are calculated correctly!!!

谢谢

推荐答案

试试这个...

CGFloat dx = self.player.position.x - self.enemy.position.x;
CGFloat dy = self.player.position.y - self.enemy.position.y;

CGFloat mag = sqrt(dx*dx+dy*dy);

CGVector vector = CGVectorMake(dx/mag*kImpulseScale, dy/mag*kImpulseScale);
[self.enemy.physicsBody applyImpulse:vector];

这篇关于SpriteKit 物理应用方向的脉冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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