First of all, if you’ve played the original Mario before, Mario will eat mushrooms to get bigger, where there is only a small Mario on the wall and the ground. If you eat mushrooms to get bigger, it won’t get stuck and can be squeezed out. As shown in figure

If you use the physics feature that comes with the GoDot engine, when you change the collision volume, you will find that Mario will get stuck in the wall, so this feature has to be implemented by itself. My implementation is relatively simple and complicated, but I did not expect it for the moment, I used Rect2 as the collision box. Rect2 is added to each object requiring collision detection, and Intersects will be called to determine whether there is a collision. These are all functions provided by GoDOT. Here are the basic attributes

Basic class # all the objects in the game var debug = false var the rect = Rect2 (Vector2. ZERO, Vector2. ZERO) # var visible = true # whether visible var isCollide = true if # collision Constants. Empty # type var gravity=0 # gravity var xVel=0 # X-axis velocity var yVel=0 # Y-axis velocity var offsetX=0 var offsetY=0 var constants CollisionShow =false # Show whether a collision occurs when testingCopy the code

The movement of objects is mainly Mario and enemies, and Mario’s movement is accelerated, which is divided into the direction of x axis and Y axis. The y axis is gravity, and the speed of the Y axis needs to be set to maximum delivery. So to change the speed is to change the value of the speed every frame, if the value is constantly changing it is variable speed, if it is constant it is constant speed, and here is the code for Mario to walk, which is to change the speed when pressing the button, xVel is negative on the left and positive on the right.

func walk(delta): if xVel>0 || xVel<0: ani.speed_scale=1+abs(xVel)/constants.marioAniSpeed if Input.is_action_pressed("ui_action"): acceleration=constants.runAcceleration maxXVel=constants.marioRunMaxSpeed if fire&&allowShoot: shootFireball(false) allowShoot=false else: Acceleration = the acceleration maxXVel = the marioWalkMaxSpeed allowShoot = true if # jump Input.is_action_pressed("ui_jump"): yVel=-constants.marioJumpSpeed gravity=constants.marioJumpGravity status=constants.jump playJumpSound() # print('walk jump') return if Input.is_action_pressed("ui_down") &&big: startCrouch() return elif isCrouch and big: Rect =Rect2(Vector2(-11,-30),Vector2(22,60)) position. Y -=14 ani.position Input.is_action_pressed(" UI_left "): if xVel>0: # Acceleration ("slide") =constants. SlideFriction else: acceleration. acceleration=constants.acceleration dir=constants.left animation('walk') if xVel>-maxXVel: xVel-=acceleration*delta elif Input.is_action_pressed("ui_right"): if xVel<0: animation("slide") acceleration=constants.slideFriction else: dir=constants.right acceleration=constants.acceleration animation('walk') if xVel<maxXVel: xVel+=acceleration*delta else: if dir==constants.right: if xVel>0: xVel-=acceleration*delta animation("walk") else: ani.speed_scale=1 status=constants.stand else: if xVel<0: xVel+=acceleration*delta animation("walk") else: ani.speed_scale=1 status=constants.stand position.x+=xVel*delta # position.y+=yVel*delta if ! isOnFloor: ani.stop() status=constants.fall passCopy the code

The movement of all objects is basically like this, which can be found in the code. So it is heavy set after the collision, it is necessary to readjust the location of the some objects, this thing is more trouble, I refer to an article, developer.ibm.com/technologie… A solution was reluctantly found, but it was still incomplete. When you’re colliding, you have to decide whether it’s going to be left or right or up or down, so that you can decide whether you’re going to adjust x or y.

Take a look at the update function in the map scenario. Only after the location is adjusted can the physical world function properly. Other specific ideas are added. Resources: developer.ibm.com/technologie…