• No results found

CPSC 427 Video Game Programming

N/A
N/A
Protected

Academic year: 2022

Share "CPSC 427 Video Game Programming"

Copied!
34
0
0

Loading.... (view fulltext now)

Full text

(1)

CPSC 427

Video Game Programming

Entity Component System (ECS) PART 2

(2)

© Alla Sheffer, Helge Rhodin

What will you be doing next week?

2

(3)

Office hours

Monday 10-11 am, ICCSX141, Camilo

Wednesday 9:30 – 10:30, X653 & zoom, Helge

Thursday 2-3 pm, zoom (same as lecture), Tim or Andrew

(4)

© Alla Sheffer, Helge Rhodin

This week

Monday:

• Guest lecture

• more on ECS Wednesday

• Oral pitches

• C++ Tutorial

A0 deadline (Wednesday)

Written pitch deadline (Friday)

4

(5)

Assignments

Template framework

(6)

© Alla Sheffer, Helge Rhodin

A1 – Game Graphics

6

(7)

A2 – Game AI and Collision Processing

(8)

© Alla Sheffer, Helge Rhodin

A3 – Animation and Physics

8

(9)

Your project

(10)

© Alla Sheffer, Helge Rhodin

Register your Team!

Even if incomplete, please register

-> Canvas -> People -> Groups -> Team

10

(11)

Project templates

1. Design (Oral Pitch -> Written Pitch ->

Proposal/Development Plan)

2. Implement (Three milestones)

(12)

© Alla Sheffer, Helge Rhodin

ECS implementations

12

(13)

Memory & ECS

Where do we store our Components?

• RAM, harddrive, or chache?

• Inside Systems?

• Better, but could be improved

• Different Systems may need the same Component types

• How do we decide who owns what?

• Messaging can get overly complex between systems

(14)

© Alla Sheffer, Helge Rhodin

Problem: associating entities and components

14

Mario Luigi

Goomba1 Goomba2

Position Velocity Jumps Player Squishable

Object-oriented-programming (OOP)?

ECS = containers of components?

(15)

Memory & ECS

Where do we store our Components?

• Inside Entities?

position velocity

Update loop has to

access non-contiguous

memory repeatedly!

(16)

© Alla Sheffer, Helge Rhodin

The Map Approach

(entity ID to component address)

16

Mario Luigi

Goomba1 Goomba2

Position Jumps Squishable

Mario Luigi 1

2 ID

1 3 ID

Concept: A (hierarchical) acceleration structure to lookup components Implementation: std:map<Entity,Position>

(17)

Memory & ECS

Where do we store our Components?

• In a map?

position velocity

Update loop has to

access non-contiguous

memory repeatedly!

(18)

© Alla Sheffer, Helge Rhodin

The (giant) Sparse Array

18

Mario Luigi

Goomba1 Goomba2

Position Velocity Jumps Player Squishable

Issues?

1 2 ID

Concept: A huge data matrix of size Nr. Entities x Nr. components Implementation: std:vector<Position>; std:vector<Velocity>

(19)

Memory & ECS

Where do we store our Components?

• Array with holes?

position velocity

Better cache utilization!

Last slide

(20)

© Alla Sheffer, Helge Rhodin

Bitset / Bitmap

20

Mario Luigi

Goomba1 Goomba2

Position Velocity Jumps Player Squishable

Issues?

1 2 3 4

ID Bitset/bitmap

11110 11001

Concept: Each entity has a bitset that is true for its ‘owned’ components Implementation: long bitset; // how many components can we support?

If(bitset & query == query) // has the entity all query components?

(21)

Key & Lock Metaphor

position

Unique Entity ID Entity

velocity sprite health position velocity

delta time calculations

Systems will only operate

(22)

© Alla Sheffer, Helge Rhodin

Further Improvements

22

(23)

Dense Component Vectors (an attempt, needs more)

Mario Luigi

Goomba1 Goomba2

Position Velocity Jumps Player Squishable

Issues?

1 2 ID

How to find the position of

Goomba’s squishable component?

(24)

© Alla Sheffer, Helge Rhodin

Map + Dense Component Vectors

(entity ID to component address index)

24

Mario Luigi

Goomba1 Goomba2

Position index Jumps index Squishable ind.

Mario Luigi 1

2 ID

1 3 ID 1

2

1 2

1 3 Goomba1

Goomba2

Issues?

Concept: Combine dense vectors with a map

Implementation: std::vector<Component>; std::map<Entity,unsigned int>

