Results 1 to 6 of 6

Thread: Help with platform engine

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    4

    Help with platform engine

    Hello (first post). My name is DoctorN (but seemingly, that name was taken). I am working on a platform engine for my company. At this beginning moment, gameplay will be like an old case of super mairo. However, im at a stump, and can continue no farther unless I get some help. My character can not freely fall on his own, he has to jump in order to fall off the platform (if i jump off of the platform that is). He has to be perfectly alligned on top of the platform if he wishes to jump (so if i spawn him not on the platform, he doesnt fall, nor can he jump, just move left and right). And you'll see in the TimerY_Tick, theres a +113. that is because when he lands, thats the Y coordinate that he must land on (becuase 114 and below is the platform I guess). Can you look and adjust my code to those three things, or at least guide me in the right path? 1.wherever he spawns, hell fall if theres no ground underneath him. 2. there doesnt need to be a precise code where he has to land. thanks. heres the code
    Code:
    Public Class Form1
    
        Dim WithEvents TimerX As New Timer With {.Enabled = True, .Interval = 10}
        Dim WithEvents TimerY As New Timer With {.Enabled = True, .Interval = 10}
        Dim WithEvents TimerGravity As New Timer With {.Enabled = False, .Interval = 100}
    
    
        Dim PlayerWidth As Integer = 20
        Dim playerHeight As Integer = 20
    
        'This is your Player
        Dim playChar As PictureBox
        'This rectangle represente the window,if the player does not intersec with this rectangle
        'it means that it is about to move out of the window
        Dim theBox As Rectangle
        'This rectangle is your floor, if the player intersec with it, it means that it touch the floor
        Dim TheFloor As Rectangle
    
        'These are your movement variable (Higher the value faster your player goes
        Dim SpeedX As Integer = 0
        Dim SpeedY As Integer = 0
        'This is your gravity Variable, Higher the value, stronger is the gravity
        Dim Gravity As Integer = 1.5
        'This is your jump variable, higher the value, higher is the jump
        'We set the negative value of the speed toward the top of the form, this
        'simplify the math
        Dim JumpSpeed As Integer = -5.6
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
            Select Case e.KeyCode
                Case Keys.A ' Going toward the left
                    playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_walk_2_l.png")
                    SpeedX = -1.5
                Case Keys.D ' Going toward the right
                    playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_walk_2_r.png")
                    SpeedX = 1.5
                Case Keys.W ' Jump
                    'Create a test rectangle before jumping to see if the player touch the ground
                    Dim Test As Rectangle = New Rectangle(New Point(playChar.Location.X, playChar.Location.Y + 2), playChar.Size)
                    'If The test rectangle intersect with the floor, allow the jump
                    If Test.IntersectsWith(TheFloor) Then
                        'Perform the jump
                        SpeedY = JumpSpeed
                        'Start the gravite meter
                        TimerGravity.Enabled = True
                    End If
            End Select
        End Sub
    
        Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
            Select Case e.KeyCode
                Case Keys.A ' stop moving when the key is release
                    playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_stand_l.png")
                    SpeedX = 0
                Case Keys.D ' stop moving when the key is release
                    playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_stand_r.png")
                    SpeedX = 0
            End Select
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Set up the rectangle
            Me.Size = New Size(320, 240)
            theBox = New Rectangle(PlayerWidth, playerHeight, Me.ClientSize.Width - (PlayerWidth * 2), Me.ClientSize.Height - (playerHeight * 2))
            playChar = PlayModel
            TheFloor = New Rectangle(33, 140, 233, 156)
            Me.DoubleBuffered = True
        End Sub
    
        Private Sub TimerX_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerX.Tick
            'Create a test rectangle before moving the player to see if he is about to move out of the screen
            Dim Test As Rectangle = New Rectangle(New Point(playChar.Location.X + SpeedX, playChar.Location.Y), playChar.Size)
            ' If not moving out of the screen then move
            'NB If there is some obstacles, you will also test here if the Test rectangle intersect with the obstacle before moving
            If Test.IntersectsWith(theBox) Then
                playChar.Location = New Point(playChar.Location.X + SpeedX, playChar.Location.Y)
            End If
            'redraw the screen
            Me.Refresh()
        End Sub
    
        Private Sub TimerY_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerY.Tick
            playChar.Location = New Point(playChar.Location.X, playChar.Location.Y + SpeedY)
            ' if the player touches the floor
            If playChar.Bounds.IntersectsWith(TheFloor) Then
                ' stop the downward movement
                SpeedY = 0
                'replace the player 1 pixel above the floor
                playChar.Location = New Point(playChar.Location.X, levelGround.ClientSize.Height - playerHeight + 113)
                'stop the gravity timer
                TimerGravity.Enabled = False
            End If
            'redraw the screen
            Me.Refresh()
        End Sub
    
        Private Sub TimerGravity_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerGravity.Tick
            SpeedY += Gravity
        End Sub
    End Class

  2. #2

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    4

    Re: Help with platform engine

    ive updated the code very slightly but not enough to call it an improvement, please help.

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    4

    Re: Help with platform engine

    in case the code wasnt formatted properly here:
    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim WithEvents TimerX As New Timer With {.Enabled = True, .Interval = 10}
    4.     Dim WithEvents TimerY As New Timer With {.Enabled = True, .Interval = 10}
    5.     Dim WithEvents TimerGravity As New Timer With {.Enabled = False, .Interval = 100}
    6.  
    7.  
    8.     Dim PlayerWidth As Integer = 20
    9.     Dim playerHeight As Integer = 20
    10.  
    11.     'This is your Player
    12.     Dim playChar As PictureBox
    13.     'This rectangle represente the window,if the player does not intersec with this rectangle
    14.     'it means that it is about to move out of the window
    15.     Dim theBox As Rectangle
    16.     'This rectangle is your floor, if the player intersec with it, it means that it touch the floor
    17.     Dim TheFloor As Rectangle
    18.  
    19.     'These are your movement variable (Higher the value faster your player goes
    20.     Dim SpeedX As Integer = 0
    21.     Dim SpeedY As Integer = 0
    22.     'This is your gravity Variable, Higher the value, stronger is the gravity
    23.     Dim Gravity As Integer = 1.5
    24.     'This is your jump variable, higher the value, higher is the jump
    25.     'We set the negative value of the speed toward the top of the form, this
    26.     'simplify the math
    27.     Dim JumpSpeed As Integer = -5.6
    28.  
    29.     Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    30.         Select Case e.KeyCode
    31.             Case Keys.A ' Going toward the left
    32.                 playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_walk_2_l.png")
    33.                 SpeedX = -1.5
    34.             Case Keys.D ' Going toward the right
    35.                 playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_walk_2_r.png")
    36.                 SpeedX = 1.5
    37.             Case Keys.W ' Jump
    38.                 'Create a test rectangle before jumping to see if the player touch the ground
    39.                 Dim Test As Rectangle = New Rectangle(New Point(playChar.Location.X, playChar.Location.Y + 2), playChar.Size)
    40.                 'If The test rectangle intersect with the floor, allow the jump
    41.                 If Test.IntersectsWith(TheFloor) Then
    42.                     'Perform the jump
    43.                     SpeedY = JumpSpeed
    44.                     'Start the gravite meter
    45.                     TimerGravity.Enabled = True
    46.                 End If
    47.         End Select
    48.     End Sub
    49.  
    50.     Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
    51.         Select Case e.KeyCode
    52.             Case Keys.A ' stop moving when the key is release
    53.                 playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_stand_l.png")
    54.                 SpeedX = 0
    55.             Case Keys.D ' stop moving when the key is release
    56.                 playChar.Image = Image.FromFile("C:\Users\Nick Lopez\Documents\Visual Studio 2010\Projects\n_platform\n_platform\Resources\player_stand_r.png")
    57.                 SpeedX = 0
    58.         End Select
    59.     End Sub
    60.  
    61.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    62.         ' Set up the rectangle
    63.         Me.Size = New Size(320, 240)
    64.         theBox = New Rectangle(PlayerWidth, playerHeight, Me.ClientSize.Width - (PlayerWidth * 2), Me.ClientSize.Height - (playerHeight * 2))
    65.         playChar = PlayModel
    66.         TheFloor = New Rectangle(33, 140, 233, 156)
    67.         Me.DoubleBuffered = True
    68.     End Sub
    69.  
    70.     Private Sub TimerX_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerX.Tick
    71.         'Create a test rectangle before moving the player to see if he is about to move out of the screen
    72.         Dim Test As Rectangle = New Rectangle(New Point(playChar.Location.X + SpeedX, playChar.Location.Y), playChar.Size)
    73.         ' If not moving out of the screen then move
    74.         'NB If there is some obstacles, you will also test here if the Test rectangle intersect with the obstacle before moving
    75.         If Test.IntersectsWith(theBox) Then
    76.             playChar.Location = New Point(playChar.Location.X + SpeedX, playChar.Location.Y)
    77.         End If
    78.         'redraw the screen
    79.         Me.Refresh()
    80.     End Sub
    81.  
    82.     Private Sub TimerY_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerY.Tick
    83.         playChar.Location = New Point(playChar.Location.X, playChar.Location.Y + SpeedY)
    84.         ' if the player touches the floor
    85.         If playChar.Bounds.IntersectsWith(TheFloor) Then
    86.             ' stop the downward movement
    87.             SpeedY = 0
    88.             'replace the player 1 pixel above the floor
    89.             playChar.Location = New Point(playChar.Location.X, levelGround.ClientSize.Height - playerHeight + 113)
    90.             'stop the gravity timer
    91.             TimerGravity.Enabled = False
    92.         End If
    93.         'redraw the screen
    94.         Me.Refresh()
    95.     End Sub
    96.  
    97.     Private Sub TimerGravity_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles TimerGravity.Tick
    98.         SpeedY += Gravity
    99.     End Sub
    100. End Class

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Help with platform engine

    I haven't looked at your code (because I know I won't have time to completely understand any of it), but if your character can only fall if he is ON an obstacle that indicates that your logic is wrong.

    I haven't got much experience with games, but I would use this approach. I am assuming you've got some kind of game loop that runs every few milliseconds (at least maybe 10 times a second, preferably more). In that loop I would take the following actions:
    - Store (a copy of) the bounds of the character in a local variable
    - Decrease the Y value of the speed of the character by the amount of gravity (well, increase it, since positive Y is down)
    - Change the bounds of the character by his speed (bounds.X += speed.X, bounds.Y += speed.Y)
    - Check if the new bounds of the character are inside an obstable (likely; you'd loop through every obstacle, get it's bounds and check if the new bounds are inside these bounds, Rectangle.IntersectsWith or something)
    - If they are inside an obstacle, a collision occured, and the new bounds are invalid. Set the new bounds such that the character is standing exactly on the obstacle (bottom of character bounds = top of obstable bounds, maybe with a 1px boundary as long as they don't intersect anymore)
    - If they are not inside an obstacle, all is well, just continue
    - At the end, assign the new bounds to the character again.


    The reason for making a copy of the bounds, changing that copy, and checking for collisions on that copy, is simple. You could just change the character bounds immediately and check for collisions afterwards, but if a collision occurs you'd have to move him back up, and this can cause the character to become stuck in some occasions (he would be colliding, move him back, he's still colliding, then you're stuck). Maybe not in this case, I don't know, but it doesn't hurt to use the copy approach.


    As for jumping, when the jump key is pressed you'd first check if the character is standing on an obstacle (you may want to use a simple boolean property for this, which you set to False every iteration of the game loop, and only to True after a collision has occurred and you re-set his position to just above the colliding obstacle). If yes, you set his speed.Y to a certain value. Each game loop iteration this value will be decreased (but still positive) until it reaches zero (peak of jump) and then it goes negative (falling down again).

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    4

    Re: Help with platform engine

    can someone else help too?

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help with platform engine

    Moved To Games And Graphics Programming

Posting Permissions

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



Click Here to Expand Forum to Full Width