Saturday 5 May 2012

Swift slope movement in platform games...

Introduction:

This is the first useful tutorial in the series of Jumbo Platform Tutorial.. Evolve from a noob to pro .
I have selected one of the topics marked Very Advanced to prove that this blog isn't just all about bragging,
So, let's start the real thingy ...

What is this all about and why is it marked 'very advanced'?

 

Ok, let's start with a image...

A screenshot from my example game at Game design Practical .. Part1


See those green shining things - yup, those are slopes ... you got it right...
Actually, it shows two types of slopes to be technical...

1. Steps (marked 1 in violet)


[ Many would suggest that the idea of steps and slopes are not identical in terms of physics ... 
That is undoubtedly true..
But when you look at a game, they both serve the same purpose ..
 Move vertically and horizontally at the same time...

 So, in terms of our game world, both can be handled similarly even though slopes are not continuous..]

2. Inclined planes(marked 2 in yellow)


[Slopes, as you know them...]

 [Just to add, straight and sloped surfaces are not distinguished in my approach..
 So, plane surface also fall in this category...]

Clearly, you knew about slopes, but I just revised them to make sure we are on the same page ....

Now, about being hard and marked 'Very advanced' .....

First of all, its better to know how I mark the hardness level of every tutorial..
The ones marked in Red are those which haunted me for about a week
and I spend hours achieving those results with little or no success at all.... 


So, these are like the special ones for me....

One of them is this one...


 Though I believe that it is quite possible for others to achieve this results within hours and with a different approach, I can guarantee can you one thing...


All these approaches are self-thought, original
and hence it is highly probable that it will never match with any other approach of the same problem you have ever known ...

So, it is suggested that you go through the Theoretical Approach section even
 if you already know how to solve this problem ...

Theoretical Approach



It is quite hard to explain it without this edited version of the previous image ..

Note: All the X-axis and Y-axis referred here considers the image displayed above to be in the first quadrant.. This is not similar to the representation of  x-position and y-position in Gamemaker where every object is considered to be in the 4th quadrant..

So, now my approach is:
For. eg. when right key is pressed:
Check the height of the slope in the next x-position you want the player to move, when the motion is possible ...

The checking should be done in this order ->

Start checking from a y-position somewhat below the current y-position of the player whether it is collision-free when the player sprite is placed at that point.

(this 'somewhat' value should be about 5 times the height of the slope sprite)

and continue checking vertically upright (for eg. bottom to top along the blue line in this pic)

until you are equally 'somewhat' above the y-position of the player...
If any check returns true ,

instantly snap the player to that position

and stop the loop..

I have tried to put up a pseudo-code representation here with the concept of maximum slope:


1.Max_Slope=Define the maximum slope that  the player will be able to move up along (0 to 90 degrees)
2.Max_Hor=Define the number of horizontal units that the player moves in each step (Usually, 5 is a good value in GM)
3.Height=Using any loop find the height  in next adjoining area
 (specifically the height of the (x+Max_Hor) position where it is collision-free when a player sprite is placed at that position [where x is the x-position of the player]...
 the place_meeting function of Gamemaker can be used)
(It can be negative or positive)
4. Find  Slope_obtained=Height/Max_Hor
5. If Slope_obtained<Max_Slope, move the player to (Max_Hor, Height) relative to the player's current position...

Example game:

I've avoided the maximum slope concept in the example game to keep it simple...
Controls: Use right and left arrow keys for movement..

If it appears a bit complicated, read it again slowly and try to understand it...


If still some of the discussions appear unclear to you, feel free to comment... I'm there to help you...


 


No comments:

Post a Comment