comparison TTC Sample/timeToCollision.py @ 462:af2222c0c9c0

TTC tested and updatet!
author Sohail Zangenehpour <sohail.zangenehpour@mail.mcgill.ca>
date Mon, 03 Feb 2014 15:41:57 -0500
parents
children
comparison
equal deleted inserted replaced
461:cb41f9a4652b 462:af2222c0c9c0
1 def timeToCollision(obj1,obj2,collisionDistanceThreshold,frameNum,framePerSecond):
2
3 import numpy as np
4
5 x1 = obj1.getPositionAtInstant(frameNum).x
6 y1 = obj1.getPositionAtInstant(frameNum).y
7 x2 = obj2.getPositionAtInstant(frameNum).x
8 y2 = obj2.getPositionAtInstant(frameNum).y
9 v1x = obj1.getVelocityAtInstant(frameNum).x * framePerSecond
10 v1y = obj1.getVelocityAtInstant(frameNum).y * framePerSecond
11 v2x = obj2.getVelocityAtInstant(frameNum).x * framePerSecond
12 v2y = obj2.getVelocityAtInstant(frameNum).y * framePerSecond
13 l = collisionDistanceThreshold
14
15 a = pow(v1x-v2x,2) + pow(v1y-v2y,2)
16 b = 2 * ((x1-x2) * (v1x-v2x) + (y1-y2) * (v1y-v2y))
17 c = pow(x1-x2,2) + pow(y1-y2,2) - pow(l,2)
18
19 if pow(b,2) >= 4*a*c:
20 ttc1 = (-b + np.sqrt(pow(b,2) - 4*a*c)) / (2*a)
21 ttc2 = (-b - np.sqrt(pow(b,2) - 4*a*c)) / (2*a)
22 if ttc1 >= 0 and ttc2 >= 0:
23 ttc = min(ttc1,ttc2)
24 else:
25 if ttc1 < 0:
26 ttc = ttc2
27 if ttc2 < 0:
28 ttc = ttc1
29 if ttc1 < 0 and ttc2 < 0:
30 ttc = []
31 else:
32 ttc = []
33
34 return ttc