scene modeling, mazes, and terrain

26
Scene modeling, Mazes, and Terrain CSE 3541 Matt Boggus

Upload: alec-travis

Post on 03-Jan-2016

74 views

Category:

Documents


2 download

DESCRIPTION

Scene modeling, Mazes, and Terrain. CSE 3541 Matt Boggus. Overview. Hierarchical scene modeling and Parenting in Unity Mazes Terrain. Sample Scene. Hierarchical scene – chair 1. Hierarchical scene – chair 2. Hierarchical scene – chessboard. White chess pieces – child of chessboard. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Scene modeling, Mazes, and Terrain

Scene modeling, Mazes, and Terrain

CSE 3541Matt Boggus

Page 2: Scene modeling, Mazes, and Terrain

Overview

• Hierarchical scene modeling and Parenting in Unity

• Mazes

• Terrain

Page 3: Scene modeling, Mazes, and Terrain

Sample Scene

Page 4: Scene modeling, Mazes, and Terrain

Hierarchical scene – chair 1

Page 5: Scene modeling, Mazes, and Terrain

Hierarchical scene – chair 2

Page 6: Scene modeling, Mazes, and Terrain

Hierarchical scene – chessboard

Page 7: Scene modeling, Mazes, and Terrain

White chess pieces – child of chessboard

Page 8: Scene modeling, Mazes, and Terrain

Rook piece – child of white chess pieces

Page 11: Scene modeling, Mazes, and Terrain

Simple mazes – binary grid

• Matrix of booleans• ex: 21 x 21

• Arbitrary mapping:– True = black pixel = wall– False = white pixel =

open space

Page 12: Scene modeling, Mazes, and Terrain

Creating the scene

Foreach (i,j) in boolean[x,y] mazeif(maze[i,j])

Create Cube at position (i,0,j)

Page 13: Scene modeling, Mazes, and Terrain

Creating GameObjects

for(int i = 0; i < numberOfAsteroids; i++){ GameObject aSphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); aSphere.transform.parent = transform; aSphere.name = "sphere" + i.ToString();

aSphere.transform.position = new Vector3(Random.Range(-10.0f, 10.0f), Random.Range(-10.0f, 10.0f),

Random.Range(-10.0f, 10.0f));

aSphere.transform.localScale = new Vector3(Random.Range(0.0f, 1.0f), Random.Range(0.0f, 1.0f),

Random.Range(0.0f, 1.0f));}

Page 14: Scene modeling, Mazes, and Terrain

Deleting GameObjects

GameObject myObject;// do stuff with myObject (create, animate, etc.)Destroy(myObject);/* Note: myObject still exists as a variable name, only the corresponding GameObject in the scene is removed */

Page 15: Scene modeling, Mazes, and Terrain

Procedural maze initial values

All open space – add walls All walls – add open space

Page 16: Scene modeling, Mazes, and Terrain

Checking for a solution – Flood-fill

• Determine the area connected to a given node in a multi-dimensional array

• Applications:– “Paintbucket” tool– Connected components– Pathfinding

Animation fromhttp://en.wikipedia.org/wiki/Flood_fill

Page 17: Scene modeling, Mazes, and Terrain

Recursive flood-fill codeFlood-fill (x, y, value, grid) { if(x < 0 || y < 0 || x >= XSIZE || y >= YSIZE) return; if(grid[x,y] == -1 || grid[x,y] == value) return;

grid[x,y] = value; Flood-fill(x-1,y, value, grid); Flood-fill(x+1,y, value, grid); Flood-fill(x,y+1, value, grid); Flood-fill(x,y-1, value, grid);}

Note: method is prone to stack overflow

Page 18: Scene modeling, Mazes, and Terrain

Iterative flood-fill algorithmFlood-fill (x, y, value, grid) { Set Q to the empty queue Add position(x,y) to Q While Q is not empty { Dequeue position p if (p.x or p.y out of bounds) continue; if (grid[p.x,p.y] == -1 || grid[p.x,p.y] == value) continue; grid[p.x,p.y] = value; Enqueue (p.x-1,p.y); Enqueue (p.x+1,p.y); Enqueue (p.x,p.y-1); Enqueue (p.x,p.y+1); }

Page 20: Scene modeling, Mazes, and Terrain

Heightfield definition

• Function u(x,y) gives height at (x,y)• Store height values in an array u[x,y]• Note: limited to one height per (x,y)

Page 21: Scene modeling, Mazes, and Terrain

Heightfield smoothing

• For every grid cell u[i,j], set it to average of itself and neighbors

• Critical thinking – implementation concerns:– A. looping order– B. boundary cases– C. both– D. none

For more terrain creation and editing operations, see http://www.lighthouse3d.com/opengl/terrain

Page 22: Scene modeling, Mazes, and Terrain

Heightfield mesh creation

u[x,y] ; dimensions n by n

Page 23: Scene modeling, Mazes, and Terrain

Heightfield mesh creation (clockwise winding order)

Quad[0,0] Vertices are U[0,0], U[0,1], U[1,1], U[1,0]Quad[i,j] Vertices are U[i,j], U[i,j+1], U[i+1,j+1], U[i+1,j]

Page 24: Scene modeling, Mazes, and Terrain

Heightfield creation (iteration)

Inner loop iterates over i, the x coordinate Last quad: i=5(i = n-1)

Page 25: Scene modeling, Mazes, and Terrain

Heightfield creation (iteration)

Outer loop iterates over j, the z coordinateLast row: j=5 (j = n-1)