|
#1
|
|||
|
|||
Having objects avoid one another
I made an environment that involves cars traveling at random speeds. At times, certain cars overlap. I want to avoid this by introducing proximity sensors so that when two cars get close to one another, the car in front speeds up.
Right now, the location of my sensors are identical to the location of my targets (i.e. the same car). What can I do to have all the cars be targets for one another AND have their own self sensors, but only involve the front car accelerate when the car behind it gets too close (as opposed to all cars accelerating, which is what my code is doing right now)? Looking for a solution to my flawed logic. |
#2
|
|||
|
|||
Since you're going to have a stream of cars, maybe you could declare your cars as an array of 3D objects and set the callback for the proximity to be the car next accordingly.
Refer to the proximity sensors section of the documentation for examples of how to use proximity callbacks. Also if you don't want your objects to overlap you can turn on collision. |
#3
|
|||
|
|||
So it's not so much a single stream of cars as it is three streams of cars, where the cars' positions are randomly generated. These cars are in an array already, but they're not organized based on which lane they're in. I guess I should do that first ? Is there an easier way?
|
#4
|
|||
|
|||
Also, I forgot to mention that the x-positions of the cars change so I'm wondering if there's a way to take that into account and still have the cars avoid one another without having their sensors interfere with the identical target assignment.
|
#5
|
|||
|
|||
Here is the code AFTER I've created a list of positions. Here I create an array of 3D cars:
Quote:
Quote:
|
#6
|
|||
|
|||
It maybe easier to use the vizmat.Distance command in this case. If each lane of cars is arranged in its own list, you could loop through the list and for each car check the distance of the cars both in front and in back.
|
#7
|
|||
|
|||
Thanks Jeff! This is what I ended up doing coincidentally. Cheers
|
#8
|
|||
|
|||
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) Any help would be greatly appreciated. Last edited by hannahapz; 10-07-2018 at 07:59 PM. Reason: Incomplete initial response |
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Picking up Vizard Objects on mouse down with collisions | pwsnow | Vizard | 2 | 09-17-2012 02:02 AM |
Trouble picking text3d objects | Salvar | Vizard | 4 | 12-01-2010 03:07 PM |
static or dynamic objects | shahramy | Vizard | 2 | 11-10-2010 12:32 AM |
Semi-circle array containing target and distractor objects | ptjt255 | Vizard | 3 | 08-04-2009 03:09 AM |
Could not find plugin to load objects... | halley | Vizard | 1 | 05-30-2006 11:01 AM |