02.02.2013 Views

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

Flash MX 2004 Games : Art to ActionScript

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Flash</strong> <strong>MX</strong> <strong>2004</strong> <strong>Games</strong><br />

if a and b are both vec<strong>to</strong>rs containing two components x and y. The function ‘dotproduct’ is<br />

designed <strong>to</strong> return this value.<br />

function dotproduct(x1, y1, x2, y2){<br />

return (x1 * x2 + y1 * y2);<br />

}<br />

But the dot product of two vec<strong>to</strong>rs has another definition:<br />

a·b =|a||b| cos θ<br />

This means that the dot product is the magnitude of one vec<strong>to</strong>r times the magnitude of the other<br />

times the cosine of the angle between them. If each has magnitude 1, they are unit vec<strong>to</strong>rs, and<br />

then the dot product is the cosine of the angle between the two vec<strong>to</strong>rs. If this value is less than<br />

or equal <strong>to</strong> zero then we can calculate the new velocities for the objects involved in the collision.<br />

If the value is positive then the collision has occurred with the opposite side of the object, that<br />

is the objects <strong>to</strong>tally overlap; if we allow such a collision then the effect will be <strong>to</strong> bounce in the<br />

opposite direction <strong>to</strong> the desired result.<br />

1 function collision(name){<br />

2 //Get the normal of the collision<br />

3 cnx = (_x + vecx) - (eval(name)._x + eval(name).vecx);<br />

4 cny = (_y + vecy) - (eval(name)._y + eval(name).vecy);<br />

5 //Get magnitude of normal and normalise<br />

6 mag = Math.sqrt(cnx * cnx + cny * cny);<br />

7 cnx /= mag;<br />

8 cny /= mag;<br />

9 //Set masses for collision objects<br />

10 ma = 20;<br />

11 mb = 20;<br />

12 //Coefficient of restitution value,<br />

13 //how much force is lost in collision<br />

14 e = -(1 + 0.8);<br />

15 //Components of player’s velocity are already known<br />

16 vax = vecx;<br />

17 vay = vecy;<br />

18 //Calculate components of player b velocity<br />

19 if (eval(name).speed!=0){<br />

20 vbx = eval(name).vecx;<br />

21 vby = eval(name).vecy;<br />

22 }else{<br />

23 vbx = 0;<br />

24 vby = 0;<br />

25 }<br />

26 //Calculate relative velocity<br />

298

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!