Transcription
Principles of Computer GameDesign and ImplementationLecture 7
We already Knew Viewport, renderingHow to position an objectTranslation of an objectSome basic vector operation (sum, subtract)Scene graphBook sample code in Vital2
Outline for Today Movement Code for Rotation3
Movement in Space We used vectors to specify the position of anobject in space. Vectors are also used to specify the directionof movement– (and other purposes, e.g., lightening, physics, etc.)4
Uniform Motion An object moves– starting from point P0– with a constant speed– along a straight liney. P0VP(t) P0 t VxPosition in time t5
Vector Speed Motion equationP(t) P0 t V– V specifies direction and speed2Vy. P0VxTwice as fast than this6
Main Loop In a game engine we do nothave access to continuous timeStartInitialiseUpdate Game Every iteration update thepositionP P VDraw SceneAre wedone?CleanupEnd7
jMonkeyEngine Create two boxes and then public void simpleUpdate(float tpf) {b.move(new Vector3f(1,0,0).mult(0.005f));c.move(new Vector3f(2,1,0).mult(0.005f));}8
Motion Speed How to make the objects move in anydirection with the same speed? Given a vector, we need to be able to keep thedirection but make its length 1.9
Length of a 2D VectorycbPythagoras theoremax Given a 2D vector V (xv,yv) its lengthE.g.10
A Unit (Direction) Vector A vector of length ONE is called a unit vector One can always normalise a vector?11
Length of a 3D Vector Given a 3D vector V (xv,yv,zv) its lengthVector normalisation12
Vector NormalisationVector3f v new Vector3f(1,2,3);float l v.length();Vector3f u v.clone().mult(1/l);c.move(u.mult(.01f));13
But then Vector3f v newVector3f(1,2,3);Vector3f u v.normalize();float speed 0.1f; // arbitraryc.move(u.mult(speed));14
Main Loop Every iteration update thepositionStartInitialiseUpdate GameP P speed U U is a unit vectorDraw SceneAre wedone?CleanupDifferent speed on different hardware!End15
Welcome TPF simpleUpdate can use a time-per-framecounterc.move(u.mult(tpf));16
Uniform Motion Every iteration update thepositionStartInitialiseUpdate GameP P speed tpf U U is a unit vector speed is speed tpf is time per frameDraw SceneAre wedone?CleanupEnd17
Arbitrary Translation Every iteration update thepositionStartInitialiseUpdate GameP P speed tpf U(t) U(t) - the direction of movementDraw SceneAre wedone?– Depends on time!! speed is speed tpf is time per frameCleanupEnd18
Rotation Rotating is harder than translating We will look at the maths of it in the nextlecture For now, let’s talk about coding19
Quaternions We could have studies what quaternions areQuaternion is a “thing” that helps rotate objects.20
simpleInitApp() Box box new Box(1, 1, 1);b new Geometry("Box", b); 21
Example Vector3f axis new Vector3f(1, 2, 3);Quaternion quat new Quaternion(); public void simpleUpdate(float tpf) {quat.fromAngleAxis(tpf, axis);b.rotate(quat);} 22
Demo23
But Then b.rotate(pitch, yaw, roll);also ns/7/7e/Rollpitchyawplain.png
A Simple Exampleb.rotate(tpf*10*FastMath.DEG TO RAD,0,0);Turns b at the rate of 10 degrees per secondaround the X axis25
Complex Motion Example A moon rotating around a planet26
simpleInitApp()Sphere a new Sphere(100, 100, 1);earth new Geometry("earth", th);Sphere b new Sphere(100, 100,0.3f);moon new Geometry("moon", 3, 0, 0);27
simpleUpdate()public void simpleUpdate(float tpf) {quat.fromAngleAxis(tpf, axis);moon.rotate(quat);}28
Let’s Run ItOOPS!29
What Went Wrong In jME rotation and translationare independentrootNode The moon rotates about it’scentre Scene graph to the rescue! pivotNodeplanetThe pivotNode is thecentre of rotationmoon30
Demo31
Code Snippetprivate Node pivotNode new Node(“PN"); public void simpleInitApp() { pivotNode.attachChild(moon); }public void simpleUpdate(float tpf) {quat.fromAngleAxis(tpf, axis);pivotNode.rotate(quat);}32
Pivot Node ExplainedYLocal translationXZ33
Pivot Points While it is possible to specify the exactposition of a geometry, it is often muchsimpler to introduce a series oftransformations associated with internalnodes of a scene graph.34
Principles of Computer Game Design and Implementation Lecture 7. We already Knew Viewport, rendering How to position an object Translation of an object Some basic vector operation (sum, subtract) Scene graph Book sample code in Vital 2. Outline for Today Movement Code for Rotation 3. Movement in Space We used vectors to specify the position of an object in space .