Download presentation
Presentation is loading. Please wait.
Published byHomer Bishop Modified over 6 years ago
1
Background Shapes & Collision Resolution (Top-down and Side-scrolling)
CS 134 Background Shapes & Collision Resolution (Top-down and Side-scrolling)
2
Today in Video Games
3
Physics Update Overview
So far: Collision Detection (sprite / sprite) To cover: Collision Detection (sprite / background) Collision Resolution Advanced Actor Motion
4
Backgrounds Recall how we stored the tile positions in an array?
int[] bg; Collision can go there too!
5
Backgrounds class Tile { int image; boolean coll; }; Tile[] bg;
Other information can go in a tile as well!
6
Backgrounds As the player moves, you will need to collide against all the background shapes. AABB / AABB Use the camera drawing optimization Start: pX / tileW End: (pX + pW) / tileW Similar in Y
7
Questions? (Remember, this is just for DETECTING collisions)
Backgrounds Questions? (Remember, this is just for DETECTING collisions)
8
Collision Resolution The job of collision resolution is to “fix” the simulation state so that it is accurate again.
9
Collision Resolution For now, assume a simple top down game
Up / Down / Left / Right move player Background is “block shaped”
10
Collision Resolution Simplest possible collision resolution is with bullets Deal damage to player Remove bullet Check if player is dead
11
Collision Resolution For colliding with walls
Simple solution: Back up player to prev position More complex: Back up player to when the collision starts This may be enough for some games!
12
Collision Resolution Best feeling solution: Back up on each axis
Back up on X axis by overlap with wall tile Back up on Y axis by overlap with ceiling tile This allows smooth sliding along walls Also, can do things like wall jumps, pushing, etc. by remembering which axes you collided with prev frame More state for a player!
13
Collision Resolution How do you know the overlap?
Collision is just returning true / false! right1 < left2 is false, return right1 – left2 Similar for other sides
14
Collision Resolution Questions?
15
Actor Motion Basic actor motion isn't enough for many games
In maze games, the player should be given some give to going around corners In platformers, the player should be affected by gravity One way walls are a common thing too
16
Actor Motion Maze edge motion:
When moving, if you are just barely colliding, fix the motion to a tile boundary Here, pressing up will still get Link into the fortune teller's house
17
Actor Motion Add new state, set during motion to detect near misses
When you release pressing the direction, clear the near miss state. If in a near miss state, move in the near miss direction instead
18
Actor Motion Frame 1: Link would collides with statue, set near miss left, moves right Frame 2: Still in near miss state, move right Frame 3: No longer in near miss state, move up
19
Actor Motion
20
Raycasting Recall: Physics is running at a fixed rate
Collision is only checked AFTER objects move Problem: Fast moving objects can jump past an object Raycasts shoot an infinitely long ray to collide with AABB / Circles
21
WARNING! WARNING! INCOMING MATH
Raycasting WARNING! WARNING! INCOMING MATH
22
Ray / Circle Ray formula: Circle formula: 𝑝=𝐀+𝑡𝐁 𝑡≥0
𝑟=∣𝑝−𝐎∣ 𝑟= 𝑝−𝐎 ⋅ 𝑝−𝐎
23
Ray / Circle Plug ray into circle formula and simplify:
24
Ray / Circle Three possibilities (solutions for t): t has no solutions
Ray does not intersect the circle t has one negative, one positive solution Ray starts inside the circle, collides at positive solution t has two positive solutions Ray hits circle, use the lesser solution
25
Ray / Circle Optimization
Remember, we don't care if ray starts inside of circle! Calculate t via: Collision is only when t is positive. 𝑡= −𝑏− 𝑏 2 −4𝑎𝑐 2a
26
Ray / AABB Intersect ray with all sides, choose earliest intersection that is inside box Four sides of of AABB are: x = left x = right y = left y = right
27
Ray / AABB Plug into side formula and simplify:
A_x + t B_x = left → t = (left – A_x) / B_x A_x + t B_x = right → t = (right – A_x) / B_x Solve for t Given t, check if specified point is in box edge Top < A_y + t B_y < bottom Then do same thing for y, and choose earliest t
28
Ray / AABB Optimization
For any particular direction, only two sides could be the earliest Unless the ray is exactly parallel to X or Y, only the later of the two times can be inside the box
29
Homework 3 Some sort of projectile with “enemies” to hit.
When the projectile hits the enemy, the projectile goes away. Enemies can have health, so they need to be hit multiple times. Extra credit: Enemies can shoot projectiles too! Raycast-style instant shots
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.