Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411283 Posts in 69325 Topics- by 58380 Members - Latest Member: bob1029

March 29, 2024, 07:39:03 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsCommunityTownhallForum IssuesArchived subforums (read only)TutorialsActually Simple: Velocity And Gravity
Pages: [1]
Print
Author Topic: Actually Simple: Velocity And Gravity  (Read 1527 times)
Hiarc_Games
Level 0
*


Hiarc Games head dev


View Profile WWW
« on: March 23, 2012, 12:10:45 AM »

NOTE: This tutorial is geared more towards the New Game Developers or experienced programmers who want to make there own movement systems.


The Game Loop:
The game loop is the process that almost every game is based around.
Initialize
this is the first part of the loop it initializes all the variables
Update
This part of the loop updates the variables that you may have declared or Initialized it is also where all the mouse and keyboard handlers are
Render
This Part of the loop is where all the textures are handed to the rasterizer and then drawn on your screen.

After Render is finished it usually calls the update function again. This process is similar to recursion, you can see what the loop looks like below:



Velocity:
Velocity is essentially the speed in any given direction e.g. in a game if your Horizontal Velocity (X Velocity) is 15. you are going 15 units to the Right on every update.

Sudo Code:
Code:
XVelocity  = 0

 [UPDATE] this is the update function that is called every frame


if( the Right Arrow Key is down)
{
      XVelocity  = XVelocity + 1;
}

if( the Left Arrow Key is  down)
{
     XVelocity  = XVelocity - 1;
}

your character's x position = your character's x position + XVelocity

[END UPDATE]

The Code Above will make your character  slide back and forth on the screen, the visual effect created is similar to ice in a side-scroller


Gravity:

you may be asking "Why are you doing Velocity And Gravity?". Well this is because Simple Gravity is achieved with Vertical Velocity, and its quite simple.

In the shortest Explanation: if the character does not collide with anything then you want his Vertical Velocity to change.

Sudo Code:
Code:
YVelocity  = 0

 [UPDATE] this is the update function that is called every frame

if(your character is not touching any other object)
{
    YVelocity = YVelocity - 1
}

your character's y position = your character's y position + YVelocity

[END UPDATE]

Thats it, now you have Gravity in your game.

you can even make your player jump by adding another if statement:

Code:
if(you press the jump button)
{
    YVelocity = 15
}

this changes your character velocity to a positive value, making him jump and come back down.


I Hope Everyone Liked this Topic , feel free to comment and message me for ideas for future tutorials.
Thanks,
indieGivaway
http://hiarcgames.com/
« Last Edit: March 23, 2012, 01:30:54 AM by indieGivaway » Logged

<super tag>non super value</super tag>
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic