1
\$\begingroup\$

Assume we have a player on a map, whereby the grid is represented as a floating point.

When a player is moving, they have an x and y velocity.

How to do you properly apply their movement each gametic?

By that I mean: do you keep nudging the player forward by one map unit and repeat that by the velocity? Where is that fine line where I say "we don't go any smaller movements than this like 0.5, or 0.25, etc"

Further, is there a way to optimize this to be able to make movement fast computationally? I thought of having some kind of blocks where you can quickly check if you'd intersect anything and make some optimizations, but I have no idea if this is good, bad, or how it should be done.

Update: Suppose you have a projectile traveling 100 map units North, and it would collide with an object 50 map units North of it. In the period of one world 'ticking', would you just halt it in a dead on collision (assuming the line is impassible), or would you detect a collision and then do something after it since that remaining 100 map unit velocity (like maybe if it's on an angle then cancel its Northward momentum and continue trying to move it to the East/West.

\$\endgroup\$
  • \$\begingroup\$ Im not sure I understood your question exactly, if I could not give you the right answer let me know. \$\endgroup\$ – eldo Jul 5 '16 at 9:41
2
\$\begingroup\$

It s a bit unclear what you are asking, in particular what you mean by "gametic".

A computer game re-paints the screen repeatedly. Between two re-paints you have a delta time T, typically 0.01...0.03 s. Say it is 0.02 s this time - note however that it fluctuates.

Then you can calculate the physics & movement over a single calculation, to which you give 0.02 s as time.

An alternative is to do the calculation at a finer granularity, for example 0.005 s. In that case, there would be 4 re-calculations, since 4*0.005 = 0.02. Hence, each physics re-calc uses timedelta=0.005, updates/re-positions the objects (but not visually, only in data), does the same again, again, 4 times or as many times as your pre-determined "fine granularity physics timestep" fits into frame timedelta T.

As these 4 iterations are completed, you paint the screen upon "what data you have now". If there was any unused time, eg. if frame time delta was 0.022, then you save 0.002 s for the next cycle/frame.

Ie. you check how many 0.005's fit into T, do the physics iterations that many times and pass the leftover to next frame. If that frame (or say "main game loop cycle") took 0.023 s, then you do the physics calc using 5 iterations, as 0.023 + 0.002 = 0.025 and 0.005 fits 5 times in 0.0025.

The amount of physics re-calcs per painted frame depends on the overall burden. Usually games are in a hurry.

As a comment on your update text: That is totally opinion-based, there is no "would" or "would not". Physics calculation at a fine granularity gives accuracy and detail, at a rough granularity gives speed. If it is essential that the physics behaviour is exact, then apparently do it at a fine granularity. This has nothing to do with frame painting. A frame paint is a "snapshot of the current situation". You can re-paint as seldom as once a day, though that would be a bad user experience. If you only do one physics re-calc during that day, well don't expect the car to have changed course from the initial. But if you do a trillion invisible physics re-calculations during the day, then the car will be at expected location when you do a screen re-paint next day. Physics calc does not have to be in sync with real world elapsed time.

\$\endgroup\$
1
\$\begingroup\$

In nutshell your a movement would look something like this: speedX speedY for vertical and horizontal speed. You have a velocityX and velocityY for example. Lets say its a top down game.

And you have your positions like posX and posY. Every tick you update your positions:

posX += speedX;
posY += speedY;

You assing a value to velocity like 0.2. In you code where you are listening for input:

if(A is pressed) speedY += velocityY;

Same with the others as well. To stop, you need to check if the button is released so you can decrease the speed to 0. Almost done, but this way your object will accelerate while you pressing the right button. You can define a max speed that will help you decide whether go any faster or not.

if(A is pressed && speedY < maxSpeed) speedY += velocityY;

Will solve this issue. To take care about direction you will need the absolute value of your speeds.

\$\endgroup\$
  • \$\begingroup\$ Thanks for the attempt at an answer. My question is about trying to determine what to do between changing the positions of the user. Example if youre at point (0, 0) with a velocity in the X and Y directions of 100 units, do you check everything between the range of 0,0 -> 100,100 and adjust your physics? What happens if you hit something? Do you then change your velocity and keep trying to move the player? etc \$\endgroup\$ – Water Jul 5 '16 at 14:10
  • \$\begingroup\$ That depends a lot on the system you are using to design the game. Typically, if you hit something you'd have some sort of collider. Based on your physics, you would stop, or maybe bounce off of it or slide around it. Are you creating the game from scratch or using something like Unity or Unreal? Are you using physics of any sort (this can also impact code for movement) or just "implying" physics via simple code? \$\endgroup\$ – Jesse Williams Jul 5 '16 at 16:32
  • \$\begingroup\$ @JesseWilliams Purely from scratch, this is my first time attempting such a thing. \$\endgroup\$ – Water Jul 5 '16 at 20:11
  • \$\begingroup\$ @Water - good for you then. That's an awesome (and daunting) undertaking. I'll answer more completely below. \$\endgroup\$ – Jesse Williams Jul 6 '16 at 14:27
1
\$\begingroup\$

I havent done much on these lines.

But every object should recive (time_elapsed) and calculate new position accordingly.

They should check for possible collision with static objects, and react (change of direction / state).

As far as other closer moving objects, you will have to do some hack (either treat the slower one as static, or get its approximate position in-between 2/3 slices of time frame and treat the slower one as static, or calculate the collision point based on their speed of movements, decide new course for both objects and mark both as done for the current time.

If there is more collision and detection, you would certainly want to use a physics library like Box2d.

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

@eldo's answer is good, but since you are doing this from scratch, you need to begin implementing systems for physics. There are all manner of ways to do this, from implementing a quadtree of physical space (so you're only checking sections that your moving object will pass through), to implementing colliders like Unity does (and I believe Unreal Engine has a similar system).

As to the question of what happens when the moving object hits something - that depends on what you implement. If the physics of your engine dictates a full stop of the object, then I would reduce the velocity to 0, only because continually trying to keep velocity on an object that cannot go anywhere seems like a waste of cycles.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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