haroon alam
DESCRIPTION
TRANSCRIPT
Physics, Rigidbody Simulation in Unity3D
Syed Haroon Alam
Pre Requisite:Basic understanding of game development, unity3d and scripting language
Introduction• Physics in Game Development: Involves the laws of physics into
a game engine for the purpose of making effects more real to the observer
• Game Physics in Unity3D: Built in physics library and components to produce results in real-time that replicate real world physics
Physics engines used in unity:
• NVIDIA PhysX Physics engine - 3D game development• Box2D physics system - 2D game development
Components
Built in physics components include:
• Colliders
• RigidBodies
• Joints • Physics materials
Colliders
• Colliders are used to define the collision shape of objects in your scene
• Unity primitive colliders: Box, Sphere and Capsule
• Mesh Collider can be used for complex meshes
• Wheel Collider component for vehicles
• Terrain Collider component for terrains.
• A concave polygon always have an interior angle with a measure that is greater than 180 degrees
• A convex polygon has internal angle is less than or equal to 180 degrees. Every line segment between two vertices remains inside or on the boundary of the polygon.
Trigger Colliders
• Colliders can be used as a Trigger by setting the Is Trigger property to true
• Not participate in collision simulations
• Fire OnTriggerEnter, OnTriggerStay, and OnTriggerExit events
Rigidbodies
• Used For objects to act under the influence of Physics.
• Forces and torques to the Rigidbody component can be applied from scripts
• Requires a Collider component to be present on the GameObject for correct Physics simulation
• Can be Physics controlled or Kinematic controlled
RigidbodiesCollision Detection
• Discrete:- Uses Discreet collision detection against all other colliders in the scene.- Skips detection when an object is on one side of a collider in one frame, and
already passed the collider in the next frame
• Continuous:- Uses Continuous Collision Detection against colliders with no rigidbody- Uses CCD against rigidbodies set to Continuous Dynamic- User Discrete collision detection for all other colliders
• Continuous Dynamic:- Uses CCD on rigidbodies that are either set to Continuous or Continuous
Dynamic and colliders with no rigidbody- Use only for fast moving objects- User Discrete collision detection for all other colliders
Continuous vs Discrete collisionsFrame - 1 Frame - 2 Frame - 3
Using continuous and continuous dynamic
Moving rigidbodies
• If isKinematic is enabled, the object will not be driven by the physics engine, and can only be manipulated by its Transform
• FixedUpdate() should be used instead of Update() when dealing with Rigidbody
• rigidbody.MovePosition
• rigidbody.AddForce
• rigidbody.AddTorque
Rigidbody Colliders
• Colliders that also have a Rigidbody will react to collisions
• Contains information about contact points, impact velocity, rigidbody and collider
• Fire OnCollisionEnter(), OnCollisionStay(), and OnCollisionExit() events
Physics Material
• Reaction of physically controlled objects on each other.
• Used to get desired physical behavior on collisions
• Unity provides a few default Physic Materials
Directional Friction
Character Controller• Used on characters i.e for third-person or first-person player
control
• Does not require a Rigidbody
• Does not react to forces on its own and it does not automatically push Rigidbodies away
• Character Controller colliding with non-static Rigidbodies must be done via script
• Events: OnControllerColliderHit()
Joints
• Joint is a constraint that is defined between two Rigidbodies
• Hinge Joint• Spring Joint• Fixed Joint• Configurable Joint
Joints
• To constrain one Rigidbody to another along a particular axis
• Spring Joint can be used to create an invisible spring that tries to keep two rigid bodies together.
• Fixed Joint can be used to keep two Rigidbody objects together at a constant distance and orientation.
• Configurable Joint allows you to create complex joint configuration
Raycasting
• Shooting ray from specific direction• Use to check if collision is detected
Example Project
Upcoming Lectures
• Wheel Colliders and Car Racing example game• Particle Systems• Rag dolls and Cloth Simulation
Assignment for next lecture – Make simple pool game
Famous Physics based mobile gamesPaper Toss Angry Birds
Cut the Rope