Actions

SCHG How-to

Fix jump height bug when exiting water

From Sonic Retro

(Original guide by MoDule)

There is a bug in Sonic's water entry and exiting routines that is present in every main series 16-bit Sonic game. When Sonic is just above the point at which he would be considered submerged and jumps his jump height will be very low. This guide will show a possible way to fix this bug and explain its cause.

Fixing the bug

Locate the Obj01_InWater routine. It should look like this:

; loc_1A18E:
Obj01_InWater:
	move.w	(Water_Level_1).w,d0
	cmp.w	y_pos(a0),d0	; is Sonic above the water?
	bge.s	Obj01_OutWater	; if yes, branch

	; <--

	bset	#6,status(a0)	; set underwater flag
	bne.s	return_1A18C	; if already underwater, branch

At the indicated line, add this:

	tst.w	y_vel(a0)	; check if player is moving upward (i.e. from jumping)
	bmi.s	return_1A18C	; if yes, skip routine

Why this fixes the bug will be explained further down.

Things to keep in mind

This is a general player object bug, so it will need to be fixed for Tails and Knuckles, too, if they are present.

The bug explained

This bug is the result of the in- and out water routines interacting in a way that isn't immediately obvious. On their own, they wouldn't cause this bug at all, since their conditions are mutually exclusive. The actual trigger lies elsewhere, in Sonic's jumping routine:

; loc_1AA38:
Sonic_Jump:
	move.b	(Ctrl_1_Press_Logical).w,d0
	andi.b	#button_B_mask|button_C_mask|button_A_mask,d0 ; is A, B or C pressed?
	beq.w	return_1AAE6	; if not, return

	[...]

	move.b	#$E,y_radius(a0)
	move.b	#7,x_radius(a0)
	move.b	#2,anim(a0)	; use "jumping" animation
	bset	#2,status(a0)
	addq.w	#5,y_pos(a0)	; <--

return_1AAE6:
	rts

Right at the very end, before the routine returns, Sonic's position is increased slightly, moving him down. Now it's important to know, which order these routines run in. First, Sonic's movement code is run, where it checks if the player pushed a jump button. This is when Sonic is put in a jumping state, given some upward velocity and moved slightly down. Some time after that Sonic's water checks are run. If he was already very close to the water surface he might have been pushed under water by jumping. Now observe the code at Obj01_InWater:

; loc_1A18E:
Obj01_InWater:
	move.w	(Water_Level_1).w,d0
	cmp.w	y_pos(a0),d0	; is Sonic above the water?
	bge.s	Obj01_OutWater	; if yes, branch

	[...]

	asr.w	x_vel(a0)
	asr.w	y_vel(a0)	; <-- memory operands can only be shifted one bit at a time
	asr.w	y_vel(a0)

	[...]

Since Sonic is under water this code is run. The game simulates the water's surface tension by halving Sonic's horizontal velocity and quartering his vertical velocity. Normally, this would just cause the water surface to break Sonic's fall, but since he's actually jumping, it reduces his jump strength considerably. During the next few frames Sonic slowly moves upward until he hits the water surface again. Now this code is run:

; loc_1A1FE:
Obj01_OutWater:
	bclr	#6,status(a0) ; unset underwater flag
	beq.s	return_1A18C ; if already above water, branch

	[...]

	asl	y_vel(a0)	; <--

	[...]

The significant part is where Sonic's vertical speed is doubled. This simulates Sonic no longer being subject to the water's resistance. Keeping in mind that previously Sonic's speed was quartered and now only doubled; that leaves Sonic with only half of his intended jump height, at most. By only allowing the speed decrease when Sonic is not moving up, this bug is fixed.

SCHG How-To Guide: Sonic the Hedgehog 2 (16-bit)
Fixing Bugs
Fix Demo Playback | Fix a Race Condition with Pattern Load Cues | Fix Super Sonic Bugs | Use Correct Height When Roll Jumping | Fix Jump Height Bug When Exiting Water | Fix Spin Dash Code and Add Spin Dash Speeds | Fix Screen Boundary Spin Dash Bug | Correct Drowning Bugs | Fix Camera Y Position for Tails | Fix Tails Subanimation Error | Fix Tails' Respawn Speeds | Fix Accidental Deletion of Scattered Rings | Fix Ring Timers | Fix Rexon Crash | Fix Monitor Collision Bug | Fix EHZ Deformation Bug | Correct CPZ Boss Attack Behavior | Fix Bug in ARZ Boss Arrow's Platform Behavior | Fix ARZ Boss Walking on Air Glitch | Fix ARZ Boss Sprite Behavior | Fix Multiple CNZ Boss Bugs | Fix HTZ Background Scrolling Mountains | Fix OOZ Launcher Speed Up Glitch | Fix DEZ Giant Mech Collision Glitch | Fix Boss Deconstruction Behavior | Fix Speed Bugs
Design Choices
Remove the Air Speed Cap | Disable Floor Collision While Dying | Modify Super Sonic Transformation Methods & Behavior | Enable/Disable Tails in Certain Levels | Collide with Water After Being Hurt | Retain Rings When Returning at a Star Post | Improve the Fade In\Fade Out Progression Routines | Fix Scattered Rings' Underwater Physics | Insert LZ Water Ripple Effect | Restore Lost CPZ Boss Feature | Prevent SCZ Tornado Spin Dash Death | Improve ObjectMove Subroutines | Port S3K Rings Manager | Port S3K Object Manager | Port S3K Priority Manager | Edit Level Order with ASM‎ | Alter Ring Requirements in Special Stages | Make Special Stage Characters Use Normal DPLCs | Speed Up Ring Loss Process | Add beta spindash to Sonic 2 | Change spike behaviour in Sonic 2
Adding Features
Create Insta-kill and High Jump Monitors | Create Clone and Special Stage Monitors | Port Knuckles
Sound Features
Port Sonic 1 Sound Driver | Port Sonic 2 Clone Driver | Port Sonic 3 Sound Driver | Expand the Music Index to Start at $00 (Sonic 2 Clone Driver Version)
Extending the Game
Extend the Level Index Past $10 | Extend the Level Select | Extend Water Tables | Add Extra Characters | Free Up 2 Universal SSTs