Hello Guest, please login or register.
Did you miss your activation email?
Login with username, password and session length.

Pages: [1]   Go Down

Author Topic: Problems with platform-engine  (Read 2171 times)

0 Members and 1 Guest are viewing this topic.

Fox

Turnbeutelvergesser since 1988.
Problems with platform-engine
« on: February 06, 2011, 07:03:06 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Female
  • Posts: 4062
This is probably rather basic and I could just use someone else's engine, but I want to make my own one that I understand fully.

I'm creating a platform-engine using aspects like place_free and gravity. The problems I encounter are as follows:

1.) The player, when reaching a solid block that he can stand on, slows down before touching the block.
2.) When touching a block from the side when in mid-air, the player stops and "sticks" to the wall. If jumping and moving is done right, the player is catapulted into the air.
3.) After the latter of the previously mentioned problem happens, the player, when no key is pressed at all, moves slowly towards the right the entire time. This problem is completely inexplicable to me.

Here are the relevant codes:

Code: Text
  1. //Jumping 2
  2.  
  3. if place_free(x,y+1){gravity=1.5}else{gravity=0}
  4. if keyboard_check(vk_space) && place_free(x,y-1) && place_free(x,y+1)=0 vspeed-=20
  5.  
  6. //Left and Right
  7.  
  8. if keyboard_check(ord('D')) && hspeed>10=false hspeed+=1
  9. if keyboard_check(ord('D'))=0 and hspeed>0 hspeed-=1
  10.  
  11. if keyboard_check(ord(&#39;A&#39;)) && hspeed<-10=false hspeed-=1
  12. if keyboard_check(ord(&#39;A&#39;))=0 and hspeed<0 hspeed+=1
  13.  
  14.  
  15. //Shoot!
  16. if mouse_check_button_pressed(mb_left) instance_create(x+16,y+16,obj_bullet)

Code: Text
  1. if place_free(x,y+vspeed)=false{vspeed=0 move_contact_solid(270,2)}

obj_block is solid, obj_player isn't.

I hope you can help me out. Thanks for reading and trying in advance!

Logged
  • Me on deviantART
Re: Problems with platform-engine
« Reply #1 on: February 06, 2011, 07:19:43 pm »
  • *
  • Reputation: +12/-2
  • Offline Offline
  • Gender: Male
  • Posts: 4849
First I would recommend some kind of state, whethere it be complex or just a simple bool.

jumping = false
falling = false
etc.(or could be equal to 0 or 1)
 if both jumping and falling are false then nothing happens with vspeed(I use my own variables such as jumppower and grav).

If place_meeting(x,y,obj_solid) && keyboard_check(vk_space) && (jumping = false && falling = false)
{
     jumping = true;
}
else if !place_meeting(x,y+vspeed(or grav as I use),obj_solid) && (falling = false  && jumping = false)
{
     falling = true;
}


Now then You have jumping and falling

if jumping = true && jumppower > 0
{
      y = y - jumppower;
      jumppower = jumppower - (whatever.  smaller the number, higher jump/less feeling of gravity);

}
else if jumping = true && keyboard_check_released(vk_space)
{
      jumppower = 0;
      jumping = false;
}

if falling = true && grav < 4(you dont want the player to continue to gain speed)
{
       y = y + grav;
        grav = grav + (whatever, high the amount, greater feel of gravity);
}


Something like that.  i am too tired to try it again in GM. 
Psuedo code up there should give you an idea of how I experiment with platforming.


Dont forget to reset jumppower and grav after jumping = false or falling = false
Logged
  • Super Fan Gamers!

Fox

Turnbeutelvergesser since 1988.
Re: Problems with platform-engine
« Reply #2 on: February 06, 2011, 07:33:46 pm »
  • *
  • Reputation: +0/-0
  • Offline Offline
  • Gender: Female
  • Posts: 4062
First I would recommend some kind of state, whethere it be complex or just a simple bool.

jumping = false
falling = false
etc.(or could be equal to 0 or 1)
 if both jumping and falling are false then nothing happens with vspeed(I use my own variables such as jumppower and grav).

If place_meeting(x,y,obj_solid) && keyboard_check(vk_space) && (jumping = false && falling = false)
{
     jumping = true;
}
else if !place_meeting(x,y+vspeed(or grav as I use),obj_solid) && (falling = false  && jumping = false)
{
     falling = true;
}


Now then You have jumping and falling

if jumping = true && jumppower > 0
{
      y = y - jumppower;
      jumppower = jumppower - (whatever.  smaller the number, higher jump/less feeling of gravity);

}
else if jumping = true && keyboard_check_released(vk_space)
{
      jumppower = 0;
      jumping = false;
}

if falling = true && grav < 4(you dont want the player to continue to gain speed)
{
       y = y + grav;
        grav = grav + (whatever, high the amount, greater feel of gravity);
}


Something like that.  i am too tired to try it again in GM.  
Psuedo code up there should give you an idea of how I experiment with platforming.


Dont forget to reset jumppower and grav after jumping = false or falling = false

Thank you! I gave it a read and I think I get the idea. Gotta find that I can implement it well, but I think it'll work out. Thank you for your help.

« Last Edit: February 06, 2011, 08:19:06 pm by Fox »
Logged
  • Me on deviantART
Re: Problems with platform-engine
« Reply #3 on: February 07, 2011, 09:20:05 pm »
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 3725
First, I have to say this. I mean no offense, but your coding convention is horrible. I know it is all allowed in the GML language, but GML tries to combine several different syntaxes which makes it a very ugly language. I'll give you this advice to make your code more readable in the future (and because you have a desire to switch to C++ one day).

- First of all use only one set of logical operators. Either use the set and, or and etc. or use &&, ||, etc. My preference and the convention that is used in Java and the C family is && and ||.

- Second do not use the assignment operator as the equality operator. Even if GML allows it, use '=' for assignments and '==' for equality.

- When using if-statements it is good practice to use '(' and ')' to indicate the start and the end of the boolean expression that is evaluated by the if.

- When using two consecutive if statements without ant other kind of statements in between and both expresionscontain an element that make both expresions mutually exclusive. Then it is better that you use if ... else if ...

- This is not necessary wrong, but any boolean variable or expression that you want to have passed when it is false is often not changed by comparing it to the value false, but by using the negation operator '!'. For example: 'place_free(x,y+1)=0' -> '!place_free(x,y+1)'

- Know what your expresions do. For example: 'hspeed>10=false' has a redundant part, which could better have been written as 'hspeed <= 10'. It does the same.


Okay, now onto your problem.
1.) The player, when reaching a solid block that he can stand on, slows down before touching the block.

...

Code: Text
  1. if place_free(x,y+vspeed)=false{vspeed=0 move_contact_solid(270,2)}
The problem is probably with the part of code indicated. For falling you use the gravity variable, while for jumping you use a negative vspeed. And my guess is that gamemaker has a cumulative of the gravity on vspeed, because when you jump up you will go down at one point and that is nowhere to be found in your code. Thus when going down your vspeed probably increases to a higher value than 2. When a collision event occurs with a solid GM puts your object back with to its starting position of that event. In effect that means that y - vspeed is done. Thus your check of 'place_free(x,y+vspeed)=false' will always hold true, because that is what generated the collision event.

In this collision event you set the vspeed back to 0 and move your object with a maximum of 2 pixels down, which is smaller than the vspeed was. after that the gravity sets in starting with 1.5 in the first step for vspeed and 3 in the next. Until another collision is generated and the vspeed is once again reset. Your object is moved down with a maximum of 2 and the vspeed can build up again in the following steps. At least until the move of 2 pixels down actually moves the object to the collision point.

2.) When touching a block from the side when in mid-air, the player stops and "sticks" to the wall. If jumping and moving is done right, the player is catapulted into the air.

...

Code: [Select]
if keyboard_check(vk_space) && place_free(x,y-1) && place_free(x,y+1)=0 vspeed-=20
In the indicated code you do '-=' which is a cumulative operator and can be written as x = x - a. Thus once you move your object in such a manner that he jumps, the vspeed is probably somewhere in the hundreds. This is probably solved by using the '=' assignment operator instead of the cumulative operator.

3.) After the latter of the previously mentioned problem happens, the player, when no key is pressed at all, moves slowly towards the right the entire time. This problem is completely inexplicable to me.
I would guess something with the hspeed goes wrong but it is weird as it should not happen. I don't know how to solve it.



I have to agree with TFS in that you probably use states to separate code. However having a list of boolean variables is not really a state variable but characteristics of a state. The purpose of a state is that with 1 variable you can indicate a lot of characteristics. My suggestion would be a number or a string to indicate the state. Or you use a script identifier and the script contains state dependent code and the object state independent code.

Another things is not to use the 'place_free' function, because that takes all the objects into account. Thus also not solid ones. In case of Super Mario you would be able to stand on a mushroom and a coin. Just like in TFS example you are better of using position_meeting and place_meeting. Or if you want the id of the object instance_position and instance_place.

And last, when you have to check whether a certain key is pressed more than once, you can better check it, assign it to a variable and just use the variable for the rest.
Logged
Re: Problems with platform-engine
« Reply #4 on: February 07, 2011, 11:46:22 pm »
  • *
  • Reputation: +8/-0
  • Offline Offline
  • Gender: Male
  • Posts: 6604
In terms of working with gravity and place_free, well I always thought it was bad convention to use place_free in that it checks all instances even if their objects were not meant to in any way be considered to interact with the player in a collision sort of way. I might be wrong on that as I am not sure how Game Maker does the checking, like if it goes through only non-solid instances OR if it goes through all instances checking to see if they are non-solid.

Additionally though, using place_free is wasteful in terms of gravity because it isn't necessary at all to check the whole mask collision region when the collision will always be at the bottom of the character. You only really have to check the bottom of the collision mask when the character is falling down, and then left/right sides when it is moving left or right.
Logged

Mamoruanime

@Mamoruanime
Re: Problems with platform-engine
« Reply #5 on: February 08, 2011, 08:19:40 am »
  • ^Not actually me.
  • *
  • Reputation: +9/-0
  • Offline Offline
  • Gender: Male
  • Posts: 9786
http://en.wikipedia.org/wiki/Finite-state_machine

^Does wonders.

As Niek said; having a bunch of variables for each action (IE "jumping = true", "falling = true") is not right for a finite state machine. The idea is that it gives you a level of control when your object's state is defined in a single variable. Being able to define *which new state* your object can go to from the state it's in rules out a lot of errors down the road. If you have a variable for each state however, you'll run into issues where you have a list of "state variables" set to true, and no indication of the source. You would be looking at a jumbled mess with no clue how you ran into the problem :P

One issue people have typically with platformers is that they don't realize that you don't want to *constantly* apply gravity. It's only needed for certain states (jumping, falling, etc). Something to consider as well, because it'll make your movement wonky and your objects will constantly try to compensate for their vertical position.

If I had to relate a finite state to Gamemaker, I'd say it's like having an entirely different set of code to run as a 'step' event per state. Easy stuff once you start getting into it, and it makes your life much easier :P
« Last Edit: February 08, 2011, 08:22:29 am by Mamoruアニメ »
Logged
Pages: [1]   Go Up

 


Contact Us | Legal | Advertise Here
2013 © ZFGC, All Rights Reserved



Page created in 0.195 seconds with 50 queries.

anything