How to Move Your Game Object Like Lunar Lander Part 1/2

Lunar Lander

If you have read my previous tutorial How to jump a game character realistically without using physics engine in Corona SDK then you will see in this post we will use the same concept but will take it to the next level. If you haven’t read, it is not a problem you don’t have to.

Basic variables

Ok let’s start with the basic variables that we need to move our lunar lander.

  • Gravity is the force that moves our lunar lander to the surface.
  • verticalVelocity and horizontalVelocity will be used to move lunar lander vertically and horizontally.
  • verticalAcc and horizontalAcc are accelereations that will effect the vertical and horizontal velocities.
  • And finally i will limit the velocities with the maximum up,down,right and left values.

As you can see below i’ve given some default values to these variables but of course you can change them and control your lunar lander’s movement as you wish. Basic variables

Adding display objects

After defining basic variables, i’ve added a button bar at the bottom of the screen and added left, right and up buttons on it to control my lunar lander. I  did not use any images just basic rectangles. Adding display objects

Adding event listeners

In the last 4 lines i’ve added event listeners to the buttons and Runtime. Adding event listeners

Moving Lunar Lander

When i touch the upButton, moveUp function is called and it simply equals the verticalAcc to -0.02. It is a negative number because i will add this verticalAcc to verticalVelocity and verticalVelocity has to be negative because, i will add it to y position of lunarLander and you know if y positon of an object is getting smaller in corona SDK then that means my object is going up on the screen. Using the same logic above, when i touch left-right buttons moveLeft and moveRight functions are called and they equals the horizontalAcc to negative or positive 0.01. Later, i will add horizontalAcc to horizontalVelocity and add horizontalVelocity to x position of lunarLander to move it left or right. touch listeners You see the touch event listener functions only change the horizontal or vertical accelerations. The actual movement happens in enterFrame event. In the last line i’ve added enterFrame event listener to Runtime and used lunarLander object as listener.That’s why my lunarlander has an instance method with the name enterFrame and it will be called in every frame.Moving lunar lander Let me explain what happens in that method.

  • Lines 25,26 : I find the final values of vertical and horizontal velocities.While vertical velocity is effected by vertical acceleration and gravity, horizontal velocity is only effected by horizontal acceleration.
  • Lines 28-32 : I dont want my lunar lander to go very fast so i limit verticalVelocity with maximumUp and maximumDown values. I know it looks strange that maximumUp value is negative but maximumDown value is positive but you know we need negative verticalVelocity to go up.
  • Line 34 : Finally, my lunar lander is moving. After i’ve calculated verticalVelocity, i add it to the y position of lunar lander.
  • Lines 35-39 : I don’t want my lunar lander go below the button bar so put a control on y position. If y position is bigger than the top side of the button bar then, i equal vertical and horizontal velocities to zero and adjust y position to land my lunar lander just top side of the button bar.
  • Lines 41-45 : I limit the horizontalVelocity with maximumRight and maximumLeft.
  • Lines 47-52 : I add horizontalVelocity to x position of lunar lander to move it left or right. If lunar lander goes outside of screen from left then it will come inside from right or vice versa

And this is the result

. Sample gif

Ok it looks pretty good but needs something more. Our lunar lander has rockets and it is moving by using them. While rockets are active, we need some visuals or animations to make the movement seem more realistic and eye-catching.

Here is what i mean.

Sample gif

And i will explain how to do this in the second part of this tutorial. Please don’t hesitate to ask me question in comments.

One thought on “How to Move Your Game Object Like Lunar Lander Part 1/2

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s