games development 2 entity update & rendering co3301 week 2, part 1

19
Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Upload: debra-barnett

Post on 13-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Games Development 2Entity Update & Rendering

CO3301

Week 2, Part 1

Page 2: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Entity UpdateEntity Update

• Each entity (type) has a its own update function, which is called periodically– Maybe every frame– Perhaps less frequently in the case of distant, non-visible or less

important entities

• The update function defines the entity behaviour:– Receive and process messages

• Update internal state based on these

– Perform decision making / AI to decide what to do– Send messages to other entities– Perform movement & rotation– Set and play animations, update health, etc.

Page 3: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Scene Update to Entity UpdateScene Update to Entity Update

• Contrast use of entity update with our previous use of models in TL-Engine

• Models are just graphical - contain no behaviour• Model behaviour was typically controlled in a single

global “Scene Update” function– No attempt to encapsulate behaviour– Becomes a bloated, hard to maintain function

• Using entity-based Update is another shift of functionality towards a generalised game engine– However, we will retain “SceneUpdate” to do certain global

update work, e.g. manage user/system input

Page 4: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Pros and ConsPros and Cons

• Using separate Entity Update functions:– Entity behaviour (code) and entity state (data) collected together

within each entity and encapsulated for OO benefits– Easier to maintain in this form, especially for a team– Easier to comprehend behaviour of each entity type, wimpler to

add scripted entity behaviour– However, overall game behaviour is distributed, can get

unexpected interaction– Messaging between entities can be long-winded

• All behaviour in SceneUpdate:– Global function easy to find and work with– But ultimately clumsy and bloated – team unfriendly– Model state tends to become a set of globals

Page 5: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Entity RenderingEntity Rendering

• We also give each entity a render function• This is called every frame

– Provided the entity is a renderable type and visible

• Typically, the entity passes its current position / animation / textures etc. to its entity template– The template class can render any one of its own entities given

these specifics– Recall that the template stores the mesh

• This is fairly similar to model/mesh rendering

Page 6: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Pre / Post RenderingPre / Post Rendering

• Modern engines often perform one or more passes of pre or post-rendering

• E.g. entities might have a PreRender function– Called for all (visible) entities prior to the main rendering calls

• Different purposes for different entities:– A models blends its animations to get the final matrices for

rendering– Cameras calculate their view/projection matrix– Lights perform a shadow rendering pass (dynamic shadow

mapping)– Many entities might do nothing

Page 7: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Games Development 2 Entity Update Example

Targeting: Turning and Movement

CO3301

Week 2, Part 2

Page 8: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

ContentsContents

• Forward Movement to a Target

• Turning– Dot product method– Turning Speed

• Acceleration / Deceleration

• Practicalities

Page 9: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Forward Movement to a TargetForward Movement to a Target

• Have an entity that needs to reach a target point• However, the entity can’t move freely:

– Has a forward velocity - can accelerate / decelerate– Can also turn– In a ground-based situation, can only turn left or right

• Like a car driving towards a target

Page 10: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Forward Movement to a TargetForward Movement to a Target

• Need an entity update function - each update we need to know:– Whether to turn left or right, and by how much– Whether to accelerate or decelerate

• Want to reach the target swiftly– But avoid excessively large turning circles– Worst case – circling around the target

Page 11: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Assumptions and Required InfoAssumptions and Required Info

• To start, assume entity is on a 2D flat surface• Need to know:

– Entity position and target position– Entity local Z & X-axes (facing and rightward directions)

• Local X & Z axes can be found in world matrix– Or if local X unavailable:

In 2D: If Z = (a,b), then X = (b, -a) in a left handed system

In 3D: Use cross products (targeting - discussed elsewhere)

Page 12: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

• First normalise X & Z axes if necessary• Then normalise vector from source to target (T)• Consider angles α and β using dot products:

)cos( :similarly and )cos( :So1 :so here normals are and

)cos( : writtenbecan product Dot

TZTXTXTX

TXTX

Turning – Dot Product MethodTurning – Dot Product Method

Page 13: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

• First consider β, it can tell us which way to turn to face the target:– If β < 90° then target is to the right– If β > 90° the target is to the left

• Now when β < 90° then cos(β) > 0, so:

Turning – Dot Product MethodTurning – Dot Product Method

left turn otherwise right, turn 0 if So)cos( :slidelast From

TXTX

Page 14: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Turning – Dot Product MethodTurning – Dot Product Method

• Now consider α, it indicates how much to turn to face the target

Note: α will always be positive & so won’t indicate the direction (left/right) to rotate, hence the last slide

• cos-1 is the inverse cosine and can be performed in C++ using the acos function

)(cos :so and )cos( :slide previous From

1 TZTZ

Page 15: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Turning OverviewTurning Overview

• Overview of this turning method:– Entity rotated around Y axis to turn towards the target

– Get normalised vectors X, Z and T

– Determine direction to rotate first:

– Then determine angle to rotate to face target:

– But should we rotate by α in the entity update?

left turn otherwise right, turn 0 If TX

)(cos 1 TZ

Page 16: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Turning SpeedTurning Speed

• No, we want to turn gradually to face the target– Don’t want to snap instantly to face it

• Set a maximum turn speed for the entity and limit turning in each update by this value– I.e. turn by min(turn speed, α)– The turn speed can be a setting in the entity template

• Can consider angular acceleration / deceleration– Acceleration to max turn speed when starting to turn– Deceleration to 0 when almost facing target

• Similar to the movement acceleration / deceleration that follows

Page 17: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Acceleration / DecelerationAcceleration / Deceleration

• When to accelerate and decelerate?• Simple (breakneck) approach:

– The stopping distance is the distance covered before stopping if we apply maximum deceleration

– If entity is further from its target than stopping distance then apply maximum acceleration

– Otherwise maximum deceleration

• If the current speed (not velocity) is s and max deceleration d, then: Stopping Distance = s2 / 2d– Since average speed while stopping = s / 2– And time to stop = s / d

Page 18: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

PracticalitiesPracticalities

• Use this method for static or dynamic tracking– When tracking a moving object, consider tracking a point in front

of it (depending on its speed)

• Method needs tweaking:– Won’t target perfectly – so stop when within a certain range – use

the length of the target vector– The stopping distance as given doesn’t quite match algorithm,

should be s2 / 2d – s / 2

• Needs careful insertion into a timed game loop:– Only use update time when applying movement / turning, don’t

alter angle / stopping distance formulae

Page 19: Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1

Further DevelopmentFurther Development

• This is a very basic method, which can easily be refined / altered for more realism:

• Angular acceleration / deceleration

• Decelerate to make a sharp turns (when α is large)– Adapt the acceleration algorithm to consider α

• Or even smarter acceleration / deceleration / maximum speed based on distance and manoeuvrability of target

• Adapt to 3D space – use cross products, see Space Game entity code shortly or Van Verth p39