Presentation is loading. Please wait.

Presentation is loading. Please wait.

Further Programming for 3D applications CE00849-2 Motion, Fundamental Physics and Collision Detection Bob Hobbs Faculty of Computing, Engineering and Technology.

Similar presentations


Presentation on theme: "Further Programming for 3D applications CE00849-2 Motion, Fundamental Physics and Collision Detection Bob Hobbs Faculty of Computing, Engineering and Technology."— Presentation transcript:

1 Further Programming for 3D applications CE00849-2 Motion, Fundamental Physics and Collision Detection Bob Hobbs Faculty of Computing, Engineering and Technology Staffordshire University

2 Hierarchy of Models Geometry Physics Bio-Mechanics Behaviour

3 Transformations in OpenGL ► Modeling  Translate  Rotate  Scale  Shear ► Viewing  orient camera  projection ► Animation ► Map to screen

4 Camera paradigm for 3D viewing 3D scene Camera 2D picture Content of 2D picture will depend on: camera parameters (position, direction, field of view,...), properties of scene objects, illumination,... ► 3D viewing is similar to taking picture with camera:  2D view of 3D scene

5 Model description ► Models are used to represent the 3D things we are simulating ► A standard way of defining models is needed ► Model made up of points and lines joining the lines to form faces ► A co-ordinate system is used to represent the points

6 Coordinate Systems ► World coordinate system: reference frame for specification of (relative) position / orientation of viewer and scene objects (size?) xwxw zwzw ywyw Scene (head looking at bird) xmxm zmzm ymym Head model xmxm zmzm ymym Bird model

7 Coordinate Systems ► Viewing coordinate system: reference frame for specification of scene from viewpoint of camera / viewer xwxw zwzw ywyw xmxm zmzm ymym xmxm zmzm ymym Taking a view of scene (head looking at bird) Camera yvyv zvzv xvxv

8 3D to 2D ► The next part of the process has to take the image from viewpoint and calculate the way the 3D shapes that can be seen can be drawn on a 2D surface. ► Any surfaces not seen are eliminated. ► Involves a mathematical process of manipulating and generating resultant 2D vertices

9 xwxw zwzw ywyw World coordinates Viewing Pipeline Coordinate transformations:  generation of 3D view involves sequence (pipeline) of coordinate transformations Camera Modelling coordinates 3D object 2D picture Device coordinates xmxm zmzm ymym xmxm zmzm ymym xmxm zmzm ymym xvxv zvzv yvyv Viewing coordinates

10 Camera Analogy ► 3D is just like taking a photograph (lots of photographs!) camera tripod model viewing volume

11 Camera Analogy and Transformations ► Projection transformations  adjust the lens of the camera ► Viewing transformations  tripod–define position and orientation of the viewing volume in the world ► Modeling transformations  moving the model ► Viewport transformations  enlarge or reduce the physical photograph

12 Coordinate Systems and Transformations ► Steps in Forming an Image  specify geometry (world coordinates)  specify camera (camera coordinates)  project (window coordinates)  map to viewport (screen coordinates) ► Each step uses transformations ► Every transformation is equivalent to a change in coordinate systems (frames)

13 Affine Transformations ► Want transformations which preserve geometry  lines, polygons, quadrics ► Affine = line preserving  Rotation, translation, scaling  Projection  Concatenation (composition)

14 Homogeneous Coordinates  each vertex is a column vector  w is usually 1.0  all operations are matrix multiplications  directions (directed line segments) can be represented with w = 0.0

15 3D Transformations ► A vertex is transformed by 4 x 4 matrices  all affine operations are matrix multiplications  all matrices are stored column-major in OpenGL  matrices are always post-multiplied  product of matrix and vector is

16 Specifying Transformations ► Programmer has two styles of specifying transformations  specify matrices ( glLoadMatrix, glMultMatrix )  specify operation ( glRotate, glOrtho ) ► Programmer does not have to remember the exact matrices

17 Programming Transformations ► Prior to rendering, view, locate, and orient:  eye/camera position  3D geometry ► Manage the matrices  including matrix stack ► Combine (composite) transformations

18 vertexvertex Modelview Matrix Projection Matrix Perspective Division Viewport Transform Modelview Projection object eye clip normalized device window ► other calculations here  material  color  shade model (flat)  polygon rendering mode  polygon culling  clipping Transformation Pipeline

19 Matrix Operations Specify Current Matrix Stack glMatrixMode( GL_MODELVIEW or GL_PROJECTION ) Other Matrix or Stack OperationsglLoadIdentity()glPushMatrix()glPopMatrix() Viewport usually same as window size viewport aspect ratio should be same as projection transformation or resulting image may be distorted glViewport( x, y, width, height )

