java - JavaFX traffic simulation transitions -
im trying build traffic sim college project. implemented traffic light logic, dynamically controlling traffic lights etc... im stuck adding vehicles path transition nodes. if add alot of them each 1 having own boundsinparentproperty change listener listens for:
-collision other vehicles keep them apart
-area in front of traffic light able stop when red on
then im ending alot of calculations make animations not ui friedly , app becomes unresponsive.
im guessing transitions needs part of main javafx ui thread, listeners? on choosing correct approach appreciated
the rules on threading are:
- any properties of nodes part of scene graph must accessed fx application thread.
- avoid doing unnecessary calculations on fx application thread.
obviously, you're in case gets tricky manage, because lengthy calculations involve properties have accessed on fx application thread.
the javafx.concurrent package , javafx.animation package have number of facilities ease thread management. specifically, there number of callbacks defined area executed on fx application thread.
nodes modify properties, , consequently execute change listener's methods on fx application thread. transitions update state of properties manipulate on fx application thread, , same callbacks such setonfinished(...). in javafx.concurrent api, task class has call(...) method intended executed on background thread, , there various callback methods (updatemessage(...), updateprogress(...)), handlers: setonsucceeded(...) etc, executed on fx application thread.
for simulation this, recommend looking @ animationtimer. has handle() method implement, invoked once every render frame. receives long argument timestamp in nanoseconds. target frame rate javafx (currently) 60fps, should aim code executes reasonably in here, else slow frame rate, can accomplish quite lot in 16ms.
here example using technique. simulates lots of balls bouncing around in box, it'll give idea. had version kicking around somewhere in each ball implemented it's own animationtimer check wall collisions, , there animationtimer checked ball-ball collisions; performance pretty identical (which not surprise). used "animationframe per object" approach in unfinished space invaders simulation well, , worked pretty well.
Comments
Post a Comment