Box2D (Cocos2D) 随时间变化的恒定力

发布于 2024-11-15 05:48:34 字数 2027 浏览 8 评论 0原文

我是一个相当聪明的人,但当我看到某种数学时,我可能会变成一个大白痴。我真的需要一些帮助。

当我学习 iOS 游戏开发时,我一直在研究很多东西,并且在进行一些搜索时发现了一个公式。公式如下:

x(t) = x(0) + v(0)*t + .5 (F/m) * t^2

还说明了求解 x 和 y:

Fx = (x(t) - x(0) - vx(0)*t) * 2m/t^2

Fy = (y(t) - y(0) - vy(0)*t) * 2m/t^2

来源:Box2D.org论坛

现在我的实际问题是,这是什么意思?请记住,在这种情况下我是个白痴。如果有人能用简单的术语解释这些变量以及它们与 box2d 的关系,那就太好了。我该如何应用这个公式?这是我的代码示例(发射射弹):

- (void)spawnProjectile:(CGPoint)from direction:(CGPoint)direction inParent:(CCNode*)parentNode
{
    double curTime = CACurrentMediaTime();
    
    double timeTillShotDies = curTime + SHOT_TYPE1_EXIST_TIME;
    
    b2Body *shotBody = projectileBodyTracker[nextShot];
    
    b2Vec2 moveToPosition = b2Vec2(from.x/PTM_RATIO, from.y/PTM_RATIO);
    shotBody->SetTransform(moveToPosition, 0.0);
    shotBody->SetActive(true);
    
    CCSprite *shot = [projectiles objectAtIndex:nextShot];
    shot.position = ccp(from.x/PTM_RATIO, from.y/PTM_RATIO);

    shot.visible = YES;
    [projectilesTracker replaceObjectAtIndex:nextShot withObject:[NSNumber numberWithDouble:timeTillShotDies]];
    CCParticleSystemQuad *particle = [projectileParticleTracker objectAtIndex:nextShot];
    [particle resetSystem];
    nextShot++;
    if(nextShot >= projectiles.count) nextShot = 0;
    
    // dx2 and dy2 are analog stick values (see below code)
    b2Vec2 force = b2Vec2(dx2, dy2);
    shotBody->SetLinearVelocity(force);
     
    [AudioController playLaserShot];
}

在这段特定的代码中,我以模拟所在的角度从玩家发射。我还需要使用公式从敌人向玩家开火。这是一款自上而下的太空射击游戏。

总而言之,如何用 box2d 代码求解 x 和 y 随时间变化的恒定力?

额外信息:

dx2 = (float)joypadBG2.position.x - (float)convertedPoint.x;
dy2 = (float)joypadBG2.position.y - (float)convertedPoint.y;

所有对象都已预加载并保持这种状态。身体被设置为非活动状态,精灵被设置为不可见。粒子系统停止。再次使用射弹时则相反。

非常感谢您提供的任何帮助。我希望我没有忘记任何事情。

I am a fairly intelligent person, but when I see a certain kind of math I might as well be a gigantic moron. I could really use some help here.

I have been researching a ton of things as I learn iOS game development and I came across a formula while doing some searches. Here is the formula:

x(t) = x(0) + v(0)*t + .5 (F/m) * t^2

Also stated was solving for x and y:

Fx = (x(t) - x(0) - vx(0)*t) * 2m/t^2

Fy = (y(t) - y(0) - vy(0)*t) * 2m/t^2

Source: Box2D.org forums

Now for my actual question, what does that mean? Keep in mind that in this situation I am an idiot. It would be great if someone could explain the variables in simple terms and how they relate to box2d. How would I apply this formula? Here is an example of my code (firing projectiles):