(25)

Map + Dense Vector (different visualization)

Mario Luigi

Goomba1 Goomba2

Position Velocity Jumps Player Squishable

1 2 ID

2 3

(26)

© Alla Sheffer, Helge Rhodin

Cache is Key

Each Component type has a statically allocated array

Minimizes costly cache misses

Keeps components we access around the same time close to each other

position

collision sprite velocity

Memory Blocks

26

(27)

Map + Component Vector + Entity Vector

1 5 13

0.5 map

keys (entity ID)

value array (components) 0.3 0.3 10 12

0 -0.1

Registry for one component

Concept: Add a dense vector of entities to facilitate quick iteration over entities

Implementation: std::vector<Entities>; std::vector<Component>; std::map<Entity,unsigned int>

(28)

© Alla Sheffer, Helge Rhodin

Faster iteration via entity and component array

28

for(int entity : velocity_entities) // efficient

if (position_entity_map.has(entity)) // inefficient lookup

position_entity_map.get(entity)+= velocity_entity_map.get(entity); // 2x inefficient lookup

for(int vel_i = 0; vel_i < velocity_entities.size(); vel_i++) // efficient Entity entity : velocity_entities[vel_i]; // efficient

int pos_i = position_entity_map.getIndex(entity); // inefficient lookup if (pos_i)

position_components[pos_i]+= reg_velocity_components[vel_i]; // efficient

Accessing the velocity map (reg_velocity.map) is an unnecessary indirection

We can access the velocity components in linear fashion

(29)

Map + Component Vectors + Entity Vector Cache is Key

position

collision sprite velocity

Memory Blocks

Update loop

accesses contiguous memory IDEAL!

position entity IDs velocity entity IDs

(30)

© Alla Sheffer, Helge Rhodin

Advanced ECS: Archetypes / prototypes / pools

30

• Concept: store all types with the same components in dense arrays

• Used by the Unity ECS system

• Difficult to implement

Mario Luigi Goomba1

Goomba2

Position Velocity Jumps Player

Squishable Position Velocity Jumps

Position Velocity

(31)

How Does a System Find its Entities?

Extension: Entity Manager

Each system has a list of entity IDs it is interested in

Systems register their bitsets/bitmaps with the Entity Manager

Whenever an Entity is added…

Evaluate which systems are interested & update their ID lists

(32)

© Alla Sheffer, Helge Rhodin

Self-study: A special map approach

32

Mario Luigi

Goomba1 Goomba2

Position Velocity Jumps Player Squishable

1 2 ID

2 3

(33)

Self-study: The ‘Sparse Set’

Mario Luigi

Goomba1 Goomba2

Index Pos Index Vel Index Jump Index Player

1

Index Squish

Issues?

1 2 ID

2

2 1 1

Position Velocity Jumps Player Squishable

3 4

(34)

© Alla Sheffer, Helge Rhodin

Self-study: Faster Lookup with Sparse Sets

34

Insert:

Lookup:

1 2 3 4 5 6 7 0

[https://skypjack.github.io/2020-08-02-ecs-baf-part-9/]

The map lookup (map.get(entity)) is costly

• A hashmap is O(1), but that 1 is big Sparse set:

• An array as large as the number of entities in the game

• Crazy waste of memory?!

• 32 bit integer -> ???

• a sparsely filled array

• A small dense array of all entities in sequence (as before)

• Extremely fast lookup, insert, & clear

References

Related documents

- Regards the domain as a series of fixed-sized blocks; we represent sparse blocks using the UINT layout and dense blocks using the BITSET layout. - Selecting layouts on a set

The percentage of Y-IDUs that said they were referred to YFHS by an outreach worker decreased slightly in Tashkent over time (from 50% to 45%), but remained consistent and

In order to begin the process of connecting your Sharp Smart Countertop Microwave Oven to the internet please make sure you’ve downloaded the Alexa App onto your mobile device,

In 111 instances, the initiators of the moves were not identified, but the case files cited child behavior problems as the reason for the placement changes.. The foster

a chronic condition or pre- palliative state Observations may be delayed due to night-time ward management reasons Level of compliance with scheduled observation is used as

Objective: Clinical Pharmacy Services (CPS) are considered standard of care and is endorsed by the Joint Commission International, the American Academy of Pediatrics, and the

\bitsetSetDec and \bitsetGetDec transpar- ently switch to package bigintcalc if the numbers get too large for TEX’s number limit.. Expandibility: Any operation that does not change