20 Projection Transformation ► Shape of viewing frustum ► Perspective projection gluPerspective( fovy, aspect, zNear, zFar ) glFrustum ( left, right, bottom, top, zNear, zFar ) ► Orthographic parallel projection glOrtho( left, right, bottom, top, zNear, zFar ) gluOrtho2D( left, right, bottom, top ) ► calls glOrtho with z values near zero

21 Applying Projection Transformations ► Typical use (orthographic projection) glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( left, right, bottom, top, zNear, zFar );

22 Viewing Transformations ► Position the camera/eye in the scene  place the tripod down; aim camera ► To “fly through” a scene  change viewing transformation and redraw scene ► gluLookAt( eye x, eye y, eye z, aim x, aim y, aim z, up x, up y, up z )  up vector determines unique orientation tripod

23 Modeling Transformations ► Move object glTranslate{fd}( x, y, z ) ► Rotate object around arbitrary axis glRotate{fd}( angle, x, y, z )  angle is in degrees ► Dilate (stretch or shrink) or mirror object glScale{fd}( x, y, z )

24 Connection: Viewing and Modeling ► Moving camera is equivalent to moving every object in the world towards a stationary camera ► Viewing transformations are equivalent to several modeling transformations gluLookAt() has its own command can make your own polar view or pilot view

25 Projection is left handed ► Projection transformations ( gluPerspective, glOrtho ) are left handed  think of zNear and zFar as distance from view point ► Everything else is right handed, including the vertexes to be rendered x x y y z+ left handedright handed

26 Hierarchy of Models Geometry Physics Bio-Mechanics Behaviour

27 Basic Physics Modelling ► Fundamental Laws ► Gravity ► Friction ► Collision Response ► Forward Kinematics ► Particle Systems

28 The Fundamental Laws ► Mass (m)  Mass = number of atomic units (not weight)  Measured in Kilograms  Forces act on a mass – Newtons -> weight ► Time (t)  In a virtual environment is related to frame rate not real time  If system slows time slows

29 Centres of Mass ► Position (s)  X,y,z co-ords in 3D space  An object is positioned by its centre of mass For complex masses centre less obvious

30 ► To calculate centre of mass of object can use a mass for each vertex (n vertices)  X value = sum of x pos * mass (from 1 to n) divided by sum of masses divided by sum of masses  Y value = sum of y pos * mass (from 1 to n) divided by sum of masses divided by sum of masses Bounding shapes make easier computation

31 ► Velocity (v)  Velocity = ds/dt  X pos = x pos + x velocity (pixels per frame)  Virtual velocity = pixels/second(for a set fps) since we are redrawing using a frame rate  New position = old position + velocity * time x 0 Time = t 0 x 1 = x 0 =v 0 (t 2 -t 1 ) Time = t 1

32 ► Acceleration (a)  Rate of change of velocity with time vel Time -> vel Time -> vel Time -> Constant velocity(a =0) Acceleration a = constant Non-constant accel a = f(t)

33 ► After t secs  X t = x 0 + v 0 *t + ½*a*t 2  E.g. Object starts at pos 50, start velocity is 4 units per frame and acceleration is 2 units per frame 2  New position x t = 50 + 4*t + 0.5*2*t*t assuming start at frame 0 and t is no. of frames assuming start at frame 0 and t is no. of frames

34 ► Force (f)  Force = mass * acceleration (F= m*a)  Measure in Newtons (Kg*m/s 2 )  Acceleration = Force / mass  Used in simulations to ► Work out acceleration of objects when hit with a force ► Forces on colliding objects ► Acceleration given to different mass objects when hit with same force  Forces act in all three dimensions and are represented as vectors.  Usually decompose all forces into 3 vectors and add all forces acting on an object in each axis

35 ► Momentum (P)  P = m * v  Force is the rate of change of momentum  So if a small object travelling fast comes to a halt it will impart the same force as a large object travelling slow (principle of jets and rockets)

36 ► Conservation of momentum  If a ball bounces of a wall and returns at same velocity – inelastic collision – momentum conserved ► Momentum transfer  Usually some energy is absorbed in the form of heat and vibration – momentum is lost – ball returns at lower velocity.  Ignored in VR or game simulations

37 Simple Bounce Physics ► Given an object with initial velocity (vx,vy), if this object collides with another object of greater mass, then the collision is simplified, we only need to predict the behaviour of the colliding object. ► Consider the balls on a pool table…

38 Simple Bounce Physics ► When a ball hits a cushion, then it reflects of the side at an angle that is equal and opposite to the incident angle.  Normal

39 Simple Bounce Physics (+vy,-vx) (-vy,-vx) (-vy,+vx) (+vy,+vx)

40 Further Collision Physics ► Two objects each with their own mass, when they collide we need to calculate the final trajectory and velocity. ► Using the law of the conservation of momentum we obtain ► For this problem we used the conservation of kinetic energy

