使用 cocos2d 投掷球,但没有任何物理引擎

发布于 2024-12-29 07:11:44 字数 56 浏览 1 评论 0原文

我想以滑动速度扔球,但不想使用任何物理引擎。所以请有人建议我如何做到这一点。

谢谢

I want to throw a ball with swipe speed but do not want to use any physic engine. so please can any one suggest me how i do this.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜灵血窟げ 2025-01-05 07:11:44

无需显式使用任何物理引擎的一种简单而有效的方法是在更新循环中手动步进球精灵的速度和位置,如欧拉式。

在典型情况下(重力向下),您将在 y 方向上具有非恒定速度,在 x 方向上具有恒定速度,因此代码如下:

-(void) update: (ccTime) dt
{
    // Step only the y-velocity
    velocity_y += GRAVITY * dt; 
    // Step the position values and update the ball sprite position accordingly
    ball.position.x += velocity_x * dt;
    ball.position.y += velocity_y * dt;
}

然后,当检测到滑动事件时,

  1. 捕获滑动速度(您必须计算当前帧和最后一帧中手指触摸位置的变化)
  2. 如有必要,将速度乘以缩放因子。
  3. 将 *velocity_x* 和 *velocity_y* 设置为这些初始值。

A simple yet effective approach without having to explicitly use any physics engine is to step the velocity and position of the ball sprite manually in your update loop, Euler-style.

In the typical case (with downward gravity), you will have non-constant velocity in the y-direction and constant velocity in the x-direction, hence the following code:

-(void) update: (ccTime) dt
{
    // Step only the y-velocity
    velocity_y += GRAVITY * dt; 
    // Step the position values and update the ball sprite position accordingly
    ball.position.x += velocity_x * dt;
    ball.position.y += velocity_y * dt;
}

Then when a swipe event is detected,

  1. Capture the swipe velocity (you will have to compute the change in position of the finger touch in the current and last frame)
  2. Multiply the velocity with a scaling factor if necessary.
  3. Set *velocity_x* and *velocity_y* to these initial values.
依 靠 2025-01-05 07:11:44

我正在使用抛射运动公式来执行此操作, http://en.wikipedia.org/wiki/Projectile_motion< /a>,以及 cocos 操作。这里有一个可能的方法:

  • 首先

实现一个CCActionInterval子类,用于接收弹丸公式参数并更新弹丸位置。在本例中为ProjectileAction。这些是您需要重写的关键方法:

-(void) startWithTarget:(id)target
{
    self.initialPosition = [target position];
    self.elapsedTime = 0;
    [super startWithTarget:target];
}


-(void) update: (ccTime) tt
{   
    self.elapsedTime += tt;
    float t = self.elapsedTime;
    float theta = CC_DEGREES_TO_RADIANS(self.angle);
    float v0 = self.velocity;
    float g = self.gravitationalAcceleration;
    double x = v0 * cos(theta) * t;
    double y = v0 * sin(theta) * t - 0.5 * g * t * t ;
    [self.target setPosition: ccp(self.initialPosition.x + (float)x, self.initialPosition.y + (float)y)];

}
  • 其次,

使用滑动手势来识别速度,这将转换为上面等式中球的初始速度和下面的 theVelocity 参数。我把这部分留给你。

  • 第三次

运行动作。即

    ProjectileLaunch* action = [ProjectileLaunch actionWithDuration:10 
                                                              angle:45
                                                    initialVelocity:theVelocity
                                                                  g:9.8];
    [sprite runAction:action];

我希望这对您有帮助。

问候。

I am using projectile motion formulas to do this, http://en.wikipedia.org/wiki/Projectile_motion, along with cocos actions. Here you have a possible approach:

  • First

Implement a CCActionInterval subclass that receives the projectile formula parameters and updates the projectile position. In this case, ProjectileAction. These are the key methods you need to over-ride:

-(void) startWithTarget:(id)target
{
    self.initialPosition = [target position];
    self.elapsedTime = 0;
    [super startWithTarget:target];
}


-(void) update: (ccTime) tt
{   
    self.elapsedTime += tt;
    float t = self.elapsedTime;
    float theta = CC_DEGREES_TO_RADIANS(self.angle);
    float v0 = self.velocity;
    float g = self.gravitationalAcceleration;
    double x = v0 * cos(theta) * t;
    double y = v0 * sin(theta) * t - 0.5 * g * t * t ;
    [self.target setPosition: ccp(self.initialPosition.x + (float)x, self.initialPosition.y + (float)y)];

}
  • Second

Use your swipe gesture to recognize the speed, that will translate to the ball's initial velocity in the equation above and to the theVelocity parameter bellow. I leave that part to you.

  • Third

Run the action. i.e.

    ProjectileLaunch* action = [ProjectileLaunch actionWithDuration:10 
                                                              angle:45
                                                    initialVelocity:theVelocity
                                                                  g:9.8];
    [sprite runAction:action];

I hope this helps you.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文