math / physics 101
DESCRIPTION
Math / Physics 101. GAM 376 Robin Burke Winter 2008. Homework #1. Use my code not Buckland’s web site Visual Studio Express see DL’s comments ( col site). Discussion site. any course-related topic. Begin math / physics 101. If you’ve taken GAM 350 or any other kind of physics - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/1.jpg)
Math / Physics 101
GAM 376Robin BurkeWinter 2008
![Page 2: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/2.jpg)
Homework #1
Use my code not Buckland’s web site Visual Studio Express
see DL’s comments (col site)
![Page 3: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/3.jpg)
Discussion site
any course-related topic
![Page 4: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/4.jpg)
Begin math / physics 101
If you’ve taken GAM 350or any other kind of physicsmostly review
Vectors Coordinate spaces Kinematics
![Page 5: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/5.jpg)
Why physics?
What does physics have to do with AI?
Game charactersmust react to the world and its physicsmust make predictions about what will
happen nextmust take actions that depend on the
world’s physical properties
![Page 6: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/6.jpg)
Why physics?
This is the most fundamental form of “avoiding stupidity”
Game charactersshould act as though they understand
the physical world of the gamewill often be blind to other aspects of
the game
![Page 7: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/7.jpg)
Vector
A multi-dimensional quantityrepresented by a tuple of numbers
We will deal mostly with 2-D vectors<2, 5>
3-D vectors are more mathematically complexbut not fundamentally differentfor AI purposes
![Page 8: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/8.jpg)
Vectors
can represent positions<1,1> is a position 1 unit north and 1
unit east of the origin can also represent velocities
<3,-4> is a speed of 5 units in a northwesterly direction
other quantitiesorientation, acceleration, angle, etc.
![Page 9: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/9.jpg)
Vector operations
Magnitude v = <x1, y1> | v | = magnitude(v) = sqrt(x12+y12) the length of the vector
Adding <x1, y1> + <x2, y2> = <x1+x2, y1+y2>
Scalar multiplication <x1, y1> * z = <x1*z, y1*z> changes only the length
Normalization v / |v| makes a vector of length 1 does not change the orientation of the vector
![Page 10: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/10.jpg)
More vector operations
dot product <x1, y1> ● <x2, y2> =
• x1 * x2 + y1 *y2 scalar quantity related to the angle between vectors cos (angle) = u ● v / | u | | v |
Note if u and v are normalized (length = 1) then u ● v is the cosine of the angle between
them
![Page 11: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/11.jpg)
Example we have two agents: a, b
each has a vector position and a normalized vector orientation
• the direction they are facing turn to face an enemy
agent: Pa, Oa enemy: Pb, Ob
vector from agent to enemy Pba = Pb-Pa normalize it
compute dot product norm(Pba) ● Oa
compute inverse cosine this is the angle to turn
![Page 12: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/12.jpg)
Pa
Turn to face
Pb
Pb-Pa
cos-1(Oa • norm(Pb-Pa))
![Page 13: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/13.jpg)
What if I want to run away?
![Page 14: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/14.jpg)
Normal vectors
A normal vector is orthogonal to another vectorin 2 dimensions = 90 degrees
n ● v = 0
![Page 15: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/15.jpg)
Coordinate transformations
We will always want to move back and forth between coordinate systems objects each have their own local coordinate systems related to each other in "world space"
Example NPC
• a character is at a particular position, facing a particular direction
• he defines a coordinate system Door
• the door has its own coordinate system• in that system, the handle is in a particular spot
To make the character reach for the handle• we have to make the two coordinate systems interact
![Page 16: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/16.jpg)
Transformations
We know where the handle is in door space where the agent's hand is in agent space what is the vector in agent space
Set of transformations transform handle location L0 to world space
Lw transform handle location to agent space La
Each transformation is a translation (changing the origin) and a rotation changing the orientation
![Page 17: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/17.jpg)
Affine transformations
Usually coordinate transformations are done through matrix multiplications we multiply a vector by a matrix and get
another vector out The curious thing is
that to do this we have to use vectors and matrices one size larger than our current number of dimensions
you will see <x, y, 1> as the representation of 2-D vector and <x,y,z,1> for 3-D
![Page 18: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/18.jpg)
Matrix math
2-D dimensional array Matrix multiplication
F x Gdot product of
• rows of F• columns of G
corrolary• multiplication only works• if cols of F = rows of G
333231
232221
131211
aaaaaaaaa
![Page 19: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/19.jpg)
Matrix math
Can also multiply a vector times a matrixv x M
Resulta new vectoreach entry
• a dot product of v with a different row of Mlength of v
• must equal number of cols in M
![Page 20: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/20.jpg)
Example
rotation around the origin by angle θ matrix
multiply by current vector <1, 1> becomes <1, 1, 1>
answer x = cos(θ) – sin (θ) + 0 y = sin(θ) + cos(θ) + 0 z = 1
1000)cos()sin(0)sin()cos(
![Page 21: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/21.jpg)
x' = x cos(θ) – y sin(θ) y' = x sin(θ) + y cos(θ)
(1, 2)
θ = -π / 4
(2.12, 0.70)
![Page 22: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/22.jpg)
Translation
Achieved by adding values at the ends of the top two rows
This rotates and translates by (3, -4)
1004)cos()sin(3)sin()cos(
![Page 23: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/23.jpg)
Efficiency
Mathematical operations are expensive especially trigonometric ones (inverse cosine) also square root multiplication and division, too
Normalization is particularly expensive square root, squaring and division
We want to consider ways to make our calculations faster especially if we are doing them a lot
![Page 24: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/24.jpg)
Squared space
We can do calculations in "squared space"never have to do square roots
Exampletest whether one vector is longer than
another• sqrt(X12+Y12) > sqrt(X22+Y22)• X12+Y12 > X22+Y22
• both will give the same result
![Page 25: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/25.jpg)
Exercise
![Page 26: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/26.jpg)
Kinematics
the physics of motion we worry about this a lot in computer
games• moving through space, collisions, etc.
our NPCs have to understand this too in order to avoid stupidity
![Page 27: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/27.jpg)
Basic kinematics
position a vector in space (point position) for a large object we use a convenient point
• classically the center of mass velocity
the change of position over time• expressed as a vector
pt+1 = pt + vΔt if we define the time interval Δt to be 1 we avoid a multiplication
![Page 28: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/28.jpg)
Basic kinematics II
Acceleration change in velocity over time
• also expressed as a vector vt+1 = vt + a Δt Pair of difference equations
![Page 29: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/29.jpg)
Numerical methods
We want to run a simulation that shows what happens But now we have a problem
there is a lag of 1 time step before acceleration impacts position
not physically accurate Example
deceleration to stop t = 0, d = 0, v = 10, a = 0 t = 1, d = 10, v = 10, a = -5 t = 2, d = 20, v = 5, a = -5 t = 3, d = 25, v = 0, a = 0
Correct distance traveled Δd = v0 Δt + ½ a Δt 2
Δd = 10 d = 20
![Page 30: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/30.jpg)
We can improve our simulation finer time step (expensive) improved estimation methods
for example instead of using vt, we can use the average of vt and vt+1
Example t=0, d=0, v=10, a =0 t=1, d=10, v=10, a = -5 t=2, d=17.5, v=5, a=-5 t=3, d=20, v=0, a=0
better we slow down in the first time step correct final answer but only works if acceleration is constant take GAM 350 for better ways
![Page 31: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/31.jpg)
Another derivation
time
velo
city
v(t)
v(t+1)
![Page 32: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/32.jpg)
What does this have to do with AI? Imagine a NPC
he has to decide how hard to throw a grenade
that judgment has to be based on an estimate of the item’s trajectory
When NPC take physical actions we have to know what the parameters of
those actions should be very often physics calculations are required not as detailed as the calculations needed to
run the game
![Page 33: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/33.jpg)
Approximation in Prediction
NPC will frequently need to predict the future where will the player be by the time my rocket gets
there? Assumptions
current velocity stays constant we know the speed of the rocket
Correct way vector algebra lots of square roots and trig functions
Typical approximation use the current distance won't change that much if the rocket is fast
![Page 34: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/34.jpg)
Accuracy?
Accuracy may be overrated for NPC enemies
We want an enjoyable playing experience no warning sniper attack is realistic
• but no fun a game has to give the player a chance
Built-in inaccuracy many games have enemies deliberately miss on the
first couple of shots others build random inaccuracy into all calculations others use weak assumptions about physics
• simplify the calculations• and give a degree of inaccuracy
![Page 35: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/35.jpg)
Force
Netwon's lawF = m*a
In other wordsacceleration is a function of force and
mass Force is also a vector quantity
a force acting along a vectorimparts a velocity along that vector
![Page 36: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/36.jpg)
Example
When a bat hits a ballwe will want to determine the force
imparted to the ball• we could simulate this with the mass of
the bat and its speed• more likely, we would just have a built-in
valuedirection of the force
• we need to know the angle of the bat when it strikes the ball
![Page 37: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/37.jpg)
More complex motions
To handle rotationwe also have to worry about torque
and angular momentum For example
if a rocket hits the back of a car• it will spin• if it hits the center, it will not
![Page 38: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/38.jpg)
Torque
Force applied around the center of mass of an objecttorque gives rise to angular
acceleration (spin)t = d F = I α
d = moment arm
![Page 39: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/39.jpg)
For our purposes
We will ignore rotational motiondeal only with forces acting on points
• typical simplification for AI
![Page 40: Math / Physics 101](https://reader033.vdocuments.mx/reader033/viewer/2022061617/56816662550346895dd9ee7c/html5/thumbnails/40.jpg)
Thursday
Finite state machines