41 Further Collision Physics ► Manipulating this equation we obtain

42 Newton’s First Law ► An object at rest remains at rest ► An object in motion remains in motion (i.e. at constant velocity) until an exterior force acts upon the object

43 Object Pair Collision Detection ► Vital component of interaction ► Describe Exhaustive Test for when two object intersect (process hungry) ► Try to avoid doing exhaustive test if possible

44 Exhaustive Test ► Assume all objects as collection of triangles (polygons) ► Object 1 consists of m triangles ► Object 2 consists of n triangles ► Use triangle intersection test to test all possible pairs of of intersections ► This requires n.m triangle-triangle tests

45 Triangle Intersection Test Moller 1997 comparison of triangles A and B 1. They do not intersect if all vertices in A lie to one side of plane of B and V.V 2. Otherwise plane of A intersects plane of B on L 3. Find line intersection of L with A ( L A ) and L with B ( L B ) 4. A and B intersect only if L A and L B overlap A B LBLB L LALA

46 Basic Rejection Tests ► Simplest tests based on distance ► Each scene object has a bounding sphere. Two objects cannot overlap if distance between two centres is > than sum of the radii ► Better test id the separating plane test. If a plane can be drawn such that all points of one object lie on one side and all points of the other on the reverse, cannot collide. Key ids to find a good separating plane ► Bounding Box range test

47 General Collision Detection ► Detecting collision between a set of n objects generates n 2 possible pairs of objects requiring testing for overlap ► Use spatial partitioning to discard as may pairs as possible and use object pair collision tests on remaining pairs ► Uniform Space Subdivision

48 Space Subdivision

49 Modelling Gravity effects ► Gravity is a result of a distortion of space by a large body, but is experienced as a special force unique to earth. ► Two cases to consider  Two or more objects with same relative mass  One object has much greater mass than other ► F = G*m1*m2/r2  Where G is 6.67*10 -11 Nm/kg 2  And m1, m2 are masses, r is the distance between centres

50 Modelling Projectile Trajectories  T = Viy* sin  g g = 9.8 m/s 2 Vix = V* cos  Viy = V* sin  Xhit = Vix * Viy /g Vi

51 Friction ► Friction causes deceleration ► Assumed to be a constant deceleration ► Friction on a flat surface is modelled by applying a virtual friction ► Each frame the velocity is reduced by a constant factor to match to the desired friction (Vnew = Vold – friction)

52 Friction ► Usual modelled as frictional force. ► If you try to push a mass in a direction parallel to the plane, you will encounter frictional force. Push Friction

53 Friction ► Definition of static Frictional force. ► Where m=mass,g=gravity and  s is the static frictional coefficient. ► If force is applied that is greater than the frictional force, the object will move. ► When the object is in motion, the frictional coefficient decreases to the coefficient of kinetic friction

54 Modelling Friction ► Modelling Friction on a flat surface requires a constant negative velocity be applied to all objects that is proportional to the required friction. ► V new =V old -Friction. ► In which case it is necessary to ensure that velocity stays positive.

55 Friction on an Inclined Plane ► Friction and Gravity work in similar ways.  +x+x -x-x +y+y -y-y

56 Friction on an Inclined Plane ► The plane has coefficients  s and  k for static and kinetic cases. ► The equilibrium case is when the sum of the forces acting on the body is zero.

57 Friction on an inclined plane ► Normal force (  ) – the force that pushes the object back. ►  =mg ► The gravitational force must be opposite to the normal force  -mg=0

58 Friction on an Inclined Plane At what angle does gravity overcome friction?  +x+x -x-x +y+y -y-y

59 Friction on an Inclined Plane: Finding the Critical Angle ► Firstly we define an xy coordinate system on the inclined plane, with x parallel to the plane, and the positive part of x in the downward sliding direction. ► For the x-axis we know the force of gravity pushing the block is mg(sin . ► The force due to friction is –  s. ► The negative sign is because the force acts in the opposite direction.

60 ► Friction on an inclined plane  Different coefficient of friction for moving and static    s Static  k Kinetic(moving)

61 Inclined Friction ► (Normal force)  = m*g ►  - m*g = 0 (gravity opposes normal force) ► on an inclined plane force due to gravity = m*g*sin  ► Force due to friction holding block = -  *  s ► Since at the point of sliding these forces are equal, ► On the x - axis (m*g*sin  -  *  s = 0) ► On the y axis (  - m*g*cos  = 0) ► By substitution the angle at which the block starts to slide  critical = tan -1  s

62 Other Physics to consider ► Object-to-object collision (of irregular shaped objects) ► Kinematics ► Particulates


Download ppt "Further Programming for 3D applications CE00849-2 Motion, Fundamental Physics and Collision Detection Bob Hobbs Faculty of Computing, Engineering and Technology."

Similar presentations


Ads by Google