Is gender (X) modulo X equal to X?

quick question. I mainly work on a program in which we have an Entity cross through a grid. Every time he finishes the "step" (i.e., Goes from (0, 0) to (1, 0)), I need to fire an event. The movement of an entity per frame is calculated using:

frameMovement = entitySpeed * (frameDeltaMs / 1000) 

and then added to the X coordinate of the object. I decided to use world units (1 world unit of 16 pixels) instead of raw pixels for the coordinate system, just to separate the anxiety. The world does not need to know that the distance for one square is 16 pixels - only the drawing system does.

So basically, I'm trying to figure out how to tell the system that the device has completed the "step". Obviously, because of how the motion of the frame is calculated, you will never be exactly on the new square, so I have to “fix” the block on this square or have a method that returns “true” if we are at a certain distance from the square (which right now I decided to be 0.05wu - about 0.8 pixels from the square).

The function that I have so far is this:

 return Math.floor(x) % x == 0; 

I use only X because I'm just trying to get it working at the moment. However, the problem is that this function returns one of two results:

 x = 0f; System.out.println(Math.floor(x) % x); > NaN 

This is to be expected since 0% 0 is indeed NaN. I can get around this without a problem. However, the question I have at the moment is a different result.

 x = 1f; // Or any number with 1sd > 0 System.out.println(Math.floor(x) % x); > 1f 

That is, another result always returns the value of X, and not the correct modulo (which, if math.floor (x) and x are equal, i.e. x is 1f), which should be 0.

I'm not quite sure what causes this. If you have any answers for me or any suggestions on how I can fix this better, I’m all ears.

TL DR: I need to find out when Entity moves from one coordinate to the next. The coordinates (and the Entity transition through them) are stored as a float. If Entity is halfway between the intersection of the square, the coordinate of Entity X is stored, for example, as 1.5. Finally, Floor (1.0)% 1.0 returns 1.0, not 0.

EDIT:

In my code, I have this

  System.out.println("x equals " + x + ". Math.floor(x)%x==Math.floor(x): "+(Math.floor(x)%x==Math.floor(x))); 

With the release of http://pastebin.com/egDwj7Gs

EDIT 2: Images are funny!

http://prntscr.com/1j193n

The white square is the essence. The red square is just a drawn grid. Each square is 16 by 16.

As you can see, right now the white square is at (6.5, 0). I need a program to basically recognize when it passes (7, 0).

EDIT: Lee and I chatted, here is our chat: http://chat.stackoverflow.com/rooms/34727/discussion-between-neglected-and-lee-meador

He solved the problem for me, so I accepted his answer as correct.

  float after = pos.x+changeInPosition; if (Math.floor(after) != Math.floor(pos.x)) { // We passed a boundary } pos.x = after; 
+6
source share
1 answer

Modulo means that we share and take the remainder (as in 3rd grade for me).

So...

 10 % 9 is 1. 

AND

 10 % 10 is 0 

AND

 10.5 % 10.5 is 0. 

(Perhaps you could solve the rounding problems if x = ... computed 10.5 .. and then x% x, where you would get a number just below 10.5 or just greater than 0.)

So for all integers

 Floor(x) % x is 0. 

For the non-integral, it works differently.

 Floor(10.5) => 10 

and

 10 % 10.5 is 10. 

Thus, excluding the effect of rounding errors in a floating point, Floor (x)% x is 0 (for integers) or Floor (x) for non-integers.

More details

Perhaps you could tell if he crossed the grid line by comparing the positions before and after:

 if (Math.Floor(xAfterMoving / gridSize) != Math.floor(xBeforeMoving / gridSize)) { fireXGridCrossingEvent(); } 
+4
source

Source: https://habr.com/ru/post/950933/


All Articles