- (void)spawnProjectile:(CGPoint)from direction:(CGPoint)direction inParent:(CCNode*)parentNode
{
    double curTime = CACurrentMediaTime();
    
    double timeTillShotDies = curTime + SHOT_TYPE1_EXIST_TIME;
    
    b2Body *shotBody = projectileBodyTracker[nextShot];
    
    b2Vec2 moveToPosition = b2Vec2(from.x/PTM_RATIO, from.y/PTM_RATIO);
    shotBody->SetTransform(moveToPosition, 0.0);
    shotBody->SetActive(true);
    
    CCSprite *shot = [projectiles objectAtIndex:nextShot];
    shot.position = ccp(from.x/PTM_RATIO, from.y/PTM_RATIO);

    shot.visible = YES;
    [projectilesTracker replaceObjectAtIndex:nextShot withObject:[NSNumber numberWithDouble:timeTillShotDies]];
    CCParticleSystemQuad *particle = [projectileParticleTracker objectAtIndex:nextShot];
    [particle resetSystem];
    nextShot++;
    if(nextShot >= projectiles.count) nextShot = 0;
    
    // dx2 and dy2 are analog stick values (see below code)
    b2Vec2 force = b2Vec2(dx2, dy2);
    shotBody->SetLinearVelocity(force);
     
    [AudioController playLaserShot];
}

In this particular chunk of code I am firing from the player at the angle the analog is at. I also would need to use the formula to fire from the enemy to the player. This is a top down space shooter.

So to summarize, how do I solve constant force over time for x and y, in terms of box2d code?

Extra info:

dx2 = (float)joypadBG2.position.x - (float)convertedPoint.x;
dy2 = (float)joypadBG2.position.y - (float)convertedPoint.y;

All objects are preloaded and kept that way. Bodies are set inactive and sprites set invisible. Particle systems are stopped. The opposite is true for using a projectile again.

Thank you very much for any help you may be able to provide. I hope I haven't forgotten anything.

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

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

发布评论

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

