|
Post by n00b on Nov 13, 2019 14:58:32 GMT -6
I have implemented jumping physics differently in each game I made but it is always interesting to see how other programmers solve problems. But sometimes you just have to find a way that works best for you.
|
|
|
Post by tbird on Nov 13, 2019 20:34:11 GMT -6
Wise words, I agree with n00b it's an interesting problem, I have only tried it once and I have no desire to try a different way lol. We have faith in you johnno56 you will have something good.
|
|
|
Post by tbird on Nov 16, 2019 20:38:43 GMT -6
|
|
|
Post by johnno56 on Nov 16, 2019 21:52:19 GMT -6
In a nutshell... It isn't...
I am more familiar with a "map-based" player moving across a 'fixed' "map-based" scene. Now the "map" moves and the only movement by the player is vertically. In a 'fixed' game I can test if the player map reference is about to 'contact' a scene map element by checking to see if the next map element is empty or not then modify the 'x' or 'y' velocity accordingly. Now that the player is a sprite, and not part of the 'map', collision is a little more complicated... well.. a LOT for me... I am quickly running out of patience because of my inability to solve, what should be - a simple - problem.
I'm going to 'go back to formula' and start again using simple geometric shapes and no viewport... I have a sneaking suspicion that getPixel(), being used for collision detection when using viewports, will need to include an offset to allow for viewport coordinates... I could be wrong...
J
|
|
|
Post by n00b on Nov 17, 2019 10:49:35 GMT -6
Hey Johnno, you don't need GetPixel(). I modified the code I gave you last time to apply a simple jump. You can use this as a start to implement a better jump in your game. Just replace the section I changed in the last bit of code I posted with this and then you will be able to jump with space bar.
'----------------CHANGES FROM ORIGINAL START HERE ----------------------------------------------------------------------------------
'---PLAYER COLLISION Player_World_X = 20 'This will be the x-coordinate of the player in the level Player_World_Y = 192 'This will be the y-coordinate of the player in the level
Function Check_Player_Collision(new_x, new_y) 'This function will take the player position and check if it collided with a pipe player_width = 16 player_height = 16 'The tile position of the top left corner of the player player_tile_x1 = new_x / tile_width_inPixels player_tile_y1 = new_y / tile_height_inPixels 'The tile position of the top right corner of the player player_tile_x2 = (new_x + player_width) / tile_width_inPixels player_tile_y2 = new_y / tile_height_inPixels 'The tile position of the bottom left corner of the player player_tile_x3 = new_x / tile_width_inPixels player_tile_y3 = (new_y + player_height) / tile_height_inPixels 'The tile position of the bottom right corner of the player player_tile_x4 = (new_x + player_width) / tile_width_inPixels player_tile_y4 = (new_y + player_height) / tile_height_inPixels 'The pipes are currently these characters: (, ), [, ] 'We will just check if the tile at the player tile position is any of these Select Case mario_map$[player_tile_x1, player_tile_y1] Case "(", ")", "[", "]" Return True End Select Select Case mario_map$[player_tile_x2, player_tile_y2] Case "(", ")", "[", "]" Return True End Select Select Case mario_map$[player_tile_x3, player_tile_y3] Case "(", ")", "[", "]", "1" Return True Case "1" 'The Ground Tile End Select Select Case mario_map$[player_tile_x4, player_tile_y4] Case "(", ")", "[", "]", "1" Return True End Select Return False End Function
'Variable for the force applied to character during jump 'This variable will be decreased by gravity until it is less than or equal to 0 Jump_Force = 0 Jump_Speed = 5 Gravity = 3
sub apply_jump(jump_ready) If Jump_Force > 0 Then Player_World_Y = Player_World_Y - Jump_Speed Jump_Force = Jump_Force - Jump_Speed ElseIf Key(K_SPACE) and jump_ready Then Jump_Force = 100 End If end sub
sub render_player() '---------- Apply Gravity ------------------------------------------------ gravity_applied = Gravity For i = Gravity to 0 Step -1 If Check_Player_Collision(Player_World_X, Player_World_Y+gravity_applied) Then gravity_applied = gravity_applied - 1 Else Exit For End If Next Player_World_Y = Player_World_Y + gravity_applied '--------------------------------------------------------------------------- ready = False If gravity_applied = 0 Then ready = True End If apply_jump(ready)
if key(k_right) and (camera_x_inPixels + 1) < (map_width_inPixels - viewport_width_inPixels) - 16 then 'Check if the Player_World_X + camera_speed results in a collision If Check_Player_Collision(Player_World_X + camera_speed, Player_World_Y) Then 'There was a collision so we won't move the camera Else camera_x_inPixels = camera_x_inPixels + camera_speed Player_World_X = Player_World_X + camera_speed 'Player_World_Y = Player_World_Y + gravity End If player = player_right elseif key(k_left) and camera_x_inPixels > 0 then 'Check if the Player_World_X - camera_speed results in a collision If Check_Player_Collision(Player_World_X - camera_speed, Player_World_Y) Then 'There was a collision so we won't move the camera Else camera_x_inPixels = camera_x_inPixels - camera_speed Player_World_X = Player_World_X - camera_speed 'Player_World_Y = Player_World_Y - gravity End If player = player_left elseif key(k_up) then player = player_up elseif key(k_down) then player = player_down elseif not key(k_left) and not key(k_right) and not key(k_up) and not key(k_down) then player = player_stand end if '----------- These variables are pulled directly from the render_map function ----------------------- start_tile_x = int(camera_x_inPixels / tile_width_inPixels) start_tile_y = int(camera_y_inPixels / tile_height_inPixels)
start_tile_offset_x = camera_x_inPixels mod tile_width_inPixels start_tile_offset_y = camera_y_inPixels mod tile_height_inPixels '---------------------------------------------------------------------------------------------------- 'Calculating player_tile_x and player_tile_y based off what the Player_World_X and Player_World_Y is player_tile_x = Player_World_X / tile_width_inPixels player_tile_y = Player_World_Y / tile_height_inPixels 'This is pulled directly from the for loop in the render_map function 'x and y are replaced with the player_tile_x and player_tile_y screen_x = (player_tile_x - start_tile_x) * tile_width_inPixels - start_tile_offset_x screen_y = (player_tile_y - start_tile_y) * tile_height_inPixels - start_tile_offset_y DrawImage(player, screen_x, screen_y)
' if key(k_down) and (camera_y_inPixels + 2) < (map_height_inPixels - viewport_height_inPixels) then ' camera_y_inPixels = camera_y_inPixels + 2 ' elseif key(k_up) and camera_y_inPixels >= 0 then ' camera_y_inPixels = camera_y_inPixels - 2 ' end if end sub
'----------------CHANGES END HERE-----------------------------------------------------------------------------------
|
|
|
Post by johnno56 on Nov 18, 2019 13:59:58 GMT -6
Wow! I would not have thought of jumping like that! You have dug me out of a hole again... Thank you SO much. I had actually prepared this response yesterday but forgot to actually post it... Anyway. The player can now jump! Cool. I have made some slight modifications... Increased the 'jump_speed' from 5 to 10. The player can now clear the pipes and reach the lowest platforms. Added the platform and hill 'blocks' to the collision-able characters. Changed initial 'Player_World_X' from 20 to 64. Now the player can 'reach' the end 'flag'. There is one 'side-effect' caused by the modifications. The player, when jumping from beneath a reachable platform, can actually 'pass through' the platform before 'landing' on it. Looking into that one... Here is the game as it stands at the moment. Player movement (and now jumping... wink, wink...) only.... mario7.zip (3.08 KB) I am looking into removing the 'non-solid' images (clouds, hills and shrubs) from the "map" and have them scroll 'behind' the player - like parallax scrolling Again. A 'huge' thank you for the jump routine... J ps: Please ignore the on-screen text and square - debugging - lol
|
|
|
Post by n00b on Nov 18, 2019 18:36:54 GMT -6
The jumping seems to be a little to fast now. Also you will probably want to adjust the jumping force and jumping speed based on how fast your sprite is going to simulate jumping physics more inline with mario. It is a good start though.
|
|
|
Post by johnno56 on Nov 18, 2019 21:10:41 GMT -6
I agree... the jumping is a little bit "jack-rabbit-ish" but, as you said, its a start. I have a sneaking suspicion that this game's coding could be quite long... I may have to consider "including" modules... but that's getting a bit ahead of things... lol Right then, to work on a "smoother jump"
|
|