import math import pyinsim insim = pyinsim.insim(b'127.0.0.1', 29999, Admin=b'', Flags=pyinsim.ISF_MCI | pyinsim.ISF_LOCAL, Interval=1000) PLID = 0 cars = [] xCoord = 0 yCoord = 0 zCoord = 0 # The array cars will contain arrays that represent a car and its data. # Example: cars[0] would contain PLID, X, Y, Z, Speed, Heading # Keep in mind: when more than 8 cars are on the track, more than one MCI packet is sent # You need outgauge to get your own playerID. Then you can distinguish your data from the other cars. def outgauge(outgauge, packet): global PLID PLID = packet.PLID outgauge = pyinsim.outgauge(b'127.0.0.1', 30000, outgauge, 30.0) # This function is basic math to calculate the distance between two points in a coordinate system. def dist(a=(0, 0, 0), b=(0, 0, 0)): return math.sqrt((b[0] - a[0]) * (b[0] - a[0]) + (b[1] - a[1]) * (b[1] - a[1]) + (b[2] - a[2]) * (b[2] - a[2])) def get_car_data(insim, mci): global cars, xCoord, yCoord, zCoord # Save new arrays of car data in update cars and then override cars, so that no data gets lost or doubled # as 8 cars can be in one packet. Without "update_cars" cars would not contain all data for a few milliseconds # every interval. This might not work if you have more than 8 cars on track, as i overwrite "cars" with every packet. # So you would need a bit of other logic for more than 8 cars on track. update_cars = [] # Look through all cars and save the own coordinates. put the others into update_cars. for car in mci.Info: if car.PLID == PLID: xCoord = car.X / 65536 yCoord = car.Y / 65536 zCoord = car.Z / 65536 else: temp = [car.PLID, car.X / 65536, car.Y / 65536, car.Z / 65536, car.Speed / 91.02, car.Heading] update_cars.append(temp) # override cars cars = update_cars # calculate distance to the first other car on track. You could use a for loop here to calculate all distances. distance_to_car = dist((xCoord, yCoord, zCoord), (cars[0][1], cars[0][2], cars[0][3])) print("Distance to car with PLID: " + str(cars[0][0]) + " is " + str(distance_to_car)) insim.bind(pyinsim.ISP_MCI, get_car_data) pyinsim.run()