Top Banner
Physics, Rigidbody Simulation in Unity3D Syed Haroon Alam Pre Requisite : Basic understanding of game development, unity3d and scripting language
23

Haroon alam

Dec 02, 2014

Download

Technology

Syed Alam

 
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Haroon alam

Physics, Rigidbody Simulation in Unity3D

Syed Haroon Alam

Pre Requisite:Basic understanding of game development, unity3d and scripting language

Page 2: Haroon alam

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

Page 3: Haroon alam

Components

Built in physics components include:

• Colliders

• RigidBodies

• Joints • Physics materials

Page 4: Haroon alam

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.

Page 5: Haroon alam

• 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.

Page 6: Haroon alam

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

Page 7: Haroon alam

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

Page 8: Haroon alam

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

Page 9: Haroon alam

Continuous vs Discrete collisionsFrame - 1 Frame - 2 Frame - 3

Page 10: Haroon alam

Using continuous and continuous dynamic

Page 11: Haroon alam

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

Page 12: Haroon alam

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

Page 13: Haroon alam

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

Page 14: Haroon alam

Directional Friction

Page 15: Haroon alam

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()

Page 16: Haroon alam

Joints

• Joint is a constraint that is defined between two Rigidbodies

• Hinge Joint• Spring Joint• Fixed Joint• Configurable Joint

Page 17: Haroon alam

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

Page 18: Haroon alam

Raycasting

• Shooting ray from specific direction• Use to check if collision is detected

Page 19: Haroon alam

Example Project

Page 20: Haroon alam

Upcoming Lectures

• Wheel Colliders and Car Racing example game• Particle Systems• Rag dolls and Cloth Simulation

Page 21: Haroon alam

Assignment for next lecture – Make simple pool game

Page 22: Haroon alam

Famous Physics based mobile gamesPaper Toss Angry Birds

Cut the Rope

Page 23: Haroon alam

Thank You

Please feel free to drop me note at:[email protected]