# SPG:Game Objects

### From Sonic Retro

Notes: Research applies to all four of the Mega Drive games, and Sonic CD. If there are any varying differences between the games, this will be covered below.

## Contents

## Bridges

The bridges in Sonic 1, 2 and 3 are known for their dynamic movement as Sonic moves over them. Bridges are set up with a controller object and an array of log objects which the controller object creates, though this can just be an array of values to represent the segments in most engines now. The controller object contains a few variables: There's the length (in segments) of the bridge, which is usually 12, but it can be longer; there's the index of the segment Sonic is standing on, which starts at 0 for the leftmost segment and ends at length-1.

#### Depression Amount

The depression amount is the lowest the bridge can go at any given time. This changes depending on the log Sonic is standing on. To get the current maximum depression amount in pixels the bridge controller uses predetermined values for each log.

The values go up in 2s, starting at 2 and continuing to the middle, with the other side being a mirror of the first half. For example, a bridge with length 5 will have the values 2,4,6,4,2 and a bridge with length 6, the values would be 2,4,6,6,4,2. The bridges commonly found in Sonic (12 segments in length) would have the values 2,4,6,8,10,12,12,10,8,6,4,2.

To get the current maximum depression for the bridge, the game uses the value from the log currently being stood on. We'll call the current value MaxDepression.

#### Calculating Each Log Depression

The Y position of the segments in the bridge depend on the log that Sonic is currently standing on, as so:

Sonic is on the 5th segment, we'll call this the CurrentSegment and it's value is 5.

In this example, the depressions would look as follows: 2,4,6,8,10,12,12,10,8,6,4,2 So the current MaxDepression for the bridge will be the 5th log's value, which is 10.

To calculate the position of each log, we calculate how far it is from CurrentSegment relative to the edge it's near. We will call this value LogDistance.

Segment 0 is 1/5 of the way to CurrentSegment, so it's LogDistance is 1/5 or 0.2.

Segment 1 is 2/5 of the way to CurrentSegment, so it's LogDistance is 2/5 or 0.4.

Segment 4 is 5/5 of the way to CurrentSegment, so it's LogDistance is 5/5 or 1.

Working from the other side, we use the distance from the end to CurrentSegment, rather than from the start.

Segment 11 is 1/8 of the way to CurrentSegment, so it's LogDistance is 1/8 or 0.125.

Segment 6 is 6/8 of the way to CurrentSegment, so it's LogDistance is 6/8 or 0.75.

(Since we've already calculated segment 4 from one direction, there's no need to do it from the other).

We then use LogDistance to calculate it's position:

LogY = BridgeY + MaxDepression * sine(90 * LogDistance).

Some code to calculate these automatically may go as follows (assumes first segment index starts at 0 in loop for other purposes, but CurrentSegment would start at 1).

for (i = 0; i < SegmentAmount; i++) { //get difference of log var log_difference = abs((i+1)-CurrentSegment); //get opposite distance from current log to a side, depending if before or after CurrentSegment if (i < CurrentSegment) LogDistance = log_difference / CurrentSegment; else LogDistance = log_difference / ((SegmentAmount - CurrentSegment) + 1); //get y of log using max depression and log distance (reversed) LogY = BridgeY+((MaxDepression) * sine(90 * (1 - LogDistance))); }

Meanwhile, all these depression values are multiplied by the sine of the angle in the controller object that counts to 90 when Sonic is standing on top, and down to 0 when Sonic gets off, so the depression will smoothly increase with time when Sonic jumps on to the bridge, and then smoothly decrease when he leaves it.

## End of Level Capsules

### Sonic 1 Method

#### Explosion

For 60 steps, every 8 steps, spawn explosion at capsule position plus random x,y offset (Max horizontal offset of 31 pixels, and according to calculations, vertical is the same). At end of those 60 steps, start with the animals

#### Animals

Switch to exploded frame. Spawn 8 animals at capsule position -28x, +32y, horizontally separated by 7, with alarms starting from 154 and decreasing by 8 per animal (animals don't jump out until their alarm reaches zero).

For 150 steps, every 8 steps, spawn animal at random x position amongst the existing animal group (but tighter in, not near edges), with their alarm set to 12.

When all animal objects have disappeared, run Got Through message.

### Sonic 2 Method

#### Explosion

Spawn explosion at lock's position, move lock at +8x, -4y. Wait 29 steps.

#### Animals

Spawn 8 animals at capsule position -28x, +32y, horizontally separated by 7, with alarms starting from 154 and decreasing by 8 per animal (animals don't jump out until their alarm reaches zero).

For 180 steps, every 8 steps, spawn animal at random x position amongst the existing animal group (but tighter in, not near edges), with their alarm set to 12.

When all animal objects have disappeared, run Got Through message.