1. #1

    Sonic game programming, how does it work?

    A while ago I made a Sonic game in very basic C++ using OpenGL. It lacked curves, etc and featured a new combat system for Sonic which turned out to be pretty awesome. Sadly I cannot get this code to work anymore as I wrote it 6 years ago in VS 97.

    I was thinking of starting again but I cannot figure out how they did the following:
    - made Sonic run up curves and down
    - made it so it was harder to run up hills at lower speed
    - loop the loops

    I'm wondering if anyone can help to explain generally how each of these things was achieved. I'm not asking for code or anything, I would just like to know the techniques. Even if you don't really know the exact techniques they used, a guess as to how they did it is still good!

  2. #2
    Utilizing physics. Basically determine movement location by using velocity. Very weak and unthought out pseudo-code:

    locX = 0
    --GameLoop--
    if(isGoingUpHill)
    velocity = 5
    else if(isGoingDownHill)
    velocity = 15
    else if(isFalling)
    velocity = 12
    else //on regular ground
    velocity = 10
    locX += velocity
    --End GameLoop--

    For looping and going up down, you would just add in some form of bounding boxes to control what is solid and modify the code to have a locY and velocityY

    Here is the basic movement code I used for creating a clone of the original arcade Donkey Kong. It takes into consideration the location of the ladders to determine if Plumberman is climbing or jumping, and checks to see if the ladder he is climbing is broken or complete. This code is ~3 years old, and I was a novice when I wrote it, but it might give some ideas. Note this is VB.NET, not C++

    Code:
    Public Class Character : Inherits ControlSprite
    
        Private _upKey As Keys
        Private _downKey As Keys
        Private _leftKey As Keys
        Private _rightKey As Keys
        Private _isUpKeyPressed As Boolean
        Private _isDownKeyPressed As Boolean
        Private _isLeftKeyPressed As Boolean
        Private _isRightKeyPressed As Boolean
    
        Private ladders(18) As Ladder
        Private platforms(140) As Platform
        Private row = 7
    
        Private _isJumping As Boolean
        Private _initY As Single = 0
    
        Public MaxPositionX As Single
        Public MinPositionX As Single
        Public MaxPositionY As Single
        Public MinPositionY As Single
    
        Public Const Width As Integer = 10
        Public Const Height As Integer = 10
        Private Shared Color As Color = Color.DarkSlateBlue
        Private Const _speed As Double = 40
    
        Public Sub New(ByVal control As Control, ByVal maxPosX As Single, ByVal maxPosY As Single)
            MyBase.New(control)
            control.BackgroundImage = My.Resources.Mario
            MaxPositionX = maxPosX
            MaxPositionY = maxPosY
            MinPositionX = 0
            MinPositionY = 0
    
            Location.X = 10
            Location.Y = maxPosY
    
            control.BackColor = Color
            control.Width = Width
            control.Height = Height
    
            setHeightWidth()
    
            _upKey = Keys.Up
            _downKey = Keys.Down
            _leftKey = Keys.Left
            _rightKey = Keys.Right
    
        End Sub
    
        Public Overrides Sub Update(ByVal gameTime As Double, ByVal elapsedTime As Double)
            processMove()
    
            MyBase.Update(gameTime, elapsedTime)
    
            If Location.Y < MinPositionY Then
                Location.Y = MinPositionY
            End If
            If Location.Y > MaxPositionY - Height Then
                Location.Y = MaxPositionY - Height
            End If
            If Location.X < MinPositionX Then
                Location.X = MinPositionX
            End If
            If Location.X > MaxPositionX - Width Then
                Location.X = MaxPositionX - Width
            End If
        End Sub
    
        Public Sub setLadders(ByRef ladders() As Ladder)
            Me.ladders = ladders
        End Sub
    
        Public Function hasWon() As Boolean
            If Location.Y < 30 And Location.X < MaxPositionX / 2 Then
                Return True
            End If
            Return False
        End Function
    
        Private Sub processMove()
            Dim workingVelocityY As Double = 0.0
            Dim workingVelocityX As Double = 0.0
    
            If Not collision() And Not _isJumping And Not Location.Y < onPlatform() Then
                workingVelocityY += _speed / 2
            End If
    
            If _isJumping Then
                'jumpDown()
                If Location.Y >= _initY Then
                    _isJumping = False
                Else
                    workingVelocityY += _speed / 4
                End If
            End If
    
            If _isUpKeyPressed Then
                If collision() Then
                    workingVelocityY -= _speed
                ElseIf Not _isJumping Then
                    jumpUp()
                End If
            End If
            If _isDownKeyPressed Then
                If aboveLadder() Then
                    workingVelocityY += _speed
                End If
            End If
            If _isLeftKeyPressed Then
                workingVelocityX += -_speed
            End If
            If _isRightKeyPressed Then
                workingVelocityX += _speed
            End If
            Velocity.Y = CSng(workingVelocityY)
            Velocity.X = CSng(workingVelocityX)
        End Sub
    
        Private Sub jumpUp()
            _initY = CSng(Location.Y)
            Location.Y = Location.Y - 8
            _isJumping = True
        End Sub
    
        Private Sub jumpDown()
            If Location.Y >= _initY Then
                _isJumping = False
            Else
                Velocity.Y = CSng(_speed)
            End If
        End Sub
    
        Private Function collision() As Boolean
            For i = 0 To 17
                If Not (Location.X > ladders(i).Location.X + ladders(i).Size.Width OrElse Location.X + Size.Width < ladders(i).Location.X OrElse Location.Y > ladders(i).Location.Y + ladders(i).Size.Height OrElse Location.Y + Size.Height < ladders(i).Location.Y) Then
                    ladders(i).Draw()
                    If Not (ladders(i).isBroken) Then
                        _isJumping = False
                        Return True
                    End If
                End If
            Next
            Return False
        End Function
    
        Private Function aboveLadder() As Boolean
            For i = 0 To 17
                If Not (Location.X > ladders(i).Location.X + ladders(i).Size.Width OrElse Location.X + Size.Width < ladders(i).Location.X) Then
                    If Not (Location.Y + Height + 2 > ladders(i).Location.Y + ladders(i).Size.Height OrElse Location.Y + Height + 2 < ladders(i).Location.Y) Then
                        ladders(i).Draw()
                        Return True
                    End If
                End If
            Next
            Return False
        End Function
    
        Public Sub KeyDown(ByVal keys As Keys)
    
            If keys = _upKey Then
                _isUpKeyPressed = True
            End If
            If keys = _downKey Then
                _isDownKeyPressed = True
            End If
            If keys = _leftKey Then
                _isLeftKeyPressed = True
            End If
            If keys = _rightKey Then
                _isRightKeyPressed = True
            End If
        End Sub
    
        Public Sub KeyUp(ByVal keys As Keys)
    
            If keys = _upKey Then
                _isUpKeyPressed = False
            End If
            If keys = _downKey Then
                _isDownKeyPressed = False
            End If
            If keys = _leftKey Then
                _isLeftKeyPressed = False
            End If
            If keys = _rightKey Then
                _isRightKeyPressed = False
            End If
        End Sub
    End Class
    Last edited by Arcilux; 2011-11-09 at 04:44 AM.
    Author of Instance Profit Tracker
    Find out how much gold you earn soloing raids and dungeons

    Curse | GitHub
    WowInterface

  3. #3
    Thanks for the insight and code was interesting.

    I've never really thought about trying to implement physics. I guess most of the equations are fairly simple especially for a 2D world. Think i'll give it a shot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •