Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1."— Presentation transcript:

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

2 Entity 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.

3 Scene 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

4 Pros 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

5 Entity 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

6 Pre / 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

7 Games Development 2 Entity Update Example Targeting: Turning and Movement CO3301 Week 2, Part 2

8 Contents Forward Movement to a Target Turning –Dot product method –Turning Speed Acceleration / Deceleration Practicalities

9 Forward 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

10 Forward 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

11 Assumptions 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)

12 First normalise X & Z axes if necessary Then normalise vector from source to target (T) Consider angles α and β using dot products: Turning – Dot Product Method

13 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 β 0, so: Turning – Dot Product Method

14 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

15 Turning 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?

16 Turning 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

17 Acceleration / 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 = s 2 / 2d –Since average speed while stopping = s / 2 –And time to stop = s / d

18 Practicalities 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 s 2 / 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

19 Further 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


Download ppt "Games Development 2 Entity Update & Rendering CO3301 Week 2, Part 1."

Similar presentations


Ads by Google