26
More PBM Ruth Aylett Overview ! Collisions ! Deformable objects – Hookes law – Cloth – Flesh ! Fluids ! Plants ! Physics engines

Lectures on Virtual Environment Development L12

Embed Size (px)

DESCRIPTION

Virtual Reality development is taking the world by storm. Follow all 16 Lecture Notes to learn how to build your own VR experiences. -By Ruth Aylett, Prof.Comp Sci. @ Heriot Watt University

Citation preview

Page 1: Lectures on Virtual Environment Development L12

More PBM

Ruth Aylett

Overview

!  Collisions !  Deformable objects

– Hookes law – Cloth – Flesh

!  Fluids !  Plants !  Physics engines

Page 2: Lectures on Virtual Environment Development L12

Collision response

!  When objects collide they apply forces on each other

!  Collisions can be: – elastic, where no kinetic energy is lost

during the collision –  inelastic, where kinetic energy is lost

Collision response

!  Consider a collision between 2 balls !  An impulse force, P, acts for a very

short time m1 ( v1

after - v1before ) = -f

m2 ( v2after - v2

before ) = f

!  Therefore m1v1

after + m2v2after = m1v1

before + m2v2

before

f

f

v1

v2

m1

m2

Page 3: Lectures on Virtual Environment Development L12

Collision response

!  We can define the velocity at which the 2 balls approach each other as:

vrelbefore = v1

before - v2before

!  and the speed at which they separate as: vrel

after = v1after - v2

after

!  Inelastic collisions can be simulated with a coefficient of restitution, e (between 0 and 1)

vrelafter = -e vrel

before

Collision response

!  We can calculate the impulse force, f, for a perfectly elastic collision

f = 2 m1 m2 * ((v1 x v2).n) * n

m1+m2

!  where n is the normal at the point of collision (equal and opposite for each ball)

!  for 2 spheres n is the normalised vector between the centres of the 2 spheres

Page 4: Lectures on Virtual Environment Development L12

Collision response

!  We can implement a simulation of collisions in the following way: –  test for collision (check distance between the

centres of the balls) –  if a collision occurs, calculate the impulse

force, P – add that force to the total force for the object

and continue the standard simulation loop

Deformable objects

!  Deformable or soft objects can also be simulated using physical laws –  jelly – cloth –  flesh

Page 5: Lectures on Virtual Environment Development L12

Deformable objects

!  Modelling Approach – Spring and dashpot elements – Hooke’s law – Replace polygon edges with springs – Extend to allow cutting and fracture

Hooke’s Law !  Theory

– Consider a spring with rest length, L

– Let the extension of the spring be !L

– Let the coefficient of restitution be k

L+!L L

Page 6: Lectures on Virtual Environment Development L12

Hooke’s Law

!  The force generated in the spring is given by:

f = -k !L L

–  The same expression works for spring compression, except !L is negative

–  The dashpot element is included in the system to add some drag, reducing total energy

Modelling cloth

!  To create a model of a deformable object we create a mesh with each edge made from a spring & dashpot

Page 7: Lectures on Virtual Environment Development L12

Modelling cloth

!  When a node on the mesh is moved, then all the connected spring elements will attempt to pull or push the node back

p1 p3

p4

p2

p

Modelling cloth

!  The new lengths of the springs will be:

L1 = |p - p1| L2 = |p - p2| L3 = |p - p3| L4 = |p - p4|

Page 8: Lectures on Virtual Environment Development L12

Modelling cloth

!  So the force on the node is: f1 + f2 + f3 + f4

where fi = k * (Li - L) * ( pi - p ) L

–  note that each fi points in the direction of the associated spring

–  once the forces have been added, the acceleration can be found ( f / m = a)

normalised

Modelling cloth

!  We can then use our standard simulation loop to calculate new velocities and positions for the node

!  When we have a mesh with many nodes we calculate each step on all nodes simultaneously

Page 9: Lectures on Virtual Environment Development L12

Problems

!  Retaining the original structure –  When these routines are applied to 3D objects,

they can easily lose their original shape

!  Solution –  Record the original locations of the object’s nodes –  Connect extra imaginary springs between the

