Spawning and Destruction - SC-SGS/surviving-sarntal GitHub Wiki

Purpose

This page aims to explain the implementation details of the spawning and destruction process. The lifetime of entities is managed in the spawning and destruction phase.

Item spawning

Items are spawned by the ItemSpawner.

Spawn Time Determination

The `ItemSpawner` class determines the next spawn time by generating a random interval between 2 and 10 seconds, this can be changed in the configuration. This interval is added to the current game time to set `nextSpawnTime`. The spawn time is updated after each item is spawned.

Item Type Determination

The item type to be spawned is selected based on predefined spawn weights. Each item type has a spawn weight, and a random number between 1 and the sum of all spawn weights is generated. This random number is used to select an item type, with higher-weighted items being more likely to be chosen

Item Spawn Position Determination

The item spawn position is selected based on the current maximum x position plus an offset so that the spawning happens off camera. The height of the spawn position is determined as an offset of the mountain height at the set position. The offset is a random number from a given range that can be configured in the configuration.

Terrain

The terrain generation is performed concurrently. See here.

Rock spawning

Rocks are modelled as random convex polygons with a type (e.g. snow/ice/lava/crystal). The type is chosen randomly with varying probabilities as the game progresses. It determines a rock's texture and its density $\rho$.

Polygons are obtained by generating a random set of points within a random radius and the subsequent application of Andrew's monotone chain algorithm \cite{polygon_generation}. The algorithm determines the convex hull of the set of points as the resulting polygon. In addition to density, polygons possess mass and a moment of inertia as static properties. The mass $m$ is defined as the area of the convex polygon times its density $m = \rho A$. For the moment of inertia $I$, the calculation is based on Fotino's approach. It is illustrated below The polygon is split into counter clockwise triangles $OV_iV_{i+1}$ with the centroid $O$ constituting the origin. These triangles are subdivided into right triangles. For the inertia, the following holds:

$$ I = \sum_{i=0}^{#vertices-1} I(OV_{i}V_{i+1 \mod #vertices})\\ I(OV_{i}V_{i+1}) = \alpha_{OV_{i}H_i}\cdot I(OV_{i}H_i) + \alpha_{OH_iV_{i+1}}\cdot I(OH_iV_{i+1})\\ = \rho \cdot \Bigg(\alpha_{OV_{i}H_i}\cdot \left(\frac{h_iw_{1,i}^3}{4} + \frac{h_i^3w_{1,i}}{12}\right) + \alpha_{OH_iV_{i+1}}\cdot \left(\frac{h_iw_{2,i}^3}{4} + \frac{h_i^3w_{2,i}}{12}\right)\Bigg) $$

$\alpha$ is 1 if a triangle is counter clockwise and -1 otherwise.

After the polygon's static properties are calculated, rocks are spawned at a random position, with random angular momentum, and random linear momentum in x-direction.

inertia

Figure: The convex polygon is triangulated. The inertia of the polygon is the sum of the inertia of all right subtriangles. On the right, the blue triangle $OV_iH_i$ is clockwise and thus, its inertia has to be subtracted.

Destruction

At the end of a physics update, rocks that have left the scope of the world or hit the hiker are destroyed and removed. Similarly, old biomes are destructed. This garbage collection ensures a consistent performance no matter how long the game runs.

$\rightarrow$ Check out the dynamic properties of polygons here.

⚠️ **GitHub.com Fallback** ⚠️