1 / 43

Web Games Programming

Web Games Programming. Game Math. Agenda. Revise some basic concepts Apply Concepts to Game Elements. The Natural Numbers. 4. 1. 3. 5. 2. 6. 7. The Natural Numbers 1..infinity. The Integers. Computer data type int, uint (AS3) name depends on language in use. 0. -3. -1. 1. -2.

keelia
Télécharger la présentation

Web Games Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Web Games Programming Game Math

  2. Agenda • Revise some basic concepts • Apply Concepts to Game Elements

  3. The Natural Numbers 4 1 3 5 2 6 7 The Natural Numbers 1..infinity

  4. The Integers Computer data type int, uint (AS3) name depends on language in use 0 -3 -1 1 -2 2 3 The integers = whole numbers positive, negative or zero

  5. Rational Numbers Computer data type long, double ,float, real, Number (AS3) name depends on language in use 0 -3 -1 1 -2 2 3 The Rationals = fractions with an exact decimal value e.g 1/2 = .5 positive or negative

  6. Irrational Numbers Computer data type as for rationals long, double ,float, real, Number 0 -3 -1 1 -2 2 3 The irrationals = fractions without an exact decimal value e.g 1/3 = .33333 positive or negative

  7. The Number Line: Root 2 Square root of 2 1.41421 2 -1 1 3 0 4 5

  8. The Golden Ratio - Phi The Golden Ratio 1.618033897 2 -1 1 3 0 4 5

  9. The Natural Logarithmic Constant e e = 2.7182818284… 2 -1 1 3 0 4 5

  10. The Number Line Hmm… pie PI the Relationship between the diameter of a circle and its circumference 3.142 (easy)3.141592654 (calculator) 2 -1 1 3 0 4 5 valueForPI = Math.PI; // AS3 (3.141592653589793)

  11. The Number Line Attributes of a Circle: RadiusDiameterCircumferenceArea Radius =rDiameter =2rCircumference = C = PI x DArea = PI x r x r

  12. ActionScript const PI:Number = Math.PI;// define a constant public function areaOfCircle(radius:Number):Number { var area:Number; area = PI * radius * radius; return area; }

  13. Radians Radian is a natural measurement of angular rotation There are 2PI radians in a circle (~= 6.284)

  14. Radians v Degrees There are 2 PI radians in a circle = 3.142 x 2 = 6.284 There 360 degrees in a circle One radian = 360 / 6.284 approximately 57.288 degrees Formula for changing radians to degrees : degrees = (360 / 2PI) Formula for changing degrees to radians : radians = degrees x (2PI / 360)

  15. Pythagoras a c b a2 = b2 + c2

  16. Pythagoras a2 c2 b2

  17. Pythagoras a = 5 c = 3 b = 4

  18. Pythagoras a2 = 42 + 32 a = 16 + 9 a = SQRT (25) a = 5

  19. Application to Games x P1 (x1, y1) y Length = y2 - y1 P2 (x2, y2) Length = x2 - x1

  20. Application to Games x P1 (10, 15) Length = y2 - y1 = 50-15 = 35 y a P2 (80,50 ) Length = x2 - x1 = 80 - 10 = 70

  21. Application to Games x distance a = SQRT((x2-x1)2 + (y2 - y1)2) y a

  22. ActionScript // distanceXY calculates the distance between two points x and y // and returns the resulting value public function distanceXY(pointX1, pointY1, pointX2, pointY2:int):Number{ var distance:Number; distance = Math.sqrt((pointX2-pointX1) * (pointX2-pointX1) + (pointY2-pointY1) * (pointY2-pointY1)); return distance }

  23. Trigonometry 1 s I n e Tan -1 1 cosine -1

  24. Sine Values 1 Rotate line through some angle Read value off sine axis s I n e -1 1 cosine -1

  25. Sine read sine value angle about 0.52 33 degrees

  26. Cosine Values 1 s I n e -1 1 cosine angle 45 degrees -1

  27. Tan Values Read value off tan axis tan 1 Rotate line through some angle (45) tan(45) = 1 -1 1 tan -1

  28. Tan Values Read value off tan axis(about 2.7) 1 Rotate line through some angle(65 degrees) s I n e -1 1 cosine -1

  29. Tan Values Read value off tan axis(about 57.289) 1 Rotate line through some angle(89 degrees) s I n e -1 1 cosine -1

  30. Tan 90 ! Lines parallel -tan 90 not defined 1 Rotate line through some angle(90 degrees) s I n e -1 1 cosine -1

  31. Tan > 90 1 Rotate line through some angle(135 degrees) s I n e -1 1 cosine value read fromthis axis (-1) -1

  32. Trigonometric Ratios hypotenuse opposite adjacent sine of the angle = opposite / hypotenuse cosine of the angle = adjacent / hypotenuse tan of the angle = opposite / adjacent Angle in degrees = 1/sine etc.

  33. Trig Ratios sine of the angle = opposite / hypotenuse cosine of the angle = adjacent / hypotenuse tan of the angle = opposite / adjacent SOH CAH TOA

  34. Finding the angle to a point Point (x2, y2) angle theta? point(x1,y1)

  35. Finding the angle to a point Point (x2, y2) point(x1,y1)

  36. Finding the angle to a point Point (x2, y2) angle theta? point(x1,y1)

  37. Use the tangent formula point (x2, y2) tan theta = opposite / adjacent tan theta= (y2-y1) / (x2-x1) the angle = 1/tan (theta) atan(theta) angle theta point(x1,y1)

  38. Calculate for each frame point (x2, y2) (moving sprite) angle theta? point(x1,y1)

  39. Some tan angles point to the same value… 1 tan 45 =1 s I n e -1 1 cosine tan 225 =1 -1

  40. Using the atan2 function arctan(x) function may return same angle value for two different points if in certain quadrants - would need to determine which is correct for direction required Use atan2(y,x) specially designed function, which by usingnumbers in the complex plane, returns one unique valuefor any angle Value is returned in radians - convert to degrees withappropriate formula

  41. ActionScript // definition in GameMath.as public function pointAtSprite(xPoint:Number, yPoint:Number, pointerX:Number, pointerY:Number):Number { var dx:Number = xPoint - pointerX; var dy:Number = yPoint - pointerY; // determine angle and convert to degrees var pointAngle:Number = Math.atan2(dy,dx); var pointDegrees:Number = 360*(pointAngle/(2*Math.PI)); return pointDegrees; } //pointAtSprite stage.addEventListener(Event.ENTER_FRAME, getPointAngle); function getPointAngle(event:Event){ pointAtSpriteAngle= gameMath.pointAtSprite(boat_mc.x, boat_mc.y, pointer.x, pointer.y); pointer.rotation = pointAtSpriteAngle;// instance name of gun turret sprite };

  42. Game Math:Applications • Used to calculate sprite positions and directions • Determine distances between game objects (Pythagoras) • Use distance between objects for collision detection purposes • Change game state based on game sprites positions in game world • Give sprites speed and a known direction (Trigonometry SOH CAH TOA) • Use atan2 to follow or point at moving sprite • In 3D games, math is used to orientate objects in three coordinate systems x,y,z • Used to construct formulae which implements sprite behaviours for physics - such as acceleration, momentum, friction.

  43. The Prime Numbers A Prime number is only divisible by 1 and itself 3 5 2 7 The distribution of Prime numbers is the most important unanswered question in Mathematics - can you find a formula to say if a given number is a Prime number? 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 If you can you will probably win a Nobel prize and be famous forever!

More Related