original and current location of each node –  Use a smaller value of k for these imaginary

springs

Problems

!  The cloth is too floppy – 4 springs for each element of the mesh

isn’t enough to give rigidity

!  Solution – Add extra springs

Page 10: Lectures on Virtual Environment Development L12

Modelling flesh

!  Case with no cutting / fracture – same as for cloth, but create a 3D mesh

with cubic elements

Modelling flesh

!  Case with cutting and fracture – More complex, need to introduce a method

to cut the forces between nodes –  Introduce the concept of plates

• Each plate is a separate element • Each plate is connected to its neighbours by

filaments • Each filament is non elastic - it merely conducts

forces between neighbouring plate nodes

Page 11: Lectures on Virtual Environment Development L12

Plates

filament

plate

Controlling movement !  When no cut has taken place, the filaments

act to: –  keep the nodes of the plates together –  transmit forces from one node to another

!  Process is to calculate the forces on all nodes within each plate

!  If a node n1 from one plate and a node n2 from another plate are connected then apply the forces from n1 to n2 and vice versa

Page 12: Lectures on Virtual Environment Development L12

Cutting

!  To cut the flesh, merely delete the connections for appropriate filaments

!  To get the flesh to peel back when cutting / tearing, stretch the flesh before the simulation starts

Fracture

!  To simulate fracture (ripping, tearing), set a maximum force that the filament can conduct

!  If the total force conducted exceeds the fracture force then cut the filament automatically

!  By setting slightly different fracture forces for each filament, we get irregular ripping effects

Page 13: Lectures on Virtual Environment Development L12

Problems

!  This method can be unstable !  Physical values have to be chosen

carefully !  Structure is very important

Fluid simulation

!  Three methods used for this: – Lattice Boltzmann Method (LBM); – Navier-Stokes (NS) solvers – Smoothed Particle Hydrodynamics (SPH)

!  All require substantial amounts of computation

!  Simplified simulation also used: O’Brien & Hodgins

Page 14: Lectures on Virtual Environment Development L12

Computational Fluid Dynamics

!  Start with: – an arbitrary distribution of fluid – submerged or semi-submerged obstacles

!  Discretise the fluid volume into a 3D array of box shaped elements

CFD

!  Velocity and pressure are: – defined throughout the fluid – updated throughout the simulation, using a

set of finite difference expressions – used to drive a height field equation to

create a surface for liquids

Page 15: Lectures on Virtual Environment Development L12

Boundary conditions

!  Boundary conditions can be used to –  represent the edges of the mesh

• open, allowing fluid to flow in and out •  closed

–  represent the surface of a liquid –  represent stationary obstacles

•  sea bed •  rocks

CFD

!  A variety of effects occur naturally from this method –  waves

•  reflection •  refraction •  diffraction

–  rotational effects •  eddies •  vorticity •  splashing

Page 16: Lectures on Virtual Environment Development L12

CFD

!  We can visually represent the fluid in 2 ways – as a heightfield to represent the surface of

a liquid – as a collection of massless particles moving

through the fluid

Simple fluids

!  Navier Stokes provides an accurate, but computationally intensive solution

!  O’Brien and Hodgins provide an alternative solution which is – simpler –  less accurate –  less detailed

Page 17: Lectures on Virtual Environment Development L12

Simplified representation

!  This method starts by considering the fluid body as a 2D grid rather than a 3D grid

!  Each element represents a column of fluid

Connecting elements

!  Each 2D element is connected to its neighbours (or the outside world) by 8 ‘pipes’

!  The pipes can allow –  the fluid to flow freely through

from one element to another –  an element to have fluid

injected or extracted

Page 18: Lectures on Virtual Environment Development L12

Fluid flow in pipes

!  The equations to determine the fluid flow in the pipes are based on laws for hydrostatic pressure

!  The static pressure, H, in column i,j is Hij = hij"g + p0 + Eij

!  where –  h is the column height –  g is gravity –  " is the density of the fluid –  p0 is the atmospheric pressure –  Eij is an external pressure applied by other objects #

Constraints

