0
votes

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.

2

2 Answers

1
votes

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!
1
votes

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).