1
\$\begingroup\$

I'm new to game development and I'm trying to build my first Pong game on the Godot engine. the tutorial I'm following is given below.

Simple Pong Game

What I don't understand is the line:

ball_pos += direction * ball_speed * delta

How is the new ball position being calculated?

As far as I know, delta is the time elapsed in seconds (float) since the last _process() call.

Also, why is the direction vector set to (1.0, 0.0)?

I'm looking for a clear explanation since the tutorial doesn't explain much. Thank you.

\$\endgroup\$
  • \$\begingroup\$ Direction * Speed * DeltaTime = Velocity * DeltaTime = DeltaPosition \$\endgroup\$ – S. Tarık Çetin Jul 10 '17 at 19:08
1
\$\begingroup\$

The += operator adds the right hand operand to the left hand operand. So the line is equivalent to

ball_pos = ball_pos + direction*ball_speed*delta

So direction*ball_speed*delta is the offset of the new position compared to the old position. It's the distance the ball travelled during this frame. direction*ball_speed is the speed of the ball combined with its direction, so it's a vector. Direction is a so called unit vector, so its length is one. This represents the velocity of the ball in (some distance-unit)/second. If you multiply this by the time that passed since the last update you have the change in position in this frame.

To learn more about this I recommend researching the basics of linear algebra.

\$\endgroup\$
0
\$\begingroup\$

Mostly assumptions based off what I've seen for similar variables and formulas, but ball_pos is probably a vector (so it has a direction, hence multiplying by direction to get it's direction). ball_speed is likely velocity, so you need to know how far it travels/elapsed time, and delta is likely the elapsed time between calculations.

That with the += basically says where was I, what direction did I go in and how fast did I go over the previous time frame, now where did that get me to.

Direction is set to (#, 0) because presumably it's not an x, y, z direction but just x, basically is it facing left or right (or up and down depending on what movement you're looking for).

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.