!  We must also apply some constraints to the system – The flows at each end of a pipe must be

equal and opposite – A column may not have a negative volume

Page 19: Lectures on Virtual Environment Development L12

Checking pipes

!  To ensure that no column has a negative volume – at the end of each update, the volume of

each column is tested –  if the volume is negative, scale back the

pipes removing fluid from the column –  repeat this procedure until all columns

have a non-negative volume

Setting the boundaries

!  Columns that are on the edge of the grid have pipe connections to the outside world

!  As there is no column at the other end, we must specify the flow in the pipe – barrier - set the flow to zero –  fluid source - positive flow –  fluid sink - negative flow

Page 20: Lectures on Virtual Environment Development L12

Fluid surface

!  We can represent the fluid with a heightfield surface

!  The surface is made of triangles / quads, joining control points

Plants

!  Apart from L-Systems (Lindenmeyer) can also model plants with voxels – A voxel is like a 3D pixel

!  Voxel space automata (Greene)

Page 21: Lectures on Virtual Environment Development L12

Voxel automata

!  This technique models plants with stochastic (random) growth

!  The space in which the plant grows is discretised into voxels

!  This technique is particularly good for modelling climbing / rambling plants

Growing the plant

!  The growth of the plant is affected by – occlusion (availability of light) –  intersection (colliding with objects) – proximity (stick to walls, ramble over other

objects) !  These features are recorded within the

voxels

Page 22: Lectures on Virtual Environment Development L12

Probabilistic growth

!  The growth of the plant is probabilistic –  test several different options for the next

bit of growth –  for each option assess how good the

conditions are in the new position – choose and apply the best option

Light levels

!  Light levels are modelled with the assessment of occlusion

!  Each voxel assesses – amount of sunlight [ 0 .. 1 ] – amount of sky light [ 0 .. 1 ]

!  The amounts of these types of light are calculated using ray casting

Page 23: Lectures on Virtual Environment Development L12

Light from the sky

!  Sky light –  rays are cast from a voxel to points all over

the sky –  if the ray hits an object before it gets to

the sky it is occluded [0] – otherwise it is exposed [1] –  the average occlusion of all the rays can

then be calculated

Light from the sun

!  Sunlight –  the 180 degree arc forming the trajectory

of the sun is determined –  rays are cast from the voxel to the arc –  rays are assessed for occlusion / exposure

!  The average occlusion is calculated

Page 24: Lectures on Virtual Environment Development L12

Growth cycle

!  Growth is affected by the amounts of light available –  The plant has acceptable max, min light levels

specified

!  Also assess all voxels for the presence of objects within each voxel (including bits of the plant) –  Allows assessment of proximity, iintersection

!  Once we know the conditions for all voxels, we can proceed with one stage of growth

Updating growth

!  Once a stage of growth has occurred the voxel space is updated

!  Identify changes in – occlusion –  intersection – proximity

!  Then another stage of growth can be started

Page 25: Lectures on Virtual Environment Development L12

Physics engines

!  Graphics programmers do not want to learn physics –  So specialist graphics libraries seemed a really

good idea

!  Now incorporated into most games engines –  UT, Half-life, Unity etc –  Also in Director, Flash (2D), Flex, Blender

!  Physics Processing Unit (2006) –  A physics card in spirit of a graphics card –  Physics processing now part of Nvidia and ATI

graphics cards

Physics engines available

!  Commercial – Havok

– www.havok.com

• Which took over Ipion • Used in Director, Half-life

– NVidia took over Ageia Feb 2008 • PhysX engine and PPU

Page 26: Lectures on Virtual Environment Development L12

Free-or-nearly physics engines

!  ODE - Open Dynamics Environment –  Rigid body dynamics, C/C++ API, mature, big community

•  http://ode.org/

!  Bullet •  http://www.bulletphysics.com/Bullet/wordpress/

!  The Physics Engine •  http://www.thephysicsengine.com/

!  Newton game dynamics •  http://www.newtondynamics.com/

!  Probably many others… !  Physics Abstraction Layer - PAL

–  Interface to variety of physics engines •  http://www.adrianboeing.com/pal/