CMSC 425: Lecture 7 Geometric Programming: Samle Solutions Samles: In the last few lectures, we have been discussing affine and Euclidean geometr, coordinate frames and affine transformations, and rotations. In this lecture, we work through a few examles of how to al these concets to solve a few concrete roblems that might arise in the context of game rogramming. Caveat: I have not tested the Unit code given here, so don t trust it! Shot-Gun Simulator: Problem: You have been asked to imlement a new weaon that behaves something like a shot gun. It has a wide ange of effectiveness, but it is onl effective at relativel small distances. Suose that the end of the mule of the gun is located at a oint (in 3-dimensional sace), and it has been aimed at a target oint t (see Fig. 1). The gun shoots a sra of ellets within angle θ the central axis between and t, but bullets are onl effective u to a distance of r from the end of the mule. (Let s assume that θ is given in degrees and is strictl smaller than 90.) Given a oint, write a rocedure to determine whether the oint will be hit when the gun is fired. r r û u θ t θ v t (b) Fig. 1: Shot gun. Solution: We need to determine (1) whether lies within the infinite cone about the central axis t and (2) whether is close enough to be hit. How do we determine the first condition? We can construct two vectors, one from to t and one from to, and determine whether the angle between them is at most θ degrees (see Fig. 1(b)). First, define a vector v to be the vector from to t, that is, v t. Next, define the vector u to be a vector that is directed from to, thus, u. Let us normalie these vectors to unit length, b defining û normalie( u) = u/l(u), where l(u) = u = u u. (Here we have used the roert of dot roduct, that the dot roduct of a vector with itself is the vector s suared length.) We can do the same for v. In order for to lie within the cone, we comute the angle between these vectors. Recall, that we can comute the cosine of two unit vectors b taking their dot roduct. Since the cosine is a monotonicall decreasing function (for the angles in the range from 0 to 180 ), this is euivalent to the condition û v cos θ. Lecture 7 1 Sring 2018
Be careful! Remember that θ is given in degrees and the cosine function assumes that the argument is given in radians. To convert from degrees to radians we multil b π/180. So the correct exression is ( π ) û v cos θ. 180 To solve (2), it suffices to test whether If l(u) > r then is too far awa to be hit. To summarie, we have the following test. v t ; u l(v) v = v v; l(u) u = u u v normalie( v) = v/l(v); û normalie( u) = u/l(u) c 1 û v ( π ) c 2 cos θ 180 return true iff (c 1 c 2 and l(u) r). A Unit imlementation of this rocedure (which I haven t tested) can be found in the following code block. } Does a shot gun fired at toward t hit oint? bool HitMe ( Vector3, Vector3 t, float theta, float r, Vector3 ) { Vector3 v = t - ; // vector from mule end to target Vector3 u = - ; // vector from mule end to float lu = u. magnitude ; // distance to Vector3 vv = v. normalied ; // directional vector to t Vector3 uu = u. normalied ; // directional vector to float c1 = Vector3. Dot ( uu, vv); // cosine of angle between vectors float c2 = Mathf. Cos ( theta * Mathf. PI / 180) ; // cosine of hit cone return ( c1 >= c2) && ( lu <= r); // target within cone and distance Projectile Shooting: Your game involves a shooting an object (and arrow, rock, grenade, or other rojectile) in a certain direction. Your boss wants ou to write a rogram to determine where the rojectile will land, as art of an aiming tool for inexerienced laers. Suose that the rojectile is launched from a location that is h meters above the ground. Following Unit s convention the rojectile starts above on the vertical () axis, at coordinates (0, h, 0). Suose that the rojectile is launched with a velocit given b the vector v 0 = (v 0,x, v 0,, v 0, ). Let s assume that the arrow is shot uwards, that is, v 0, > 0. To simlif matters, let s assume that the rojectile is shot in the forward () direction. Thus v 0,x = 0 and v 0, > 0. We want to determine the distance l from the shooter where the rojectile hits the ground. Let t = 0 denote the time at which the object is shot. After consulting a standard textbook on Phsics, we are reminded that (on Earth at least) the force of gravit results in an acceleration of g 9.8m/s 2. Lecture 7 2 Sring 2018
v 0 = ( x, h, ) v 0 h h v 0, t x (b) Fig. 2: Projectile shooting After consulting our hsics text, ou find out that after t time units have elased, the osition of the rojectile is (t) = ((t), (t)), where (t) = v 0, t and (t) = h + v 0, t 1 2 gt2. (Assuming no wind resistance, the rojectile s motion with the -axis is constant over time as v 0,. It s motion with resect to the -axis follows a downward arabolic arc as a function of time t.) We are interested in the time t when the rojectile hits the ground, that is, (t) = 0. Time of Imact: Letting a = g/2, b = v 0,, and c = h, we seek the value of t such that at 2 + bt + c = 0. (We have intentionall negated the coefficients so that a > 0.) B the uadratic formula we have t = b ± b 2 4ac 2a = v 0, ± v0, 2 + 2gh. g Note that the uantit under the suare-root sign is ositive and is larger than v 0,, which imlies that both roots exist, one is ositive and one is negative. Clearl, we want the ositive root. (As an exercise, what does the negative root corresond to?) Thus, we take the + root from the ± otion, which ields t = ( v 0, + v 2 0, + 2gh ) /g. Location of Imact: We know that the rojectile moves at a rate of v 0, units er second horiontall. Therefore, at time t = t it has traveled a distance of v 0, t units. Since we started at the origin, the location we hit the ground is (x,, ) = (0, 0, v 0, t ). Generaliing this: We assumed that the rojectile was shot along the -axis. Note that the generaliation is ver eas. We comute t in the same manner as above, since it deends onl on the height and vertical velocit. Then we al the same reasoning for x as for. The rojectile travels a distance of v 0,x t units along the x-axis, so its final osition is (x,, ) = (v 0,x t, 0, v 0, t ). We also assumed that the rojective was shot from h units above the origin. If, instead it had been shot from some arbitrar oint ( x, h, ), then we would dislace the final location b this amount, as = ( x + v 0,x t, 0, + v 0, t ). This is where we would draw our sot for aiming tool (see Fig. 2(b)). Lecture 7 3 Sring 2018
Aiming Tool for Projectile Vector3 HitSot ( Vector3, Vector3 v) { float h =.; // height above ground float a = 9.8 f / 2. 0 f; // uadratic coefficients float b = -v.; float c = -h; float t = (-b + Math. Srt (b*b - 4*a*c)) / (2* a); // time in the air return new Vector3 (.x + v.x*t, 0,. + v.*t); // where we hit } Shooting and Arrow: Before leaving the toic of shooting the rojectile, it is worth observing that the Unit Phsics engine can simulate the motion of the rojectile. This will look fine if the rojectile is a ball. However, if the rojectile is an arrow, the arrow will not turn roerl in the air in the manner that arrows do. Unit will siml translate the arrow through sace, without rotating it (see Fig. 3). This raises the uestion, How can we rotate the arrow to oint in the direction it is traveling? (see Fig. 3(b)) (b) Fig. 3: Arrow shooting This ma seem to be a comlicated uestion. Clearl, this is not a rotation about the axes, and so Euler angles would not be the right aroach. We need to use uaternions. We want to secif a uaterion that will cause the arrow to rotate in the direction it is moving. Luckil for us, Unit rovides a function that does almost what we need. The Unit function Quaternion.LookRotation(Vector3 forward) generates a rotation (reresented as a uaternion) that aligns the forward () axis with the argument. Thus, to align the arrow with the direction it is heading, we can use the following Unit commands: RigidBod rb = getcomonent < RigidBod > (); transform. rotation = Quaternion. LookRotation ( rb. velocit ); This will rotate the object so its orientation matches its velocit, as desired. Evasive Action: Problem: In our latest game, ou are simulating the AI for some alien sace shis. Each sace shi is associated with its current osition, a oint, and two unit-length vectors. The first vector v indicates the direction in which the sace shi is fling. The second Lecture 7 4 Sring 2018
vector u is orthogonal to v and indicates the direction that is u relative to the ilot fling the sace shi (see Fig. 4). There are various obstacles to be avoided (asteroids, and such) and the AI sstem needs to issue turning commands to avoid these obstacles. Given an obstacle at some oint, the uestion that we want to determine is whether we should turn (aw) to the left or right and whether we should turn (itch) u or down to avoid the collision. (We won t worr about the actual number of degrees of rotation for now, just the direction.) u v r u ŵ v w Fig. 4: Evasive action. Solution: First observe that w defines a vector that is directed from the sace shi to the obstacle (see Fig. 4(b)). To convert this into a unit vector (since we just care about the direction), let us normalie it to unit length. (Recall that normaliing a vector involves dividing a vector b its length.) We can comute the length of a vector as the suare root of it dot roduct with itself. Thus, we have w ŵ normalie( w) = w w = w w w = w. wx 2 + w 2 + w 2 What we want to know is whether this vector is ointing to the sace-shi ilot s left or right (in which case we will turn the oosite direction), or is above or below (in which case we will itch in the oosite direction). Let s first tackle the roblem of whether to turn itch u or down. We can determine this b checking whether the angle between the u-vector u and ŵ. If this angle is smaller than 90, then the obstacle is above us and we should itch downward. Otherwise, we should itch uward. Given that both vectors have unit length, we can comute the cosine of the angle between them b the dot roduct. If the dot roduct is ositive, the angle is smaller than 90, thus the obstacle is above, and we turn down. Otherwise, we turn down. We have ŵ u 0 (obstacle above) itch downwards ŵ u < 0 (obstacle below) itch uwards. (B the wa, since we are onl checking the sign of this dot roduct, not its magnitude, it was not reall necessar to normalie w to unit length. We could have substituted w for ŵ above without affecting the correctness of the result.) Lecture 7 5 Sring 2018
Next, let s consider whether to turn left or right. We would like to erform a similar te of comutation, but to do so, we should generate a vector that indicates left and right relative to the ilot of the shi. Such a vector will be orthogonal both to the direction that we are fling and to the u direction. We can obtain such a vector using the cross-roduct. In articular, define a vector r v u. B the right-hand rule, this vector will oint to the ilot s right. B our assumtion that v and u are orthogonal to each other and of unit length, it follows from the definition of the cross roduct that r will also be of unit length. We reason in an analogous manner to the u-down case. If the angle between ŵ and r is smaller than 90, then the obstacle is to our right, and we turn left to avoid it. Otherwise, we turn right. This is euivalent to testing whether the cosine of the angle is ositive or negative. Thus, we have r v u ŵ r 0 (obstacle to the right) aw to the left ŵ r < 0 (obstacle to the left) aw to the right. A Unit imlementation of this rocedure (which I haven t tested) can be found in the following code block. Turning a shi at osition to avoid obstacle at void Evade ( Vector3, Vector3 v, Vector3 u, Vector3 ) { Vector3 w = - ; // vector from ilot to obstacle float l = w. magnitude ; // distance to obstacle Vector3 ww = w. normalied ; // directional vector to obstacle if ( Vector3. Dot ( ww, u) >= 0) // obstacle is above? PitchDown (); else PitchU (); Vector3 r = Vector3. Cross ( v, u); // vector to ilot s right if ( Vector3. Dot ( ww, r) >= 0) // obstacle is to the right YawToLeft (); else YawToRight (); } We have not discussed how to erform the itch or aw oerations. In Unit, these could be exressed as rotations about the vectors r and u, resectivel. Lecture 7 6 Sring 2018