评论(2

风向决定发型 2024-11-22 05:48:34

第一个方程描述了受到恒定力作用的物体的运动。
物体从位置 x(0) 开始,速度为 v(0)。 x 和 v 都是向量,因此在 2d 射击游戏中,x(0) 将为 (x0,y0) 或 xy 位置,而 v(0) 将为 (vx0, vy0)。

如果没有重力,则无推进弹丸(没有推进器的弹丸)上的 F=0,
所以速度将是恒定的。

x(t1) = x(t0) + vx * (t1-t0)
y(t1) = y(t0) + vy * (t1-t0)

t1-t0 或 dt (delta-t) 是自上次更新射弹位置以来经过的时间。

如果物体或重力对物体施加力,那么速度将随着时间而变化。

vx(t1) = vx(t0) + ax * (t1-t0)
vy(t1) = vy(t0) + ay * (t1-t0)

a 是加速度。在游戏中,您通常不关心质量和力,只关心加速度。在物理学中,a = F/m。

编辑1:
在电脑游戏中,您会非常频繁地更新对象的位置(通常每秒大约 60 次)。您拥有上次更新时对象的位置和速度,并且想要计算新位置。

您可以通过假设速度恒定来更新位置:

positionVectorAt(newTime) = positionVector(lastTime) + velocityVector*(newTime - lastTime);

如果对象的速度发生变化,您也可以更新速度:

velocityVectorAt(newTime) = velocityVector(lastTime) + accelerationVector*(newTime - lastTime);

假设我们有一个精灵

positionVector.x=100; 
positionVector.y=10;

初始速度为

velocityVector.x = 3;
velocityVector.y = -10;

精灵正在使用推进器,每秒给出水平加速度 并且它还受到重力

thrusterVector.x= 5; 

的影响,从而产生每秒的垂直加速度

gravityVector.y = -10;

更新精灵位置的代码将是:

deltaTime = now - lastTime; // Time elapsed since last position update

// Update the position
positionVector.x = positionVector.x + velocityVector.x * deltaTime;
positionVector.y = positionVector.y + velocityVector.y * deltaTime;

// Update the velocity
velocityVector.x = velocityVector.x + (thrusterVector.x + gravityVector.x) * deltaTime;
velocityVector.y = velocityVector.y + (thrusterVector.y + gravityVector.y) * deltaTime;

// Done! The sprite now has a new position and a new velocity!

The first equation describes the movement of an object that is subject to a constant force.
The object starts at position x(0) and has speed v(0). Both x and v are vectors, so in a 2d shooter, x(0) would be (x0,y0), or the xy-position, and v(0) would be (vx0, vy0).

If there is no gravity then F=0 on unpropelled projectiles (projectiles without thrusters),
so the velocity will be constant.

x(t1) = x(t0) + vx * (t1-t0)
y(t1) = y(t0) + vy * (t1-t0)

t1-t0 or dt (delta-t) is the time elapsed since the last time you updated the position of the projectile.

If thusters or gravity are excerting force on an object then the velocity will change over time.

vx(t1) = vx(t0) + ax * (t1-t0)
vy(t1) = vy(t0) + ay * (t1-t0)

a is the acceleration. In a game you usually don't care about mass and force, just acceleration. In physics, a = F/m.

Edit 1:
In computer games, you update the position of an object very frequently (typically around 60 times per second). You have the position and velocity of the object at the previous update and you want to calculate the new position.

You update the position by assuming that the velocity was constant:

positionVectorAt(newTime) = positionVector(lastTime) + velocityVector*(newTime - lastTime);

If the velocity of the object is changed you also update the velocity:

velocityVectorAt(newTime) = velocityVector(lastTime) + accelerationVector*(newTime - lastTime);

Let's say we have a sprite at

positionVector.x=100; 
positionVector.y=10;

The initial speed is

velocityVector.x = 3;
velocityVector.y = -10;

The sprite is using thrusters which is giving a horizonal acceleration per second of

thrusterVector.x= 5; 

and it is also subject to gravity which gives a vertical acceleration per second of

gravityVector.y = -10;

The code to update the sprites position will be:

deltaTime = now - lastTime; // Time elapsed since last position update

// Update the position
positionVector.x = positionVector.x + velocityVector.x * deltaTime;
positionVector.y = positionVector.y + velocityVector.y * deltaTime;

// Update the velocity
velocityVector.x = velocityVector.x + (thrusterVector.x + gravityVector.x) * deltaTime;
velocityVector.y = velocityVector.y + (thrusterVector.y + gravityVector.y) * deltaTime;

// Done! The sprite now has a new position and a new velocity!
黑凤梨 2024-11-22 05:48:34

这是一个快速解释:

x(t) = x(0) + v(0)*t + .5 (F/m) * t^2
Fx = (x(t) - x(0) - vx(0)*t) * 2m/t^2
Fy = (y(t) - y(0) - vy(0)*t) * 2m/t^2

这 3 个方程是标准运动方程:

  • t:时间
  • x(t):时间 t 的位置
  • v(t):时间 t 时的速度
  • vx(t):时间 t 时速度的水平分量
  • vy (t):时间 t 时速度的垂直分量
  • m:质量
  • F:力
  • Fx:力的水平分量
  • Fy:力的垂直分量

So of当然,每次看到x(0)vy(0)时,这些值都是在时间t获取的,即它们是初始值 。这些方程是带有基本电影变量(位置、速度、力、质量)的基本电影方程

Here is a quick explanation:

x(t) = x(0) + v(0)*t + .5 (F/m) * t^2
Fx = (x(t) - x(0) - vx(0)*t) * 2m/t^2
Fy = (y(t) - y(0) - vy(0)*t) * 2m/t^2

These 3 equations are standard movement ones:

  • t: time
  • x(t): position at time t
  • v(t): speed at time t
  • vx(t): horizontal component of speed at time t
  • vy(t): vertical component of speed at time t
  • m: mass
  • F: force
  • Fx: horizontal component of the force
  • Fy: vertical component of the force

So of course, each time you see x(0) or vy(0), these values are taken at time t, i.e. they are initial values. These equations are basic cinematic equations with the basic cinematic variables (position, speed, force, mass).

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