
:-object wasp2v : [bcilib,vectorlib].

var url = './street/street5.wrl'.

var timelimit = 300.

main :-
	text_area(Browser),
	set_output(Browser),
	format('Loading street1 from ~w~n', [url]),
	loadURL(url),
	format('The bus1 is going to jump in front of you in 5 seconds,~n'),
	format('then you can drive the bus for ~w seconds~n', [timelimit]),
 	delay(5000),
 	jump_to(bus1),
 	drive(bus1, timelimit).

jump_to(Object) :-
	getSFVec3f(proxSensor,position,X,_Y,Z),
	Z1 is Z-5,
	setPosition(Object,X, 0.0 ,Z1).

drive(_,0):-!.	

drive(Object,N) :-
	N > 0,
	N1 is N-1,
	format('time left: ~w seconds~n', [N]),
	delay(1000),
	getSFVec3f(proxSensor,position,X,_Y,Z),
	getSFRotation(proxSensor,orientation,X2,Y2,Z2,R2),
	setPosition(Object,X, 0.0 ,Z),
	vector_rotation(vector(0,0,-1), rotation(X2,Y2,Z2,R2), vector(X3,Y3,Z3)), 
	look_on_direction(Object,vector(1,0,0),vector(X3,Y3,Z3)),
	drive(Object,N1).

look_on_direction(Object, InitVector,DesVector):-
	vector_cross_product(InitVector,DesVector,vector(X,Y,Z),R),
	setRotation(Object,X,Y,Z,R).




:-end_object wasp2v.

