done already for your convenience! telerik school academy unity 2d game development

29
2D Physics Engine Done already for your convenience! Telerik School Academy http://academy.telerik.com Unity 2D Game Development

Upload: isabel-phelps

Post on 19-Dec-2015

225 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

2D Physics EngineDone already for your convenience!

Telerik School Academyhttp://academy.telerik.com

Unity 2D Game Development

Page 2: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Table of Contents

1. 2D Physics Overview

2. RigidBody 2D

3. Colliders 2D

1.Circle Collider 2D

2.Box Collider 2D

3.Polygon Collider 2D

4.Edge Collider 2D

4. Forces

2

Page 3: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

2D Physics OverviewRigidbodies, Colliders,

Joints, etc.

Page 4: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

2D Physics Overview

All physics elements are normal components

Just add them toa game object

Set all needed properties The physics engine

will start using them Enjoy the effects

4

Page 5: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

2D Physics Overview Rigidbodies

Main component for the Physics Engine to start

With Rigidbody the object will start to respond to gravity

If you enable Rigidbody, you should not try to change the Transform part of the object

Instead, you should apply forces and let the engine do the heavy lifting

If you need non-moving by the engine Rigidbody, check “Is Kinematic” option

5

Page 6: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

2D Physics Overview Colliders

Define the shape of an object in terms of physical collisions

Most of the time invisible Use Box Collider and Circle Collider

whenever possible for performance reasons

Otherwise use Polygon Collider Colliders with Rididbody are

dynamic colliders Colliders without Rigidbody are the

static environment in your game – walls, floor, etc.

6

Page 7: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

2D Physics Overview Physic Materials

Materials define the physics collision effect

For example – ice is slippery, rubber has friction

Usually parameters are found by trial-error

In 2D there are two parameters Friction – friction coefficient Bounciness – the degree to which

collisions rebound from the surface 0 – no bounce effect

1 – full bounce effect with no loss of energy

7

Page 8: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

2D Physics Overview Joints

Attaching two rigidbodies to a fixed point

Allow restricted movement

Rotation by the three axes

Translation by the three axes

Stretching like a spring

You can set joint to break if certain force is applied

8

Page 9: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

2D Physics Overview Different Types of Colliders

Trigger Collider – does not respond to physics calculations but sends event to which you can subscribe from the scripts OnCollisionEnter2D /

OnTriggerEnter2D – when two colliders starts colliding for the first time

OnCollisionStay2D / OnTriggerStay2D – when two colliders are colliding

OnCollisionExit2D / OnTriggerExit2D – when colliders are no longer colliding

Simply select “Is Trigger”

9

Page 10: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

2D Physics Overview Different Types of Colliders

Static Collider – object with collider but no Rigidbody Used for level geometry and should

never move

Should not be disabled or enabled

If you do, major performance decrease may occur

Also – calculation errors in the engine may occur

If you want to alter it – add Rigidbody to it

10

Page 11: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

2D Physics Overview Different Types of Colliders

Rigidbody Collider – object with collider and Rigidbody

Most common case

Fully simulated by the physics engine

Reacts to collisions

Reacts to forces from script

Collide with all other objects11

Page 12: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

2D Physics Overview Different Types of Colliders

Kinematic Rigidbody Collider – object with collider and Kinematic Rigidbody

Has “Is Kinematic” property set

Does not react to collisions and forces from the engine

Should be moved by a script

They behave like static colliders but can be enabled/disabled/moved when needed

12

Page 13: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

RigidBody 2DHow to use it?

Page 14: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

RigidBody 2D Rigidbody 2D

Attach it to objects controlled by the physics

In 2D only the XY plane takes effect for translation and the Z axis for rotation

14

Page 15: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

RigidBody 2D Properties of RidigBody 2D

Mass – mass of the object Linear Drag / Angular Drag –

coefficient for movements Gravity Scale – whether the gravity

should be more or less on this object

Fixed Angle – can be rotated or not? Is Kinematic – is the body moved by

forces and collisions

15

Page 16: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Box Collider 2DHow to use it?

Page 17: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Box Collider 2D Box Collider 2D

Rectangular shaped collider

Attached to an game object

With or without a sprite

17

Page 18: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Box Collider 2D

Properties of Box Collider 2D

Material – the material to use for the collider

Is Trigger – whether events should be sent

Offset – local offset of the collider geometry

Size – the size of the rectangular shape 18

Page 19: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Circle Collider 2DHow to use it?

Page 20: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Circle Collider 2D Circle Collider 2D

Circular shaped collider

Attached to an game object

With or without a sprite

20

Page 21: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Circle Collider 2D

Properties of Circle Collider 2D

Material – the material to use for the collider

Is Trigger – whether events should be sent

Offset – local offset of the collider geometry

Radius – the radius of the circular shape 21

Page 22: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Polygon and Edge Colliders

2DHow to use them?

Page 23: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Polygon and Edge Colliders 2D

Polygon Collider 2D and Edge Collider 2D

Complex shaped collider

Attached to an game object

With or without a sprite

Difficult to compute – performance impact

23

Page 24: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Circle Collider 2D

Properties of Polygon Collider 2D and Circle Collider 2D

Material – the material to use for the collider

Is Trigger – whether events should be sent

Offset – local offset of the collider geometry

24

Page 25: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

ForcesHow to use them?

Page 26: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Forces

Two types of forces Constant Force 2D Effectors (Area Effector 2D for

example) They both are added to a

Rigidbody 2D Applied every Update of the game Good for in-game mechanics Have acceleration options

26

Page 27: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Summary

Rigidbody – component used by the physics engine for calculations

Colliders – components to register any colliding objects in the game

Different types of colliders – static, kinematic, etc.

Other physics elements – joint, forces, etc.

27

Page 28: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

форум програмиране, форум уеб дизайнкурсове и уроци по програмиране, уеб дизайн – безплатно

програмиране за деца – безплатни курсове и уроцибезплатен SEO курс - оптимизация за търсачки

уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop

уроци по програмиране и уеб дизайн за ученициASP.NET MVC курс – HTML, SQL, C#, .NET, ASP.NET MVC

безплатен курс "Разработка на софтуер в cloud среда"

BG Coder - онлайн състезателна система - online judge

курсове и уроци по програмиране, книги – безплатно от Наков

безплатен курс "Качествен програмен код"

алго академия – състезателно програмиране, състезания

ASP.NET курс - уеб програмиране, бази данни, C#, .NET, ASP.NETкурсове и уроци по програмиране – Телерик академия

курс мобилни приложения с iPhone, Android, WP7, PhoneGap

free C# book, безплатна книга C#, книга Java, книга C#Дончо Минков - сайт за програмиранеНиколай Костов - блог за програмиранеC# курс, програмиране, безплатно

?

? ? ??

?? ?

?

?

?

??

?

?

? ?

Questions?

?

2D Physics Engine

http://academy.telerik.com

Page 29: Done already for your convenience! Telerik School Academy  Unity 2D Game Development

Free Trainings @ Telerik Academy

C# Programming @ Telerik Academy csharpfundamentals.telerik.com

Telerik Software Academy academy.telerik.com

Telerik Academy @ Facebook facebook.com/TelerikAcademy

Telerik Software Academy Forums forums.academy.telerik.com