Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

32
Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah

Transcript of Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Page 1: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Unity3D PhysicsApril 3, 2015Comp 150 - Game DesignMichael Shah

Page 2: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Physics Topics● Physics Games

o Couple studies of physics gone well● Physics as the main mechanic

o Case study of games● Unity3D Physics

o Tweaking Physics● Physics Engines

o Further items to look at (and appreciate physics)

Page 3: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Realistic Physics● Goal is to simulate

the real world as close as possible.

Page 4: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Realism on a sliding Scale● Trials HD● Tony Hawk Pro

Skater

Page 5: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Cartoon Physics● Angry Birds● Flappy Bird● Mario

Page 6: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Another reason to Love Unity

Page 7: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Physics as A Mechanic● Half-Life 2 Gravity

Gun● Portal

Page 8: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Why we need good Physicshttps://youtu.be/NYt3B9lcUm0?t=4m49s (This game was probably never tested!)

Page 9: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Unity Physics - The Basics● Built on top of Nvidia’s PhysX System● Lets look at the built in components

Page 10: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Colliders● Box, Sphere, Capsule, and Mesh

o OnCollisionEnter, OnCollisionExit, OnCollisionStay● Colliders as Triggers

o OnTriggerEnter, OnTriggerExit, OnTriggerStay● Detecting a Collision requires at least one of the

colliding objects to have a: void OnCollisionEnter(Collider col) method.

Page 11: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Mesh Collider Example

Page 12: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Mesh Collider Example

Page 13: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Rigid Body● Used for moving objects● Gameobject is effected by Gravity

Page 14: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Force and Torque● Add Force

o Change movement of object● Add Torque

o Rotate object around axis.

Page 15: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Physic Materials● Physics Materials effect the way the object

reacts in the game engine (Basketball versus bowling bowls surface).o Dynamic friction: Effects the object as it moves.o Static Friction: How much force is needed to move

an object from a static position (sticky glue-like).o Bounciness: self-explanatoryo Friction and Bounce Combine: How to respond to

another object during collision.

Page 16: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Joints● Fixed

o Does not move until break force exceeded● Spring

o Hooke’s Law● Hinge

o Doors

Page 17: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Raycasting● Raycasting: The process of shooting an invisible ray

from a point to a specified direction into any colliders.o Use Debug.DrawRay to see where you are casting a

ray

Page 18: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Raycast Against Specific GameObjects

Page 19: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Puzzles● Some Exercises to walk through

o How would you implement the scene?o What types of objects would you construct?

What does the hierarchy look like?

Page 20: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Puzzle #1 - Destructible Wallhttps://www.youtube.com/watch?v=IjELu_Q01eQ

Page 21: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Puzzle #2 - Wrecking Ballhttps://www.youtube.com/watch?v=oAWGs0kt_vM

Page 22: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Puzzle #3 - Trampolinehttp://forum.unity3d.com/threads/trampoline-bouncy-spring-please-help.53181/

Page 23: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Puzzle #4 - Pool of Waterhttps://www.youtube.com/watch?v=mDtnT5fh7Ek

Page 24: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Puzzle #5 - Ragdoll

Page 25: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Puzzle #6 - Destructible Objects

https://www.youtube.com/watch?v=IXlA3HSIDPg

Page 26: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Puzzle #7 - Improve Performance of Complicated Geometry

http://unity3d.com/learn/tutorials/modules/intermediate/physics/physics-best-practices

Page 27: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Answer - Physics Manager

Page 28: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Best Practices● Remember that 2D is different than 3D in

Unityo All of those puzzles, the same features apply, but

use the 2D components!● http://devmag.org.za/2012/07/12/50-tips-for-

working-with-unity-best-practices/o 9. Put your world floor at y = 0. This makes it easier to put objects on the floor, and treat the world

as a 2D space (when appropriate) for game logic, AI, and physics.

Page 29: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Physics in Regards to Lighting

Page 30: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Physics in Regards to Animation

● Integrate Maya into the pipeline● ‘Apply Root Motion’ powers animated objects

based on animationo No need to move Transform or add Force

Page 31: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Animation as an Agent-Based System

● Flocking Behavioro Physics - Attract and Repel Objectso https://www.youtube.com/watch?v=EZEK1lXxFfA

● Crowd Simulation● Particle System

Page 32: Unity3D Physics April 3, 2015 Comp 150 - Game Design Michael Shah.

Appreciating the built-in PhysicsThree Books to build your own physics engine