View Single Post
  #8  
Old 10-07-2018, 07:50 PM
hannahapz hannahapz is offline
Member
 
Join Date: Apr 2018
Posts: 22
Revisit

So I've managed to use the distance function to inform me of when my cars get too close to one another, and I've managed to speed up the lead car/slow down the tailing car to simulate realistic driving, but the problem I'm having is with my nested if loops.

Code:
# function that has cars move at a trajectory of, in this example, 10 m/s)
def moving_cars():
	global elapsedTime, z, obs_car
	for i, item in enumerate(my_entire_car_list):
		item.add(vizact.move(0, 0, 10)

# function ensures cars adjust speeds if they get within a certain distance of one another (lead car speed up and tail slows)			
def track_A_moving_cars():
	global track_A, track_B, track_C
	for i in range(len(track_A)):
		for a in track_A[i+1:]:
			if vizmat.Distance(track_A[i].getPosition(), a.getPosition()) < 30.0:
				if track_A[i].getPosition()[2] < a.getPosition()[2]:
					a.add(vizact.move(0, 0, 3, 1), 0)
					track_A[i].add(vizact.move(0, 0, -10, 2), 1)
					
				elif track_A[i].getPosition()[2] > a.getPosition()[2]:
					a.add(vizact.move(0, 0, -10, 2), 2)
					track_A[i].add(vizact.move(0, 0, 3, 1), 3)
EXAMPLE OF MY PROBLEM: targeted tail car will slow down, then resume regular speed, slow down again, resume regular speed, etc. I'm assuming this is because o fmy nested if within nested for loop. I've set the function to run every frame. How do I restart the function?

Any help would be greatly appreciated.

Last edited by hannahapz; 10-07-2018 at 07:59 PM. Reason: Incomplete initial response
Reply With Quote