Determining player position in racing game

Hi everyone. I am generally asking for a java alternative code of sorting about eight numbers according to size besides adding numbers to an arraylist then sorting them out in ascending order using Collections ,readoff the players index ,then clear the list for the next execution. Now when such a method is executed in the update method at about sixty times per second ,i generally dont feel this is the best way to do it. I currently keep track of the number of checkpoints each car has cleared ,and the distance to the next checkpoint and these two are the values i am trying to sort out .Will definately appreciate any idea.

Are you just trying to sort which racer is in front?

Unless you have a concept of when one racer passes another, I’m not sure you can do much better than just resorting every time (which should be plenty fast, really).

I’m not sure why you reconstruct the list every time. It seems like keeping the racers in a list and sorting it with a custom comparator would be easier and then the list is a fixed size and doesn’t ever get reallocated.

Edit: also note that you probably don’t need to sort once a frame if it’s just for racing position. I would think 4 times a second or so would be enough.

@pspeed ,thanks for the quick reply ,i do have a mechanism do determine when a car has passed another ,and i was able to determine positions for two or three cars using ifs but have resorted to arraylists to solve the problem with more cars.The car which has cleared the most checkpoints is in the lead ,and if the number of checkpts cleared is the same,i compare their distance to the next checkpt. I was clearing the list because im adding checkpts cleared to an integer arraylist ,and the number of checkpts keeps increasing ,since i havent done much on lists ,il definately limit its size and assign an index when i add my values to the list ,thanks for the ideas.

Yeah, then I would personally use a List containing the racers and not integers. When sorting, you can then use a custom comparator that pulls the relevant data to determine sorting. Then you don’t have to worry about messing with the list every time… just sort it again.

Also rella ydont worry about like everything you can do with a small list performance wise, the 3d part does thousands of calculations every frame so your own logic is performance wise usually not really existant. (There are only 3 mayor logic parts in games that can rellay impact performance, physics, database, ai(pathfinding)) everything else is normally not really measuarable in normal cases.