gpt4 book ai didi

ios - Sprite Kit - 应用 Impulse 向角色发射炮弹

转载 作者:可可西里 更新时间:2023-11-01 04:23:24 39 4
gpt4 key购买 nike

我正在使用 Sprite-Kit (Objective-C) 开发游戏。在这个游戏中,你控制一只飞翔的鸟,然后从屏幕的右/上/下侧向你射出箭和其他坏射弹。我正在使用物理学而不是 SKAction 来实现这一点,因为我希望它看起来尽可能逼真。所以我知道在射弹上使用 applyImpulse 将它射向鸟,但我想知道的是如何保证射弹将直接射向鸟,无论鸟的 y 定位和 y 定位applyImpulse 之前的射弹?

我在完成这项工作时遇到了非常令人沮丧的事情,因此非常感谢对此问题的任何帮助。谢谢。

最佳答案

基本步骤是

  1. 计算从射弹发射器到鸟的矢量分量
  2. 标准化组件(可选)
  3. 通过缩放(归一化)分量创建向量
  4. 使用矢量向射弹施加冲量

这是一个如何做到这一点的例子

Obj-C

// Calculate vector components x and y
CGFloat dx = bird.position.x - launcher.position.x;
CGFloat dy = bird.position.y - launcher.position.y;

// Normalize the components
CGFloat magnitude = sqrt(dx*dx+dy*dy);
dx /= magnitude;
dy /= magnitude;

// Create a vector in the direction of the bird
CGVector vector = CGVectorMake(strength*dx, strength*dy);

// Apply impulse
[projectile.physicsBody applyImpulse:vector];

swift

// Calculate vector components x and y
var dx = bird.position.x - launcher.position.x
var dy = bird.position.y - launcher.position.y

// Normalize the components
let magnitude = sqrt(dx*dx+dy*dy)
dx /= magnitude
dy /= magnitude

// Create a vector in the direction of the bird
let vector = CGVector(dx:strength*dx, dy:strength*dy)

// Apply impulse
projectile.physicsBody?.applyImpulse(vector)

关于ios - Sprite Kit - 应用 Impulse 向角色发射炮弹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26364921/

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