F1 Track Simulator
F1 Track Simulator
Design Document
Requirement in brief
● F1 cars report location in real time during a race at Indianapolis Motor Speedway.
● A web page should be developed to show a simulation of the race, based on the
location reported by the cars.
Technologies Used
● HTML / CSS3 / Javascript(ES6)
● Svg.js library
Lengths were converted to meters and following formula was used to determine the optimum
scale to draw the track on the user’s display.
M${x1} ${y1} L${x2} ${y2} Q ${xc2} ${yc2} ${x3} ${y3} L${x4} ${y4}
Q ${xc3} ${yc3} ${x5} ${y5} L${x6} ${y6} Q ${xc4} ${yc4} ${x7} ${y7}
L${x8} ${y8} Q ${xc1} ${yc1} ${x1} ${y1}
Stroke of width 5 x scale was applied to the path along with a texture of a road.
Rendering Cars
Cars are rendered on the track as two components(bounding box and the image of the car,
where bounding box is the parent of image).
√2 * (widthOf Car ) , which allows car to rotate around
2
Length of the bounding box was set at
center without overflowing.
Bounding box always keep it’s center coincided with the center of the track while image inside
(car) rotates around center whenever car should be rotated(just to create a visual effect when
taking turns). This makes, corners smoother and prevents car from leaving the track when
taking turns.
Moving Cars
Movement of car on track
A pixel point array was created for SVG path drawn by taking 1 pixel as the smallest distance
where 0th index of the array corresponds to the start of the track.
A car in the track always belongs to an element of the above array. When the actual distance
travelled by a car(measure from start) is reported, the intended index of the path array can be
determined by simply multiplying actual distance by the scale.
Once intended position in the array is determined, bounding box of the car is moved from
starting element to the intended element of the array(path) using a timer(Changing the center of
the bounding box to the corresponding coordinate of the array element). This creates an
animated moving effect of the car along the track.
Rotation
Distance(in pexels) from the start of the track to the end of each segment(Sn) (Pn - Pn+1) was
calculated.
s1 = length of long straight way
s2 = s1 + length of a turn
s3 = s2 + length of short straight way
.
.
s8 = s7 + length of a turn
On each animated frame(On each transition of bounding box from one element to another in
pixel points array), following algorithm is used to determine the corresponding rotation of the
car.
current distance of the car f rom center(d) = current index of the array
***
Demo https://chathurawidanage.github.io/f1-track-si
mulation/index.html
Source https://github.com/chathurawidanage/f1-track-si
mulation