Summary

This document is a guide to game design basics. It details concepts like player motivation and mechanics, how to consider the player experience, and how to improve the core loop of a game. The guide also discusses important communication among the development team. The author is an expert in the field of game design.

Full Transcript

01 Game Design Basics Tvorba a dizajn počítačových hier (FMFI) Návrh a vývoj počítačových hier (FIIT) Michal Ferko 19. 9. 2024 What is Game Design? Game Design Determines what will be and what won’t be in the game Dictates the overall player expe...

01 Game Design Basics Tvorba a dizajn počítačových hier (FMFI) Návrh a vývoj počítačových hier (FIIT) Michal Ferko 19. 9. 2024 What is Game Design? Game Design Determines what will be and what won’t be in the game Dictates the overall player experience Ensures engagement for the player Always works with difficult constraints A Game Designer Communicates the vision of the project to the whole team Explains every single detail and gets everyone on the same page Creates game mechanics (systems) that form the game Creates the story, setting, narrative… Ensures balance A Game Designer is the player’s advocate Player-centric approach What can I give the player? What does the player want? What type of players are they? How will this affect the player? Will this have the effect on the player that I want? What value do I give the player? Create experiences, not mechanics What do we want the player to experience? How do we want them to experience it? Every decision should work toward improving the experience Player Motivation People play because of different reasons You should think about what motivates the player To start playing To keep playing To return the next day It changes over time and as you play Exercise: Pick a game you played recently Why did you start playing? When you decided to play again, what was your motivation? Why did you stop playing? by Quantic Foundry Why? Not just hearing Listening! Deliberate Game Design Everything must have a purpose How it is fulfilled changes with every change to the game Existing purposes need to be re-evaluated Remember purposes If you can’t convince the team why it should be in the game, it should NOT be in the game “But I want it there” OK for gamedev as a hobby and not part of a team Will most likely clash with what some players want Game Design is Communication With the player Always think how you will present something to the player, how it will be understood Practice introspection – observing yourself while playing games With the team Keep a Game Design Document (GDD) - provides clarity, reminds of purpose Get on the same page Two teams with the same GDD should end up with 2 very similar games With the customer Think how you will market the game With the game itself A game is defined more by what you don’t put in than by what you put in Game Design is iterative Always playtest Any meaningful gameplay/change you do, you should see how someone plays it You are an expert in your game, you know everything The player usually has no idea Playtests are the only way to validate if players understand and can play your game The more iterations you manage to do (Build-Measure-Learn), the better your game will be Minimalism Every idea creates a ton of work for everyone on the team The designer is responsible for keeping it as minimal as possible Simpler features ⇒ faster iteration time ⇒ better game & happier team You must understand the ramifications of your decisions Technical difficulty, art difficulty Production speed, deadlines, business goals If you mess up scope and are not willing to cut, you have already failed Kill Ideas Quickly Every Game Developer is a Game Designer You will copy a lot! A ton of research was done into how good games are designed If you’re doing something completely new It’s not tested You don’t know exactly what effect it will have You need to test it Testing takes time Even if you think it’s new, someone might have tried it before But you don’t know it And they know why they did not use it Innovate more in fewer places Steal Like an Artist It’s Analysis & Research You need to analyze & research other games as well What makes them work? What are the problems in a game’s design? What is the purpose of individual mechanics? How will you replicate a game’s experience? How will you replicate a non-game experience? Introspection – observe yourself as you play Players should get into flow Flow also known as being “in the zone” Fully immersed in a feeling of energized focus, full involvement, and enjoyment int the process of the activity Experience of time is altered, other needs become negligible… Flow happens when you perform activities you are skilled in But face some form of challenges Not specific to games, happens with sports, work, driving, hobbies… https://www.jenovachen.com/flowingames/Flow_in_games_final.pdf https://www.amazon.com/Flow-Psychology-Experience-Perennial- Classics/dp/0061339202 Flow Attributes Flow can happen only if the activity has the following Clear Goals you are trying to achieve something, and you know what it is Balance between Challenge vs. Ability The challenges you face are proportional to your abilities Immediate Feedback When you do something, you immediately know if it’s good or bad Feeling of Control You feel that you can control the situation, it doesn’t “just happen” Concentration on the Task at Hand You fully concentrate on the task, there are no distractions Sense of Self Disappears Your consciousness becomes the thing you do Flow conditions Knowing what to do Knowing how to do it Knowing how well you are doing Knowing where to go (if navigation is involved) High perceived challenges High perceived skills Freedom from distractions http://web.cs.wpi.edu/~gogo/courses/imgd5100/papers/FlowQuestionnaire.pdf Game Mechanics Rules of a game Not printed instructions that the player is aware of Rules are not known beforehand The game teaches the player as he progresses Board and card games often require that the player knows the rules, so they don’t do invalid actions But digital games have a complex system that enforces only valid actions Careful! Enforcing only valid actions and teaching the player what is happening are very different things! If something happens without proper feedback, players might get confused Games are problem solving activities Mechanics usually creates challenges There are some exceptions Mechanics provide tools to overcome challenges – solve problems Different challenges Physical Mental Problems & Tools “A game is a series of interesting choices” Problems & Tools Give the player problems to solve (challenges) Non-trivial problems that need to be solved Optional problems that the player can ignore (but might get a reward) Repetitive – defined by the game genre (same structure, different variables) Problems can have sub-problems Give the player tools to solve these problems If a problem has a single solution, it can become repetitive and tedious (grind) Ever played a rhythm game? Offer different tools for different problems – overlap is OK, sometimes even desired Having multiple solutions results in interesting choices Problems & Tools - Example There’s 20 enemies in a room trying to kill you Problems I must survive (enemies) I must kill them to unlock the door (doors) I must find something to kill them with Tools Move to avoid enemies and their shots Find and pick up weapons Use weapons to kill enemies Collect medkits/loot from dead enemies Variability of enemies/levels/weapons/pickups ⇒ consistent but varying challenges Risk & Reward Actions you take often have a certain risk Running into a group of enemies, moving a chess piece, destructive move in a puzzle game… Actions you take should result in some reward Loot, damage to enemies, capturing a strategic location… Players will always intuitively weigh the risk/reward of actions Nobody will perform a high risk/low reward action Risk & reward are highly context-dependent Rule: risk should be proportional to reward low risk ⇒ low reward, higher action cost ⇒ higher risk Exercise: pick a genre and let’s analyze Game Mechanics Classification Mechanics are more concrete than rules, include everything that affects the actual gameplay Example: Monopoly mechanics: Written rules + prices and rent of all properties, as well as the text of all Chance and Community Chest cards Core mechanics (core gameplay) –activities that players constantly do Mechanics the player interacts with every second Most influential, affecting many aspects of the game Meta mechanics (meta game) Mechanics wrapped around core mechanics, tying them together Progression mechanics Mechanics that the player interacts with less often (e.g. every 10 minutes) Core + Meta mechanics Skyrim Core mechanics: Movement, Combat Meta mechanics: Storylines, leveling, achievements, role-playing… Core: “fire your bow 1000 times at different enemies” Core+Meta: “become a master stealth archer to save the kingdom” Meta mechanics can also give the player some rest Racing game core: driving laps Racing game meta: upgrading/repairing your car, buying new parts, unlocking new tracks… 5 types of Mechanics Physics – how the game world behaves A core mechanic in many games: FPS/Racing/Sport games, Portal, Angry Birds… Internal economy Mechanics of transactions involving game elements (resources) Resources are consumed, collected, traded… Example: what are all the resources in Counter-Strike? Progression mechanisms Describes how the player progresses through the game world Getting to a particular place, obtaining an item, unlocking a door, reaching a level... Tactical maneuvering Placement of game units on a map, gaining advantage that way (e.g. Chess) Social interaction Teams, clans, giving gifts, trading items, creating alliances,... Image from Game Mechanics: Advanced Game Design Image from Game Mechanics: Advanced Game Design Games are about doing the same thing over and over again Core (Game) Loop Clash Royale A series of actions that is repeated over and over as the primary flow of your experience Defines what the player will be going through Careful! Core gameplay ≠ Core Game Loop Overwatch Core vs. Meta Game Design step-by-step guide 1. Start with an idea (mechanic, visuals, setting…), ideally unique 2. Clear up game mechanics that fit the idea (fewer is better) Think about tools & problems, flow, motivations, mechanic types… 3. Think about what the player will be doing (core loop) Every second (core), minute (core), 15min (meta), play session (meta) 4. Make a prototype 5. Give to people to play and watch them 6. Learn what works and what doesn’t by observing players 7. Come up with new ideas to change the game 8. Go back to step 1, 3 or 5 and repeat Game Design knowledge + skills Creativity Extremely clear communication Make programmers & artists understand the same vision Soft skills Introspection Can take criticism Wide array of played games Related fields Product design, Psychology, Ergonomics, Logic, Mathematics, Programming, Art, History, Writing, Sound & Music… Game Design Specializations System Design Determining what the challenges & mechanics will be +Balancing Level/World Design Laying out individual challenges Economy Design Designing economies that make sense Monetization Design Designing what/when/how to monetize Narrative Design Determining the story, lore, characters… Close to writing 02 Game Documentation Tvorba a dizajn počítačových hier (FMFI) Návrh a vývoj počítačových hier (FIIT) Michal Ferko 26. 9. 2024 Documents related to Game Design Concept Document Game Proposal Document Game Design Document (GDD) Technical Design Doc Art Style Guide / Art Design Doc Project Plan & Schedule Test Plan Image from The Art of Game Design: A book of lenses Pitch Deck (or Concept Doc) Convey the goal and purpose of the game For management level to help “sell” the game Assess viability, budget, scope & timeline Sell to management/investors/publishers/co-founders Usually a presentation, with ~10 slides Concept doc can be 1-2 pages, max. up to 5 Written by the producer/creative director No well-defined form List key things you want to communicate Sort by importance Most important first (since nobody might read the whole thing) 3 Pitch Deck contents 1. Premise – high level concept, describe your game and how it is unique in a few sentences 2. Player motivation – what will motivate the players? How do they win? 3. Hook/Unique Selling Points – Why will players pick YOUR game? What makes it different from the rest 4. Target Market – Age, Gender, Motivators, Platform, Genre, HW requirements… Target rating (mature content, violence, …) 5. Competitor & market analysis 6. Budget, scope, timeline, revenue projections 7. License Pitch Decks Loot River: Prototypovanie a príprava pitch decku | Miro Straka, Straka Studio https://www.youtube.com/watch?v=oJ8BJI3uxgQ The 10-20-30 rule of PowerPoint https://guykawasaki.com/the_102030_rule/ Pitch Deck examples https://www.notion.so/Pitch-Decks-f56e38c13fe6417f8379859e74367e1a How to pitch your game to a publisher or anyone https://www.tinybuild.com/how-to-pitch-your-game Game Design Document (GDD) Similar to a software requirements specification Complete description of the software All functional requirements Detailed description of features Should answer all questions about the game The GDD must contain Complete gameplay description – mechanics, interactions Description of all game elements (creatures, NPCs, items, classes, story…) Can be extremely long – 50 – 200 pages It’s a living document Updated throughout development When a design change occurs, the designer must update the GDD GDD The GDD is used mainly by the production team Artists when creating art Programmers when programming features Designers when creating new mechanics or improving existing ones Should be the “ground truth” for the team When something is unclear, the GDD should have the answer If it does not, the designer should update it The form is relatively loose Certain sections are irrelevant for certain games There is no magic template Purposes: memory and communication You make lots of decisions and you might forget them GDD forms Google Docs/Notion Pros: Easily shareable, can link to any section, quick to prepare, all in one place Cons: Struggles with larger documents (50+ pages with images), more linear reading My experience: people use a ton of separate Google Docs and link between them Wiki system Pros: More structured, can link very well, quickly editable, good revision history Cons: Does not have everything in one place, slower setup, might be harder to edit/maintain Miro (single visual board) Pros: Everything in one place, more visual, less structured, able to integrate detailed documentation into diagrams Cons: If you do not maintain structure, it can be very confusing and hard to navigate GDD Structure Core gameplay description Core game loop Character, Controls, Camera Player tools Challenges (problems to solve) Game World All static & dynamic objects and their behavior Pickups, weapons, enemies, enemy AI… List of levels/locations User Interface All screens, UIs, HUDs Story, Lore, NPCs… Core game loop 2D Camera Isometric Side-scroller Top-down 3D Camera 1st person 3rd person Top-down Controls PC or Console or Touchscreen or VR or Gyro or GPS or … What is controllable & how Characters Single character (avatar) – one person Multi character – party Lots of units – commander Characters Specific or customizable? Visuals Stats UIs 1 Picture > 1000 words Art Design Document (ADD) Describe art that will be used throughout the game Art style, mood boards Color palettes 2D sprites, 3D models Animation Techniques, software, workflows, limitations Visuals & audio 18 Technical Design Document (TDD) Describes the engine/technical solutions on which the game will run Comparison to other engines on the market – why did we pick our engine Technology production path How they will get from concept to software Specific requirements and features, HW & SW Refers to the GDD/ADD Closer to the software requirements specification Farther from the concept document 19 Production Plan Required team & allocation Tasks & time estimates Gantt chart When will we ship? Marketing strategy Post-launch plan DLCs, LiveOps... Image from https://commons.wikimedia.org/wiki/File:Gantt_Chart_Template_for_Film_Production.png 20 Game Development Stages Image from https://digitalworlds.wordpress.com/2008/04/10/the-process-of-game-creation-the-game-design-docu ment/ 21 Development phases Concept - Start with a small team, work on a concept document Prototype – not only gameplay, also art “A working piece of software that captures onscreen the essence of what makes your game special, what sets it apart from the rest, and what will make it successful.” Pre-production – Create GDD, TDD, ADD, mechanics Production – create all content Alpha, Beta, Gold Post-production 22 Problem & Solutions Waterfall is not cool Moving towards Agile/Scrum Very cool Game Development is inherently iterative Need to test various systems Do players like XYZ? Are the graphics good? Is the music suitable? Are the controls good? Build-Measure-Learn Read “Agile Game Development with Scrum” 23 03 Introduction to Unity Tvorba a dizajn počítačových hier (FMFI) Návrh a vývoj počítačových hier (FIIT) Michal Ferko 3. 10. 2024 Game Engines Unity – ~70% of all mobile games, ~50% of all Steam games “50% of games across devices are made with Unity” – Unity, 2022 Unreal – ~15% of all Steam games, much higher percentage on consoles Godot – the biggest/most famous open-source engine Lots of other engines or frameworks GameMaker, XNA, CryEngine, Construct, RPG Maker, Frostbite, Source, id Tech… https://www.gamedeveloper.com/business/game-engines-on-steam-the-definitive-breakdown https://steamdb.info/tech/ Unity Unreal Engine Godot Game Maker Among Us Fortnite Dome Keeper Undertale Genshin Impact Sea of Thieves Brotato Hotline Miami Twelve Minutes PUBG Halls of Torment Hyper Light Drifter Valheim Deep Rock Galactic Cassette Beasts Spelunky Hearthstone Black Myth: Wukong … Loop Hero Cuphead Palworld Pizza Tower Ori 1 & 2 Manor Lords Nuclear Throne Monument Valley 1 & 2 Stray Swords of Ditto Cities: Skylines It Takes Two Katana Zero Inside Star Wars Jedi: Fallen Order Downwell Hollow Knight Tekken 7 Forager Pokemon Go The Outer Worlds … Superhot BioShock Gwent Kingdom Hearts III Beat Saber A Way Out Overcooked Mortal Kombat X Untitled Goose Game Borderlands Fall Guys … … Unity Free Cannot earn/raise more than $100k/year ($200k/year with Unity 6) Unity Splash Screen FORCED (OPTIONAL with Unity 6) Pro €1.877/seat/year (~€2.040/seat/year from 2025) Access to Pro Analytics, Team tools… Needed to deploy to consoles – Xbox/PS/Switch (platform devkit still required) Asset store: full of interesting models/scripts/tools Source code access: EXPENSIVE (Pro/Enterprise license) Scripting in C# (or visual scripting) https://unity.com/products/compare-plans Unity platforms Editor: Windows/Mac/Linux Release platforms: Unreal Free to use 5% royalties to Epic Games For every dollar you earn above $1M Can reduce this if you release on Epic Games Store Source code access: YES (for most parts of the engine) Not open source, only source available Marketplace Scripting: C++, Blueprints (visual scripting) Has bindings for other languages Unreal platforms Godot Free forever and open source Community-driven and community-built Gained a lot of popularity in the last year (especially thanks to Unity’s PR disaster) Not that feature-complete as Unity or Unreal Can buy assets on a few sites, but much smaller support than Unity Also much smaller (but more active) community Scripting GDScript – similar to python C/C++ – official support C# –Godot 4.2+ should have all platform support Godot 4.3 Platforms Windows, Mac, Linux HTML5 Android, iOS Consoles Cannot be part of Godot directly There is a studio porting to consoles made by Godot authors Picking an engine License/pricing – very important Features & other support anything except Unity on mobile will be complicated Some engines are more suited for some games Unreal – shooters, aspirations to do AA/AAA development It is said to have worse performance on mobile – but I cannot verify this Unity – mobile, indie PC/Console Why Unity I have 10+ years experience, can provide better support Unity rolled back crazy pricing changes Easier to learn than Unreal Editor has better performance and lower requirements than Unreal More feature-complete compared to Godot De-facto standard for indie/mobile development Let’s hope they don’t change their terms again Unity Recommendations Use Unity Hub to manage Unity installations Start new projects with the latest LTS version (2022.3 now) Don’t just automatically update Unity The only reasons to update a project: New bugfixes you need New features you need Turn off if you see this window Interface overview Scene View Hierarchy Game View Project Panel Inspector Toolbar Basic Concepts Game Objects Components Prefabs Importing assets Project Preferences Other project settings Build Scripting Adding, naming, renaming, removing C# scripts Visual Studio GetComponent() -> LOW PERFORMANCE! Input.GetKey() Renderer component -> material color Private and public variables, linking with editor Assigning prefabs/existing game objects to variables Awake() and Start() Update() and FixedUpdate() Scripting (2) Vector3 class and utility functions -> magnitude, Dot, Cross, … Component.enabled and checkboxes in editor Light component -> enabled Adding mesh filters, renderers and materials Example with light GameObject.SetActive() and checkboxes in editor GameObject.activeSelf, GameObject.activeInHierarchy Visual Studio debugging Transform component -> Translate, Scale, Rotate Why not use with colliders And only kinematic rigidbodies Scripting (3) Vector3.forward, Vector3.right, Vector3.up Time.deltaTime in Update() Running bot example Vector3.Lerp Moving light example with changing color GameObject.Destroy(GameObject[, time]) GameObject.Destroy(Component[, time]) Input.GetButton… Access scripts of other Game Objects Instantiate() Value vs. Reference types 05 Game and Engine Architecture Tvorba a dizajn počítačových hier Návrh a vývoj počítačových hier Michal Ferko 17. 10. 2024 Game development is software development Use of well-defined design patterns Relatively strict structure of a game and game engine Not only graphics and physics Although lots of concepts are borrowed/required from these fields 2 Game architecture All the parts a game (+engine) consists of How all the parts fit together Relying on established standards will make your life easier You will create maintainable and reusable software pieces Avoiding standards might pay off in terms of efficiency Engine custom-made for a specific game – e.g. Minecraft Not a good idea for most games 3 CHANGE The only constant in software development Game architecture layers Architecture with lots of layers (think TCP/IP) Every subsystem can be put into one of these categories: Application layer Deals with the hardware and the operating system This is usually handled by the game engine Game logic layer Manages your game state and how it changes over time Game view layer Presents the game state with graphics and sound Similar to the Model-View-Controller (MVC) design pattern Changes in hardware/OS should not affect the game logic or game view layers Just like MVC 5 Game logic layer This is your game – all its mechanics Without input systems, rendering & audio playback… Contains subsystems that manage the game world state Communicating state changes to other systems Examples: Playing a sound when you fire a gun Playing an animation for the gun as it fires Updating health of enemy hit by the shot Contains systems that enforce rules of your game world Physics system … 6 Game view layer Presents the game state to the player Translates input into game commands Not only drawing the game state on the screen Other views include: AI agents get a “view” of the game state A remote player gets a view of the game state The state observed from game logic is the same But they do different things 7 An example – racing game Game logic Holds the data that describes cars and tracks Car weight distribution, engine performance, tire performance, fuel efficiency, … Track shape, surface properties, physics Input – just like a real driver Steering, acceleration, braking Output – state changes and events Car and wheel positions and orientations, damage stats, how much ammo is left Events: car collisions, passing checkpoints, … 8 Racing Game – Human Game View Draw the scene, spawn particles, play audio, force feedback… Read the input devices “Accelerator at 100%” “Steer left” Sends these commands back to the game logic What happens when you press SPACE (emergency brake) 1. The view sends a message to game logic 2. Game logic sets the emergencyBrakeOn to true 3. Game logic notifies the view that state changed 4. The view responds by playing a sound and spawning a dust particle effect on tires 9 Racing Game – AI Game View Receives the same game state and events as the human view Track, weather, car positions and velocity Can react in response to events (such as “Go!”) by sending info to the game logic Set accelerator to 100% Steer left at 50% Commands remain the same! 10 Game views Flexible – any number of human/AI/remote views E.g. multiple human views on one device – split-screen multiplayer A game view that just records game events into a buffer You might replay them later The game logic is kind of disabled during replay, since a view is sending all the events With a little extra work, you can get a rewind feature working 11 Game views (2) Views can give advantages or disadvantages 4:3 aspect ratio might give smaller field of view than 16:9 aspect ratio AI might know more about game state than the player (see through walls) Game views are difficult to get used to Unity does not offer a strict separation of views from logic implicitly Using it explicitly will make more maintainable code Even if you do not strictly separate, it’s good practice You should know which layers are communicating 12 13 Application layer 14 Reading input Layer between the OS and the rest of the game application layer The state is translated into game commands Should be configurable Game state should never change directly from reading user input Not flexible Lots of changes when controls change (keyboard vs. gamepad, mouse vs. touchscreen) 15 Reading input in Unity Old Input system Direct ⇒ Input.GetKey() Indirect ⇒ Input.GetAxis() / Input.GetButton() can configure in the editor or at runtime New Unity Input System is indirect Can configure named actions Much more structured Great for multiple sets of controls 16 File System and Resource Caching Reading and writing from disk and other storage media Managing resource files can be complicated One of the hidden systems is the resource cache Commonly used assets are always in memory Rarely used assets are in memory only when needed (end-game video) The resource cache tries to “fool” the game into thinking that all the assets are available in memory If all goes well, the cache can load files before they are needed Cache misses might occur if it fails to load something in time Solve by loading screens or small lags 17 Resource Loading in Unity SceneManager.LoadScene() SceneManager.LoadSceneAsync() Resources.Load/Addressables allows more fine-grained memory control Can have a “weak reference” and load manually with Addressables Can load by name from a folder with Resources.Load or Addressables If you need this, use Addressables Avoid Resources.Load as much as you can 18 Resource Loading in Unity Instantiate(prefab) is NOT loading anything Everything that is referenced must already be in memory When a scene loads: 1. Read scene and load all assets that it references – prefabs, textures, models… 2. For each of those assets, load all assets that they reference (unless already loaded) These references are also used when building the game Takes a list of all scenes and gathers all references recursively Adds Addressables or Resources folder Unreferenced assets are NOT packaged into the build 19 Initialization, Main Loop, and Shutdown Games are simulations that have a life of their own Player input is not required for the game to continue simulation The system controlling the game simulation is the main loop or game loop Usually has three stages 1. Grab user input 2. Update game logic 3. Present the updated game state through all views Rendering, playing sounds, sending state over the internet 20 Simple game loop 21 Game loop in Unity Game loop handled implicitly Querying user input is handled by the engine Game programmers are given two main callbacks Update() Updates as often as possible Called once for each frame rendered (lower FPS => less updates) FixedUpdate() Updates in fixed intervals (by default every 20ms => 50 FixedUpdates per second) Called once before each physics engine step Rendering is done by the engine implicitly – we only set what to render Exception: Using low-level rendering access with the GL or Graphics class 22 How Unity game loop looks (simplified) float timer = 0; while (true) { while (timer > fixedTimeStep) { FixedUpdate(); PhysicsUpdate(); timer -= fixedTimeStep; } Update(); LateUpdate(); Render(); timer += deltaTime; } 23 Unity’s game loop simplified 24 Unity Update & FixedUpdate 25 Unity Update & FixedUpdate 26 Unity Execution Order You need to know what happens and in which order! This is the most important graph for understanding Unity http://docs.unity3d.com/Manual/ExecutionOrder.html 27 From https://docs.unity3d.com/Manual/ TimeFrameManagement.html 28 Other Application Layer Code System clock String handling System libraries Threads and thread synchronization Network communication Initialization Shutdown (Scripting language) 29 Game logic 30 Game logic Defines the game universe What things (entities) are there How they interact Defines how game state can be changed by external stimulus 31 Game state and data structures A game needs to store its game objects in a container Must be able to traverse quickly to change game state Should be flexible as to what data it will hold Special game data (hitpoints, inventory, …) are stored in some custom data structure In Unity: Upgraded scene graph hierarchy Retrieve objects either by pointing directly to them, or special functions Static functions part of GameObject: GameObject.Find(), GameObject.FindWithTag(), GameObject.FindObjectsWithTag() Implicitly traverses a tree or a hash table to efficiently find the objects Special game data stored as serialized variables of script components 32 Scene Graph Hierarchy in Unity Hierarchy stored in the Transform component of the game object Each Game Object can have a parent Transform.parent Each Game Object can have any number of children Transform.GetChild(i), Transform.childCount Good practices with the graph hierarchy Never rely on the parent A parent should be concerned about its children, not the other way around Use GetChild() only to iterate through all children Never hard-code GetChild(3), use references to objects instead 33 Game state and data structures Easy to confuse game logic representation with the visual representation of objects Amount of damage a weapon deals is stored in game logic The weapon’s model, textures, icons are only relevant to the game view Another example: Skeletal mesh object that is used for skinning the character when rendering It seems that it has something to do with the character’s weight Skeletal mesh – view, weight – logic (probably for physics calculations) 34 Entity-Component-System (ECS) Design pattern used to store and manipulate game state Favors composition over inheritance Entities consist of Components Each component has a single responsibility Systems run in background and handle simulation One system serves only one purpose Physics System, Player Damage System Components register in one or more systems Entities are affected indirectly (through their components) 35 ECS in current Unity It’s not really ECS, but similar Systems are not in one place Entity = GameObject – name, tag, active/inactive, layer… Component = Component (MonoBehaviour) System Configurable but not directly visible Can create own systems Individual components register with their respective systems Split across multiple components 36 Example of systems Collider and Rigidbody register with the Physics System MeshRenderer, Camera, Light register with the Rendering System Scripts register with the Scripting System and Event System You create other systems through MonoBehaviours – Damage System, Items & Inventory… Health, Weapon and Explosive are all part of the damage system 37 True ECS in Unity Unity offers true ECS – it’s called DOTS (data-oriented technology stack) Game Objects and Components are different You don’t create MonoBehaviours, but create Systems instead Has been fully released a few months ago More complex and more difficult to work with Can provide performance improvements if done right 38 Physics and Collision Rules of your physical game universe No need for over-complicated physics to make a game fun If our game is completely abstract, players tolerate unrealistic physics If the game simulates the real world, small errors might cause players to be very disturbed Usually, rigid body dynamics with dynamic objects being mostly convex Few exceptions: Ragdoll physics Fluid simulation … 39 Game Events When the game state changes, a lot of other systems need to react accordingly Game logic is responsible for generating these events and passing them further Subsystems register with the Event Manager to listen to events that they react to Clean and efficient separation of unrelated code with a simple abstraction and the use of a unified event interface The Observer pattern 40 Process Manager The game logic is composed of small parts of code that need to be executed periodically for the game to work correctly Multiple processes that are completely independent (but we might provide dependencies) Mostly script executions (if there’s a scripting language) Otherwise, it’s just one class per operation to keep things simple Chaining processes together (throwing a grenade, explode on collision) Unity handles this by calling the Update() (or other event) functions on all script components attached to currently enabled game objects in the scene You may sometimes come up with code that needs a certain order of execution, AVOID IT as much as you can 41 Command Interpreter Provides a good separation of the game view and game logic Need to interpret commands sent by AI or human players Unified interface, no mixing of unrelated code Provides more efficient debugging You can send separate commands while debugging This is usually what consoles are for Not the hardware, the Counter-Strike console The Elder Scrolls also had a console As well as many others 42 Game view - Human Views are a collection of systems that communicate with game logic to present the game to an observer Observers can be human, AI,… The view responds to game events as well as controller input Works as a translator component that outputs game commands on one side and the presentation of the game on the other side 43 Graphics Display Renders the objects in the scene Renders the user interface (HUD) Must draw the scene as fast as possible Lots of problems Which objects to draw How to handle complex transforms How to handle complex visual effects Handles animation interpolation Lighting conditions Post-processing Level-of-detail … 44 Rendering 3D Meshes Cameras Lights Post-processing 45 Graphics Display (2) For very complex 3D scenes, need a lot of pre-computation Light maps Potentially visible sets (PVS) Light Probes The artist must be aware of the game engine capabilities Lots of constraints Unity tries to handle all of this You might reduce performance of the game without even knowing it 46 Audio Playing sounds Three main areas Sound effects – simple, when an event occurs, play some audio Music – a little harder when done right Tone of music adjusting according to game situation (Elder Scrolls, Halo, …) Speech – very tricky Lip-sync and storage of lots of sounds are the main problems Take into account 3D audio 3D positions of listener and sources 47 Audio in Unity AudioSource and AudioListener components AudioMixer, groups, filters, effects Can use other audio engine – FMOD, Wwise… 48 User Interface Presentation Every UI must be very specific and adjusted to the game Re-using components is possible, but only to a certain extent Sometimes you need a completely new GUI component to fit the game A compass, special inventory, … 49 Unity UI Uses separate rendering from 2D and 3D All components rendered on a Canvas Objects use 2D sprites for rendering Has numerous tools for anchoring, scaling for different resolutions, events… Can do screen-space, camera-space and world-space UI Can mix UI rendering and standard rendering Alternative: UI Toolkit https://docs.unity3d.com/Manual/UI-system-compare.html 50 Options Configuring the view Resolution, aspect ratio Controls Sound effects Graphics quality and performance In Unity: see Project Settings 51 06 Game Physics Tvorba a dizajn počítačových hier (FMFI) Návrh a vývoj počítačových hier (FIIT) Michal Ferko 24. 10. 2024 Realistic modeling of the world We are mimicking the real world with realistic rendering, environments and animations Pre-defined physics animations (exported from a 3D modeling software such as 3ds max) can add physics into our world Usually we need to dynamically create these animations We need to react to user input The simplest game physics model: Pong Image from https://en.wikipedia.org/wiki/Pong Pong physics model We have a ball moving at constant speed We render one frame every 20ms -> 50 FPS Whenever the ball hits a paddle, we compute new velocity That is, direction and speed Paddles are moved only by user input, not affected by the ball 3D points: 𝑃, 𝑄, 𝑅 … ∈ 𝔼3 3D vectors: 𝒖, 𝒗, 𝒘 … ∈ ℝ3 Pong physics model (2) 𝑛 = current frame, 𝑛 + 1 = next frame Update the position of the ball every frame: 𝑃𝑛+1 = 𝑃𝑛 + 𝒗 ∗ ∆𝑡 Bounce off the top and bottom of the screen Detect if a collision occurred between frame 𝑛 and frame 𝑛 + 1 Is the line 𝑃𝑛 𝑃𝑛+1 intersecting the paddle line? On collision, change the ball’s velocity Compute the exact point at which the ball will bounce off Change velocity by reflecting the velocity vector End game if the ball intersects the left or right of the screen Complex physics models To perfectly simulate the real world, we would need lots of different physical mechanics Rigid body dynamics Soft bodies Fluids Vehicle physics Ragdoll physics, Cloth simulation ⇒ Destructible environments … Complex physics models - Reality Most of these mechanics are very complex and have little usage in games Most games with physics use rigid body dynamics Ragdoll physics ⇒ for death animations of characters Soft bodies are used rarely, too complex or not well plugged into a game Added value for the player is usually negligible Cloth simulation ⇒ mostly for rendering Fluid dynamics is usually overkill, animating oceans or water is done using simpler tricks Rigid body dynamics We will focus only on this part of realistic physics Our objects will be rigid bodies – non-deformable solid objects We want to compute the dynamics of rigid bodies The movement and rotation of rigid bodies Based on Newton’s laws of motion (a.k.a. dynamics) Objects can have constraints Static objects – not movable by any force Kinematic objects – moved by the users or animation Applies force to other objects, any force applied to them is ignored Joints, connected components (car wheel)… Linear Dynamics Movement of the object through the world is a position function 𝑋 𝑡 We do not know the values of 𝑿 𝒕 until all player input up until time 𝒕 is known Constant velocity: 𝑋(𝑡) = 𝑋0 + 𝑡 ∗ 𝒗0 𝜕𝑋 Derivative of the position function is velocity: 𝑋 𝑡 = 𝒗(𝑡) 𝜕𝑡 2nd order derivative: acceleration When the acceleration is a zero vector, the velocity is a constant vector Otherwise, velocity is a function of time (const. accel.): 𝒗 𝑡 = 𝒗0 + 𝑡 ∗ 𝒂 Or with variable acceleration: 𝒗 𝑡 = 𝒗0 + 𝑡 ∗ 𝒂(𝑡) Example Parabolic path of a projectile – “ballistic shot” Works for all objects if we ignore air friction - rocks, cannonballs, bullets… We have an initial velocity 𝒗0 and initial position 𝑋0 Acceleration is equal to gravity, which is a constant 𝒂 = 0, −9.81, 0 1 𝑋 𝑡 = 𝑋0 + 𝑡 ∗ 𝒗0 + 𝑡 2 ∗ 𝒂 2 𝒗 𝑡 = 𝒗0 + 𝑡 ∗ 𝒂 𝒂 𝑡 = 𝒂 Forces We want a way to compute the acceleration 𝒇=𝑚∗𝒂 𝒇 – force [𝑁, 𝑘𝑔 ∗ 𝑚/𝑠 2 ], 𝑚 – mass [𝑘𝑔], 𝒂 – acceleration [𝑚/𝑠 2 ] Multiple forces affect a single object, we need to compute the position, velocity and acceleration in the next time step with respect to all those forces Gravity (assumes flat world most of the time), air friction, contact force Getting the desired positions Forces are 3D vectors (direction + magnitude) Adding all forces affecting an object in frame 𝑁 gives us the combined force that will determine the actual movement of the object Computing the result 1. Apply forces to object 𝒇(𝑡) = 𝒇0 (𝑡) + 𝒇1 (𝑡) + ⋯ + 𝒇𝑘 (𝑡) 𝒇(𝑡) 2. Compute acceleration from forces 𝒂(𝑡) = 𝑚 3. Integrate acceleration to get velocity 𝒗 𝑡 = ‫𝑡𝑑 𝑡 𝒂 ׬‬ 4. Integrate velocity to get position 𝑋 𝑡 = ‫𝑡𝑑 𝑡 𝒗 ׬‬ 5. Move object to the desired position 𝑋 𝑡 Moving with variable acceleration Analytic solution for computing the position and velocity is hard in general We work with forces, so we start from acceleration We will not find the exact equation for 𝒗(𝑡) and thus not for 𝑋(𝑡) Impluse forces are simple Applied only in a specific frame, don’t last “between” frames Problems occur with variable forces that are applied continuously Objects in contact, friction, springs, joints… Numerical integration solves these equations Advanced topic, not covered here See more in book references Rotational dynamics We have not covered rotation of objects yet! Position 𝑋 ⇒ orientation 𝒒 (a quaternion) Velocity 𝒗 ⇒ angular velocity 𝜔 Force 𝒇 ⇒ torque 𝜏 Linear momentum 𝒑 ⇒ angular momentum 𝑳 Mass 𝑚 ⇒ inertia tensor 𝑱 Center of mass important for computing the center of rotation The process of computing rotational dynamics is similar to linear dynamics A little more complex, no time to go into detail Applying any force to an object is then split into two forces Translational & rotational Object-object interaction So far, we have talked about a single object affected by forces What happens when there are more objects? We need to detect when two objects are colliding This area is called collision detection Collision response is the next important part We know two objects are colliding, what now? Objects in contact apply forces to each other Intersections Objects (rigid bodies) are just triangular meshes or simple well-defined shapes (spheres, capsules, boxes…) We might use other objects to determine intersections For instance rays when shooting from a gun (or picking an object in 3D) Other simple helper objects Planes, Spheres, Capsules, Axis-Aligned Boxes, Oriented Boxes… When working with intersections, we might want 2 types of result Boolean – are objects intersecting? (our current focus) Computed intersection (point, triangle, mesh, …) Determining the intersection We have various different objects Rays, Triangles, Planes, Spheres, Capsules, Axis-Aligned Boxes, Oriented Boxes… To determine intersection between either two, a special algorithm must be used that is designed for the two types Easy and fast-to-compute intersection algorithms Ray-Sphere, Ray-Plane, Ray-AAB, Ray-Triangle Sphere-Sphere, Sphere-Plane, Sphere-Triangle AAB-AAB, AAB-Plane, … Thousands per second are OK Complex intersections Convex meshes Intersections with convex meshes are hard in general Usually, we need to test every single triangle Non-convex meshes Even harder than convex meshes Might need to split non-convex meshes into several convex parts for performance reasons Tens to hundreds of tests can be executed per second Heavily depends on mesh complexity Mesh-Mesh intersection Might end up testing every triangle-triangle pair ⇒ 𝑂 𝑛2 Two meshes with 10000+ triangles (not so much for rendering) will be very slow Optimizing intersections Use simpler shapes Encapsulate complex objects with simpler ones (bounding volumes) No need for exact precision Take advantage of hierarchical space-partitioning Use bounding volumes Bounding volumes Bound complex mesh geometry with simple objects Bounding Spheres Axis-Aligned Bounding Boxes (AABB) Oriented Bounding Boxes (OBB) Use hierarchies of bounding objects for compound objects (Ray)casting Casting an object (ray, sphere…) along a line in a scene to determine which objects are hit and in what order Used to determine closest objects along a line Can be used to determine all objects we hit We can work with the contact points Example: shooting a gun in a FPS game Cast a ray from the camera in the viewing direction Determine what we hit first – it’s instant (usually called “hit- scan”) If it’s a character ⇒ deal damage If it’s a wall ⇒ create a decal Much more robust than fast moving projectiles Space partitioning If we have thousands of objects in the scene and need to compute object-object intersection for each 𝑂 𝑛2 tests Create a data structure that allows fast querying of possibly intersecting objects Discard distant/irrelevant objects fast Possibilities Grids (regular, irregular) – basic, but works for simple scenes Bounding Volume Hierarchies (BVH) – much better BSP or kD-trees - best Image from https://en.wikipedia.org/wiki/K-d_tree Space partitioning (2) Need to consider dynamic objects Our space partitioning system must allow fast rebuilding of the hierarchy Or small adjustments in the tree structure on update Worst case – rebuilding the whole tree Images from http://www.essentialmath.com/GDC2007/1_GDC2007_Eiserloh_Squirrel_PhysicsOverview.ppt Collision response Once we know two objects are colliding (will be colliding in the next frame), we want to produce a correct response Need to consider physical materials Throwing a ball on concrete vs. throwing a ball on soil Friction, bounciness… Collision response is produced as additional forces A new force is produced for both objects For this, we need to determine the actual intersection locations Precision problems To maintain performance, a lot of things are simplified or faked Fixed time updates should occur in infinitely short intervals Otherwise, a fast-moving object might pass through walls (tunneling) Using static intersections instead of dynamic Using algorithms that consider linear and angular velocity of objects might solve the previous problem Usually requires much more computation Viable (fast enough) for simple bounding volumes such as spheres Solving collision response pair-wise is simple If we solve the collision for A, B and decide to move B so that it collides with C A ⇒ B ⇒ C ⇒ A ⇒ B ⇒ C… Tunneling Small & fast objects go through other objects Solutions (ideally combine all 3) Limit minimum object size/thickness Limit maximum speed Simulate more often Dynamic algorithms help For more details about Unity, see https://docs.unity3d.com/Manual/Contin uousCollisionDetection.html Images from http://www.essentialmath.com/GDC2007/1_GDC2007_Eiserloh_Squirrel_PhysicsOverview.ppt Solutions Ostrich approach Ignore the problems and let’s hope nobody notices Selective approach Select dynamic collision algorithms for more important and fast-moving objects Grenades, bullets, … Any object it will collide with is going to have to use the dynamic intersection version Or at least the “half-dynamic” version (one object dynamic, the other static) Know physics limitations Thickness of colliders & maximum object speed Amount of objects Complexity of objects – colliders, rigidbodies, joints… Unity’s physics engine The Transform component of a game objects contains the position X and orientation q We add physics through components Rigidbody - Provides mass for our object and allows forces to affect it Other settings such as drag, collision detection settings, kinematic settings… Colliders - Give our objects shape Allow us to detect collision events Can be triggers that do not actually collide but provide trigger events “regions” we can activate Are used to calculate the inertia tensor automatically Joints and cloth - Constraints and non-rigid simulation Character controller – unaffected by forces, responds to collisions Special component for controllable characters – physically unrealistic NVIDIA PhysX (3D), Box2D (2D), Havok (3D AAA replacement) Unity’s physics engine scripting Physics computation happens after FixedUpdate The Rigidbody component allows for manipulation of objects with forces Hitting objects with colliders produces contact force Computes center of mass automatically AddForce, AddTorque, AddForceAtPosition, manually setting velocity Can have physical material (friction and bounciness) Collider components Can have density (for Rigidbody auto-mass computation) Can be triggers – objects do not collide, but trigger callbacks are executed Provide collision callbacks OnCollisionEnter, OnCollisionStay, OnCollisionExit OnTriggerEnter, OnTriggerStay, OnTriggerExit Execution Order of Event functions What Unity events get called in what order Most important image for every Unity programmer!!! https://docs.unity3d.com/Manual/ExecutionOrder.html Collisions messages called only in some cases https://docs.unity3d.com/Manual/ CollidersOverview.html Unity’s physics engine scripting (2) Physics class Global physics settings (As well as Project Settings => Physics) Overlap tests (e.g. Physics.OverlapSphere) Cast tests (e.g. Physics.Raycast) Closest point computation (Physics.ClosestPoint) 08 Math for 3D games Tvorba a dizajn počítačových hier Návrh a vývoj počítačových hier Michal Ferko 14. 11. 2024 What we need the math for Placing/moving/rotating/scaling objects Creating hierarchies of objects Animation Rendering Physics & simulation Not just 3D, also 2D (but it’s simpler) Floating point numbers IEEE 754 standard Single (32-bit) and double precision (64-bit) GPUs almost exclusively single precision Most engines perform all operations as 32-bit floats GPU FLOPS Half precision sometimes used on GPUs to speed up execution Vectors and points All math we need for 3D games revolves around vectors and points We use them to represent 3D locations and directions Transforming, animating, rendering, physics… 2D variants for 2D games, but the 3rd dimension is still often used Determine which objects are in front of which objects Simulate 3D-like behavior Vectors and Points - Unity Vectors and points share classes (32-bit floats) Vector2, Vector3, Vector4 Used for positions, directions, other spatial functionality Basic operations included Addition, subtraction, multiplication, magnitude, normalization Angle, Dot, Cross, Reflect… Mathf class for basic operations Random class for RNG Transform.position,Transform.lossyScale (+local variants) Transform.rotation is a Quaternion Transform.forward, Transform.up, Transform.right Transformations Affine Transformations – talk by Jim Van Verth Orientation Representation – talk by Jim Van Verth Transform Translate/Rotate/Scale Look At Affine Transformations A mapping between affine spaces Preserves lines (& planes) Preserves parallel lines, but not angles or distances Can represent as 𝑇 𝐱 = 𝐀𝐱 + 𝐲 Affine Space Collection of points and vectors Represented using a frame: < 𝑂, 𝐢, 𝐣 > The frame defines a coordinate space Vector: 𝐯 = 𝑥𝐢 + 𝑦𝐣 𝑥, 𝑦 ∈ ℝ Point: 𝑃 = 𝑥𝐢 + 𝑦𝐣 + 𝑂 𝑥, 𝑦 ∈ ℝ Affine Transformations Maps from space to space by using frames Determines how axes change - 𝐀 Determines how origin changes - 𝐲 𝑇 𝐱 = 𝐀𝐱 + 𝐲 Examples Translation: 𝑇 𝐱 = 𝐱 + 𝐭 (axes don’t change) 𝑡𝑥 𝐭= 𝑡 𝑦 Rotation: 𝑇 𝐱 = 𝐑𝐱 (origin doesn’t change) cos 𝜃 − sin 𝜃 𝐑= sin 𝜃 cos 𝜃 Scale: 𝑇 𝐱 = 𝐒𝐱 (origin doesn’t change) 𝑠𝑥 0 𝐒= 0 𝑠𝑦 Combining Transforms 𝑇 𝐱 = 𝐀𝐱 + 𝐲 𝑆 𝐰 = 𝐁𝐰 + 𝐳 𝑆 𝑇 𝐱 = 𝐁 𝐀𝐱 + 𝐲 + 𝐳 = 𝐁𝐀𝐱 + 𝐁𝐲 + 𝐳 Order dependent! 𝑆 𝑇 𝐱 ≠𝑇 𝑆 𝐱 Can also do inverse 𝑇 −1 𝐳 = 𝐀−1 𝐳 − 𝐀−1 𝐲 Graphics APIs use matrix form 𝐀 𝐲 𝐓= 𝑇 𝟎 1 Can then use simple matrix multiplication (column vectors) 𝑇 𝑆 𝑥 = 𝐓 𝐒𝐱 Is a 4x4 matrix for 3D But most engines combine TRS into a single transform Translation vector (position in Unity) Scale vector (scale in Unity) Rotation (rotation in Unity) - Euler angles in Unity, but are quaternions under the hood Transform.rotation is of type Quaternion Unity transform details Unity combines all into a 4x4 matrix that’s sent to the GPU for rendering GPU takes the mesh vertex position and multiplies with the matrix Result is final position in world space var t = transform; var matrix = Matrix4x4.TRS(t.localPosition, t.localRotation, t.localScale); var matrix2 = transform.localToWorldMatrix; // Same result if has no parent If the object has a parent, it’s more difficult Have to combine all parent transforms (multiply all matrices) Result is a single 4x4 matrix to get world space coordinates transform.localToWorldMatrix Orientation vs Rotation Orientation – described as relative to a reference frame Rotation – changes object from one orientation to another Orientation can be represented as a rotation From the reference frame < 𝑂, 𝐢, 𝐣 > Representing rotation is tricky in 3D – we need to do Concatenation – add two rotations together to get the resulting rotation Interpolation – animate between two orientations Rotation itself – applying rotation transform to vertex positions Orientation Representation Name Concatenation Interpolation Applying Intuitive Example rotation 3x3 matrix Easy, multiply Hard Easy No 0.41 −0.67 0.61 matrices multiply vector 0.86 0.08 −0.5 0.29 0.74 0.61 Euler angles Hard, can lead Hard, cannot be Easy Yes (45°, 30°, 85°) to gimbal lock direct convert to 3x3 matrix Axis+Angle Hard Easy Easy Yes 𝐯 = 1,0,0 convert to 3x3 α = 45° matrix Quaternions Easy Easy Easy No 2 2 spherical linear convert to 3x3 𝐪= , 0, 0, matrix 2 2 interpolation For 𝐯 = 0,0,1 , α = 45° (slerp) Quaternions Generalized complex numbers In 2D A complex number 𝑎 + 𝑏𝐢 that’s normalized: 𝑎2 + 𝑏 2 = 1 𝐢2 = −1 Represents rotation of angle α, where a = cos α , 𝑏 = sin α A quaternion is the same, but in 3D Written as 𝑤 + 𝑥𝐢 + 𝑦𝐣 + 𝑧𝐤 or 𝑤, 𝑥, 𝑦, 𝑧 When normalized: 𝑤 2 + 𝑥 2 + 𝑦 2 + 𝑧 2 = 1 𝐢2 = 𝐣2 = 𝐤 2 = 𝐢𝐣𝐤 = −1 Represents rotation of angle α around axis (𝑎𝑥 , 𝑎𝑦 , 𝑎𝑧 ) Where 𝑤, 𝑥, 𝑦, 𝑧 = (cos α, 𝑎𝑥 sin α , 𝑎𝑦 sin α , 𝑎𝑧 sin α) Quaternions Can easily transform into a matrix Easy math for rotating vectors without using matrix form: 𝐩′ = 𝐪 𝐩 𝐪−𝟏 Easy uniform interpolation with slerp Unity uses it for all rotations For transforms, animation, interpolation… Provides easy conversions into it var q1 = Quaternion.AngleAxis(45, new Vector3(0, 1, 0)); var q2 = Quaternion.Euler(45, 30, 85); var q3 = Quaternion.Slerp(q1, q2, 0.3f); var q4 = Quaternion.FromToRotation(Vector3.forward, Vector3.right); Vector3 newPosition = q4 * transform.position; Quaternions Good to know how they work You will never have to do Quaternion math directly Can use other formats, engines have support for it Always converted to a 4x4 matrix before being used on the GPU Why 4x4 matrix? We want to transform points as well as vectors Using a single transform We can differentiate between points and vectors: 𝑇 Point: 𝑃 = 𝑃𝑥 , 𝑃𝑦 , 𝑃𝑧 , 1 𝑇 Vector: 𝐯 = 𝑣𝑥 , 𝑣𝑦 , 𝑣𝑧 , 0 And if we combine 3x3 rotation matrix with 3x3 scale matrix and translation vector: 𝑺⋅𝑹 𝐭 𝐌= 𝟎𝑇 1 Then all the math works flawlessly! 4x4 matrix allows other transforms You can even use non-affine transformations with 4x4 matrices And using these, you even can transform points to vectors 𝐯 = 𝑎, 𝑏, 𝑐, 0 , 𝐓𝐯 = 𝑃, 𝑃 = (𝑑, 𝑒, 𝑓, 1) (𝐓 – non-affine transform) This is then called homogenous coordinates For these transformations, you can have a result of 𝑑, 𝑒, 𝑓, 𝑔 , where 𝑔 ≠ {0,1}. In those cases, the resulting point is: 𝑑 𝑒 𝑓 , , ,1 𝑔 𝑔 𝑔 This is used for e.g. perspective camera 3D Transformation Pipeline We want to solve 3 problems Construct hierarchies of objects – Transformation of object combined with its parents Manipulate camera – viewing transformation Render object to screen – projection/screen transformation We have objects as 3D meshes (list of vertex positions in some space) How do they become pixels? Scene Graph Basic structure for hierarchical scenes Used almost anywhere for working with 2D/3D objects placed in spaces 3D modelling programs Offline Renderers used for movies All 2D/3D game engines even for video editing in some cases It is a tree structure – directed acyclic graph Nodes can have any number of children Scene Graph Object Transformation Each scene graph node has an affine transformation Affine transform is a combination of Translation, Scale & Rotation Affine transform always outputs a 4x4 matrix Transform component in Unity Final object transformation Its own transform Multiplied by the parent’s transformation Multiplied by the grandparent’s transformation … The result is a multiplication of several 4x4 matrices ⇒ one 4x4 transformation matrix Different spaces and transforms We determine object positions in the “world” (𝐓𝑀𝑜𝑑𝑒𝑙 ) Created from a hierarchy of transformations – from object through its parents By placing the camera in the world, we determine from where we are looking at the world (𝐓𝑉𝑖𝑒𝑤 ) Camera position & direction The type of camera (orthographic/perspective) determines our projection 3D ⇒ 2D (𝐓𝑃𝑟𝑜𝑗𝑒𝑐𝑡𝑖𝑜𝑛 ) We select the part of the window we render to (𝐓𝑉𝑖𝑒𝑤𝑝𝑜𝑟𝑡 ) Spaces in Unity Object ⇒ World – taken from object transform (transform.localToWorldMatrix) World ⇒ Eye – taken from camera transform (camera.transform.localToWorldMatrix) Eye ⇒ NDC – taken from camera parameters NDC ⇒ Screen – taken from camera viewport Real-time Rendering Pipeline The GPU renders 3D scenes from triangles Offline rendering is very different (ray tracing instead of rasterization) 09 AI in Games Tvorba a dizajn počítačových hier Návrh a vývoj počítačových hier Michal Ferko 21. 11. 2024 Motivation We require opponents/teammates in games Non-playing characters are usually required to perform some tasks that require AI Following the player, combat, strategic thinking… We require something that responds to user actions and imitates the behavior of human players Ideally, an AI should fool the players into thinking it is an actual human To keep the immersion level high Turing test 2 A little history Pac-man (1979) was one of the first games with character AI Follow the player, run from them, or take a random road Used different states – scatter, chase… Ghosts had different personalities New target tiles are determined by personality at every crossroad Randomness added a necessary factor – unpredictable behavior A completely predictable AI is usually easy to beat After a few tries, the player has a detailed model of AI behavior 3 The Kind of AI in games Hacks Games use a lot of hacks, not only in AI Ad hoc solutions to specific problems Heuristics – predictions that work most of the time, without guarantees Algorithms – the “true” AI Techniques that simulate behavior Usually derived from how real people or animals make decisions and perform actions Machine Learning Observe thousands of examples of player behavior Derives its own algorithm on what to do 4 Hacks - “Game AI is not AI” Is the Pac-man example AI? It’s just generating random numbers and performing one of three actions based on the result It’s not an AI technique In Sims, a lot of actions are just pre-defined animation sequences There is no actual AI going on More complex AI ≠ better AI 5 Heuristics Approximate solutions to existing problems This is usually how the human mind solves problems I lost my keys ⇒ remember when I last had them and go step by step Simple heuristic – enemy aim is 90% effective - the chance they will hit you Common heuristics Most constrained If we have two groups fighting, and one character in one group has a unique weapon that pierces through some unique armor, it should attack a character wearing that armor Most difficult first – if you can buy a strong unit, do it instead of getting a few weak ones Most promising first – perform the action that will improve your chances the most E.g. Chess AI 6 Algorithms Some AI actions still require other algorithms Movement of characters Decision making Tactics or strategy Analysis of game state and future game state 7 Academic AI vs. Game AI Academic AI – be as smart as possible Solve the problem as efficiently and precisely as possible E.g. 99.99% guarantee required that a traffic camera identifies license plates correctly Game AI - make the player have fun Provide interesting challenges for the player React to the player Be predictable enough for the player Be believable enough to keep the illusion of a real being in control 8 Game State Analysis Process input data (game state) to simplify the decision process This is usually called sensing - you create senses for the AI Vision Hearing Touch Smell? … 9 Gameplay AI is a 3-step process 1. Sense – what can I see/hear/feel Some senses can cheat! 2. Think – consider what I do next based on what I am sensing Process data from senses and decide what to do 3. Act – perform actions I have decided to do Walk to a destination Attack someone Use an item … 10 AI Difficulty For some games, creating skilled AI is simple Counter-strike: aim & shoot to kill instantly Difficult ≠ Fun “Dumb down the AI” – sometimes misses, isn’t perfectly efficient Rubber-banding – adjusting to the player to always offer a reasonable challenge Trying waiting for 30 seconds in a racing game For more complex games, creating challenging AI is a complex task StarCraft 2 – there are hundreds of options of what to do at any moment Most difficult AI bots cheat, since the AI cannot compete with more skilled players Sense & Act is easy, Think is the difficult part Chess has a much smaller possibility space Can simulate 15-20 moves into the future 11 An example: Sims When does a sim become hungry? What will the sim do if they really have to go to the bathroom? These are several competing systems that are weighted Changing these weights alters the behavior of the sim Final decisions about the next are strongly affected by the weights When a sim is about to pass out of hunger Getting food becomes top priority AI navigation will help move them to the nearest food source The weight of the hunger system represents the desire to get food 12 AI types in games Hard-coded – deterministic behavior If player health above 80, fire gun at player Randomization – randomized behavior If player health above 70-80, fire gun Less predictable, more realistic Weighted randoms – every possible next step is given a weight Some of the possibilities will happen more often than others Weights control what happens in the end 13 Weighted randoms example We have a creature that can perform 3 actions We assign weight to these actions Attack 60%, Cast spell 30%, Run away 10% Very similar to the Sims example, but those weights change over time X = RandomFromRange(0, 99); if (X < 60) Attack(); else if (X < 90) CastSpell(); else RunAway(); 14 Finite State Machines (FSM) Several states an entity can be in (sleeping, wary, attacking, running away…) We define rules to transition from one state to another There does not have to be a transition from each state into every other state The transition running away ⇒ sleeping does not make sense We define when transitions from one state to another should happen 15 Finite State Machines (2) Based on the current state of the entity, perform an action Patrol ⇒ walk through corridors along a pre-defined path Attack ⇒ move continuously towards the player while shooting A proximity mine can use the same “proximity” check as the guard If near, move to state X 16 Finite State Machines (3) Decision making is encapsulated in the transition rules Transition rules can incorporate a certain degree of randomization Such as an enemy running away at less than 15-25% health This is called reactive AI – always react to a game event The other type is active AI – constantly look for the best option A sim in Sims An AI opponent in Starcraft 2 17 Decision trees Decision trees are a simple way of representing decision making The inner nodes of a decision tree are decisions with only two possible answers: Yes or No Leaf nodes are action nodes Other nodes have two children one for the Yes answer, one for the No answer We traverse the tree from root 1. Evaluate conditions till you get to a leaf node 2. Perform the action of the leaf node 18 Decision trees (2) Apply an action every time a decision needs to be made Decision trees can be shared, as can be individual nodes Decision trees are sometimes built-in visual tools Programmers write code for decision nodes and action nodes (AI) designers connect these to build an actual tree – the AI “mind” With each decision we ignore a whole sub-tree This is relatively efficient even for hundreds of nodes The decision might take several frames to decide Save the node in which decision making is paused Resume tree traversal in the next frame (decisions might be delayed) 19 Decision trees (3) Can also be non-binary 20 Behavior Trees Widely used in games It’s a tree composed of nodes Each node can return a status to its parent Success – the operation of this node finished successfully Failure – the operation failed Running – the operation is still running Nodes can have parameters Nodes can respond to context Game state Available in Unity as a package Unity Behavior Built-in support in Unreal 21 Behavior Trees (2) Leaf nodes represent actions State: Running, Success or Failure E.g. Open a door, Run towards the player Composite nodes (Sequence, Selector…) Encapsulate multiple children Execute children in some order Returns what is returned from children Decorator nodes (Inverter, Repeater…) Have a single child node Transform result from the child, repeat, terminate E.g. 22 Behavior Trees – Actions Action Walk Parameters Character Destination – location or another character Running On the way Success Reached destination Failure Failed to reach destination (blocked/died/stunned…) Init – called the first time the node is visited Process/Update – called every tick while the node is “running” 23 Halo 2 24 Fuzzy logic Decision trees work quite well, but it’s not realistic enough Using absolute threshold values to decide There should be a range of values that allow for both decisions to happen Proximity test 5 meters is still too far sometimes 7 meters is close enough sometimes The idea of fuzzy logic is that objects belong to multiple fuzzy sets by different amounts A player partially behind cover can be in sets “in cover” as well as “exposed”, however we assign percentages for each set ⇒ 60% in cover, 40% exposed 25 Fuzzy logic (2) The process of assigning the degrees of membership is called fuzzification In order to decide, we might have to defuzzify the membership degrees and give an exact result to which set we fully belong Simple fuzzification: Cutoff values for fully belonging to a set Proximity ⇒ 2 sets “near” and “far” 4 meters = near, 7 meters = far between 4 and 7 meters weighted randomness decides 26 Fuzzy logic (3) Defuzzification is much harder From several degrees, we must choose the correct one Just generating a random number and considering which set is more likely to occur can work is some situations We cannot just take the set with the highest degree fuzziness provides a chance for something unlikely to happen If the result is just a number, it is much easier to defuzzify An AI might be cautious, when combined with the fact that the player is behind cover, we generate a number that says how long the AI will take to aim For boolean values, we determine a cutoff and then compare it to the degree 27 Fuzzy logic (4) The real power comes from rapid AI prototyping If (distance < 20 AND health > 1) then Attack() If (player is close AND I am healthy) then Attack() We are using two fuzzy sets in the example We need to redefine the AND, OR and NOT operators for fuzzy sets It’s no longer Boolean logic AND → 𝑃 = min 𝐴, 𝐵 OR → 𝑃 = max(𝐴, 𝐵) 𝐴, 𝐵 − degrees of membership 𝑃 − final probability 28 Utility theory “Utility theory says that every state has a degree of usefulness, or utility, to an agent and that the agent will prefer states with higher utility.” We take the current world state, think of what would happen if we performed some action What changes in the world state can be used to derive how much that agent improved its “happiness” Actions with the highest utility value are chosen and performed 29 Utility theory – examples Chess is perfect for executing Utility theory If one action causes me to lose an important piece in the next move ⇒ most likely low utility value There are exceptions of course Predict all possible outcomes in the next few steps Choose the step maximizing the utility value A strategy game considers multiple things Troop strength Base/worker safety Estimated enemy strength Research level, amount of resources 30 Utility theory in practice 1. Make a copy of the game state 2. Perform the action (can take several seconds) 3. Evaluate what happened – how did utility change Might require player prediction Usually localized decisions – the entire game state is not needed In Sims, a sim usually cares only about themselves If the sim is hungry, eating will improve his happiness the most So they go to the kitchen In FPS games, the agents have simple utility preferences Agents will be preferring states where they continue to live And prefer when the player will have low health as a result of their actions 31 Goal-oriented action planning (GOAP) Utility theory decides what an agent wants to do, not HOW to do it GOAP is working with goals – desirable world states the agent wants to achieve these states by performing actions Simple goal: kill the player Attacking the player is one action that achieves this An agent has multiple goals, but usually only one active at a time Two-stage process: Goal selection – pick the most relevant goal Execution – solve the goal by executing actions Goal selection is solved using other methods Decision trees, utility theory, … The second stage needs a special solution https://www.gamedeveloper.com/programming/postmortem-AI-action-planning- on-Assassins-Creed-Odyssey-and-Immortals-Fenyx-Rising- 32 GOAP (2) Say a character is hungry You have no food, so you need to create a plan to obtain food Could be going into the woods to hunt animals, then extract the meat, cook it, and finally eat it Each action has a set of conditions it can satisfy, as well as a set of prerequisites that need to be satisfied Eating food requires cooking food Cooking food requires having raw food Having raw food requires buying raw food Buying food requires money Money requires a job The algorithm walks back through these preconditions and identifies which actions need to be executed 33 GOAP (3) A sequence of goals might not exist There are lots of problems with world representation (not only for GOAP) I desire a world state in which I am not hungry I desire a world state in which the player is dead We need to generate this world state with preconditions and effects Search for the shortest (or least difficult) path in a graph of actions There are many ways to solve a goal We always walk back from the desired state to the current state Trying to find a way that could work Quite advanced, but allows for very “intelligent” AI 34 Path-finding Not really an AI technique, more of a support technique for other AI Simply searching for the shortest path from A to B We have nodes and edges Nodes describe points that the agent must be able to Image from https://en.wikipedia.org/wiki/Pathfinding reach Nodes are connected by edges – straight lines An agent moves along an edge to get to another node To get to a neighboring node, you just rotate the agent and move them along the corresponding edge 35 Path-finding (2) Moving along straight lines is highly unnatural Except for robots maybe Nodes may be in a grid, resulting in not very smooth motion A few possibilities to avoid this: Irregularly placed nodes Allow each node to have a tolerance as to how close the agent must be to consider that they visited the node Placing an interpolation curve (e.g. piecewise bezier curve) through the nodes 36 Path-finding (3) Edges may be unidirectional, bidirectional, even weighted Higher weight means a harder to pass route Weights could even be different for different types of agents Flying units versus ground units, or units that can walk up cliffs Results in different paths taken by different agents Weights can be dynamic Building something on top of existing nodes sets the weight to infinity Flying units might try to avoid guard towers, so the guard towers increase the weight of nearby edges 37 A* path-finding (aka. A-star) There are lots of algorithms that solve the path-finding problem A* is the most used one Relatively fast to compute Has lots of modifications https://en.wikipedia.org/wiki/A*_search_algorithm 38 Path-finding – taking it a step further Another common technique is called a navigation mesh (navmesh) It is a simple mesh that describes all walkable terrain in the level Can be artist generated Much better is when it’s generated automatically Might require some tweaking by artists or designers Triangles are nodes, edges are between neighboring triangles A* can be used, we just have to set the tolerance values based on the triangles 39 AI in Unity Limited AI support without plugins Can use Unity Behavior for Behavior Trees Can use Unity’s Animator for Finite State Machines Can use Visual Scripting for Finite State Machines Has ML Agents package for reinforced learning Making your own is not that hard for simple games Other free/paid plugins: Behavior Designer, NodeCanvas, Apex Utility AI… Writing it yourself (no visual representation) is also OK But think about configurability & the potential to modify it 40 AI in Unity Unity has built-in support for NavMesh Path-finding Static NavMeshes Dynamic obstacles and priorities Rebuild NavMesh dynamically Only for 3D For 2D Use NavMeshPlus – https://github.com/h8man/NavMeshPlus Built on top of Unity’s 3D NavMesh Use A* Pathfinding Project - has a free/paid version – https://arongranberg.com/astar/ 41 10 Business of Video Games Tvorba a dizajn počítačových hier (FMFI) Návrh a vývoj počítačových hier (FIIT) Michal Ferko 28. 11. 2024 Business of Video Games Platforms Genres Target audiences Business models Unique selling points Platforms Platforms Arcades – mostly dead Standalone game systems in public venues Pong, Pac-man, Space Invaders… Consoles Proprietary platform Xbox Series X, PS5, Nintendo Switch More difficult submission process – dev kits, platform requirements… Computers (+Steam Deck?) Various HW – problems with compatibility and HW requirements Various SW – problems with unified development VR Mobile/Dedicated Motion tracking Platforms (2) Browser games – usually casual Mostly HTML5, limited capabilities Used to be Facebook games Mobile games – usually casual Games on portable devices such as phones or tablets Small screen, limited keyboard or touch-based input Gyroscope, GPS, Camera “Toilet and bus test” Handheld GameBoy, PSP, PS Vita Nintendo Switch Steam Deck Platform Dependent Development The platform usually dictates different I/O devices Controllers Screens, haptic feedback… Single-platform games Easier development, especially consoles (HW is always the same) Multi-platform games Need to solve differences in I/O devices Varying game difficulty due to different I/O methods Varying hardware capabilities Different OS = different technical issues Hint for beginners: focus on a single platform! Or at least similar types of platforms Platform Stores Stores tied to a platform Console store are exclusive – can only get games on the platform from there Xbox ⇒ Microsoft Store PlayStation ⇒ PlayStation Store Switch ⇒ My Nintendo Store Can still buy physical games, but those also go through the respective store PC/Mac stores are not exclusive General Steam, Epic Games Store, GOG, Microsoft Store, Humble Store, Itch.io, Mac App Store… Publisher-specific – difficult to get into without a publisher deal Ubisoft – Ubisoft Connect (Uplay), EA – EA app (Origin), Battle.net (Blizzard) Platform Stores Android is non-exclusive Google Play – biggest outside China Huawei App Gallery – biggest in China Galaxy Store (Samsung) Xiaomi Market … iOS is exclusive EU forced Apple in 2024 to allow other stores But installation of those stores is still crazy difficult and accounts for very few percent Might change in a few years Has unofficial stores for jailbroken devices Extremely small ones, jailbroken devices are very rare Platform revenue cuts PC/Mac Steam – 30% Epic Games Store – 12% Microsoft Store – 12% GOG 30%, Humble 25%, Itch.io any% Mac App Store – 30% (15% for the first $1M in a year) Consoles (also for physical releases) PlayStation, Xbox, Nintendo – 30% Mobile Google Play – 30% (15% for the first $1M in a year) Apple App Store – 30% (15% for the first $1M in a year) Genres Genres Action Sandbox Platformers Survival Shooters Horror Racing Fighting Sports Adventure Rhythm Action-adventure Simulations New genres show up from time to time Rogue-like RPGs MOBA Strategy Battle Royale Casino Souls-like Puzzle … Player Modes Single-player Local multiplayer Co-op Versus LAN multiplayer Online multiplayer Lobby-based Persistent Why have genres? Helps gamers quickly understand what you’re promising Reduce the amount of player mismatch Helps with marketing Reduces the amount of game design decisions you have to do Games need to be familiar for people to be attractive Some designers say familiarity should be 80/20 80% familiar 20% novel Players Target Audience Need to target a specific group Creating a game that will satisfy everyone is impossible Target a small subset of gamers Male aged 15-25 will probably play different games than Female aged 35-50 Which of the categories is most fun? Highly subjective Every combination of player/time/platform ensures a unique experience by Quantic Foundry Target Audience Not just age & gender Why do they play games? What do they enjoy? How often do they play? When do they play? How long do they play? On what platform do they play? Why do they stop playing? Players Hyper-casual – not invested Very short play sessions (2-5 min), not invested, players easily jump to the next one Super easy to learn Casual Shorter sessions (10-20 min), less time spent playing Easier games, slower pace Mid-core (Core) – more involved Longer sessions (20-60 min) Not obsessive, but willing to do harder challenges Enthusiastically plays different types of games Hard-core – “obsessive” Long sessions (60+ min), strong dedication to finish/achieve/… Actively consumes games media even when not gaming Acquiring Gamers Hyper-Casual Organic reach (=> app store optimization - ASO) Pay pre click ads (PPC) Casual PPC Organic Mid-core (Core) PPC Press/Influencers Hard-core – “obsessive” Press/Influencers Publishers Word of mouth works for all, but is hard to evaluate Business Models Business models Premium Free to Play Premium Game as a Service Arcade Subscription Subscription Pass Premium Games Spend longer times to develop games Build up audience before global launch Pay per-click ads (PPC) Community management Social media, press, influencers Crowdfunding Well-known publisher Franchise Global launch with large marketing push Scheduled reviews, streamers, wishlist emails… Early access Estimating Steam game sales usually comes from # of wishlists before launch Earning from Premium Games Fixed price for a game DLCs Discounts – A LOT of game units are sold during discounts Battle passes, season passes Additional in-game purchases Cosmetics, loot boxes Don’t forget platform cuts Typical earnings from a premium game There are exceptions – e.g. Among Us Estimating success Wishlist numbers 15-20% wishlist conversion at launch Most people wait for bigger sales Wishlist quality – hard to estimate Social media following Media/Influencer coverage Sales Week 1 ≈ Month 1 (after Week 1) ≈ Year 1 (After Month 1) SteamSpy on similar games Has sales estimates # of reviews on similar games Owned copies = # of reviews * multiplication factor Multiplication factor somewhere between 20-80 Free to Play (f2p) Game as a service (GaaS) ARM business model– Acquisition, Retention, Monetization

Use Quizgecko on...
Browser
Browser