Why do you call logic() inside that for-loop, and also after the for-loop. Aren't you calling the logic() way too often, and hence slowing down the game? Isn't it better to calculate the delta argument to pass to logic(delta) instead of calling logic() n-times? Like this:

What you're looking at there is fudged collision detection (which I actually use in everything these days ). 4K isn't a great place to look for good coding examples btw

In the game there may be moving elements which want to collide with each other that are updated by the logic based on the delta in time (change in time). However, if the logic time step gets too big two elements might move so far that they pass right though each other. Imagine a bullet and alien. The bullet is moving - if the logic which moves it was stepped with 1000ms it might jump right past the alien and never intersect with it. The way the loop is above the time step is always small - meaning that no matter how much time is passed the logic is only updated in small sections. Logic tends to be cheaper than rendering (horrible generalisation) in this sort of simple game - so it doesn't hurt to call render often and makes the collision detection easy to implement and much more reliable.

The other way to do this which more reliable is swept collision detection when you work out the path of all the moving elements and determine where they cross based on time. However, this sort of collision is non-trivial and doesn't really suit 4K programming (or me generally for that matter).

Let's say I have a Sprite (bullet) that needs to move 100 pixels every 1 second (let's say from bottom to top of screen), and the delta is 7ms (since the last game loop), how do I calculate how much to move that sprite in that loop?

This is my favorite pet peeve these days, but that timing loop is leaking time!The time between the two calls to System.currenTimeMillis() will be lost forever, resulting in a slightly slower running game on slow computers.

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org