# How to Invert Gravity?

22 replies to this topic

### #1 Tom Fama

Tom Fama

GMC Member

• New Member
• 61 posts

Posted 04 February 2012 - 05:57 PM

I am making a platformer obstacle game and when you press a button, you become an object with inverted gravity and movement. For the normal object, I use this code:

if !place_meeting(x,y+1,obj_block) // if we are in the air
{
grav = .5; // set the gravity to .5
vspd += grav; // add .5 to our vspd once every step
}
else // else if we are on the ground
{
grav = 0; // set the gravity to 0
vspd = 0; // set vspd to 0 to stop moving
if keyboard_check_pressed(vk_up) // since we are on the ground, we can handle jumping here, so check if we pressed up
{
vspd = -10; // set the vspd to -8, which will make us jump
}
}
if keyboard_check_released(vk_up) // if we released the up button while in the in air
{
vspd *= .5; // divide our vspd by 2, creating a smooth type of variable jumping
}
if vspd > 10 // we don't want to fall too fast, so lets limit our vspd
{
vspd = 10; // note that if you want to be able to fall fast, you can remove this without affecting the code (but I wouldn't)
}
repeat(abs(vspd)) // we want to check for a collision every pixel, so we use a repeat() function to check every pixel while falling
{
if !place_meeting(x,y+sign(vspd),obj_block) // vspd can be negative or positive, so we check 1 pixel above or below, depending on which direction we are going
{
y += sign(vspd); // add to our y value 1 pixel at a time, letting use check for a collision every pixel
}
else // else if we hit a block
{
vspd = 0; // stop moving vertically
}
}
hspd = (keyboard_check(vk_right)-keyboard_check(vk_left))*5; // a nice little trick that will set our hspd to +4 or -4 depending what button we are holding
repeat(abs(hspd)) // same as the vspd, we want to check for a collision at every pixel we move
{
if !place_meeting(x+sign(hspd),y,obj_block) // if there is no block to our left or right
{
x += sign(hspd); // add to our x value depending on which way we are going
}
}
(note: this is not my code, all credit goes to Canite)

I mostly figured out how to invert it by simply changing positive values to negative values and vice versa, but the only problem is the jumping. I try to jump but it's kinda glitchy, so how would this code be altered to allow jumping with inverted gravity? Thank you.

Edited by Tom Fama, 04 February 2012 - 06:20 PM.

• 0

### #2 Nocturne

Nocturne

Nocturne Games

• 17024 posts
• Version:GM:Studio

Posted 04 February 2012 - 06:06 PM

Hi there! First of all, if you post in the wrong place DO NOT post a duplicate topic... rather, press that nice <REPORT> button and request that the topic be moved... Now, as to your problem, some decent formating of your code would work wonders! At the moment it's nigh on impossible to understand, so please use the [code=auto:0] tags and indent the code to make more readable.
• 0

### #3 Tom Fama

Tom Fama

GMC Member

• New Member
• 61 posts

Posted 04 February 2012 - 06:16 PM

I'm sorry I don't use the forums too much but I will remember that for future reference and I don't know how to use the code tags. If the site could make it a little easier to change where to post a forum WHILE typing it, that would be much better. I will try to edit this to make it more understandable.
• 0

### #4 Tom Fama

Tom Fama

GMC Member

• New Member
• 61 posts

Posted 04 February 2012 - 06:21 PM

Okay, the code tag is fixed now.
• 0

### #5 Tom Fama

Tom Fama

GMC Member

• New Member
• 61 posts

Posted 05 February 2012 - 02:11 AM

The only problem is jumping while on the ceiling, so does anybody know how to fix it using the code provided?
• 0

### #6 Tom Fama

Tom Fama

GMC Member

• New Member
• 61 posts

Posted 05 February 2012 - 02:14 AM

The only problem is jumping while on the ceiling, so does anybody know how to fix it using the code provided?
• 0

### #7 greep

greep

Menaces with Spikes

• GMC Member
• 2300 posts
• Version:GM7

Posted 05 February 2012 - 02:21 AM

Show us the code you are using for the reverse gravity object and/or how it's glitchy. Keep in mind, you're probably better off not making a separate object, but mere making a variable called "invertedGravity = false" and setting it to true if inverted, then changing things your movement accordingly.

Also: Follow the forum bumping rules :/ Do not post again for 48 hours if someone hasn't replied, just edit your post.

Edited by greep, 05 February 2012 - 02:21 AM.

• 0

### #8 Tom Fama

Tom Fama

GMC Member

• New Member
• 61 posts

Posted 05 February 2012 - 02:34 AM

Here is the code I tried:
if !place_meeting(x,y-1,obj_block) // if we are in the air
{
grav = -0.5; // set the gravity to .5
vspd -= grav; // add .5 to our vspd once every step
}
else // else if we are on the ground
{
grav = 0; // set the gravity to 0
vspd = 0; // set vspd to 0 to stop moving
if keyboard_check_pressed(vk_up) // since we are on the ground, we can handle jumping here, so check if we pressed up
{
vspd = +10; // set the vspd to -8, which will make us jump
}
}
if keyboard_check_released(vk_up) // if we released the up button while in the in air
{
vspd *= -0.5; // divide our vspd by 2, creating a smooth type of variable jumping
}
if vspd < -10 // we don't want to fall too fast, so lets limit our vspd
{
vspd = -10; // note that if you want to be able to fall fast, you can remove this without affecting the code (but I wouldn't)
}
repeat(abs(vspd)) // we want to check for a collision every pixel, so we use a repeat() function to check every pixel while falling
{
if !place_meeting(x,y-sign(vspd),obj_block) // vspd can be negative or positive, so we check 1 pixel above or below, depending on which direction we are going
{
y -= sign(vspd); // add to our y value 1 pixel at a time, letting use check for a collision every pixel
}
else // else if we hit a block
{
vspd = 0; // stop moving vertically
}
}
hspd = (-keyboard_check(vk_right)+keyboard_check(vk_left))*5;
// a nice little trick that will set our hspd to +4 or -4 depending what button we are holding
repeat(abs(hspd)) // same as the vspd, we want to check for a collision at every pixel we move
{
if !place_meeting(x-sign(hspd),y,obj_block) // if there is no block to our left or right
{
x -= sign(hspd); // add to our x value depending on which way we are going
}
}

I mostly changed all the positives to negatives and vice versa and I inverted the left and right control scheme, but when you try to jump on the ceiling, nothing happens. If you try to jump in midair, then you kind of hover but I'm not very concerned with that. I alsp apologize for "bumping", I will remember that.
• 0

### #9 greep

greep

Menaces with Spikes

• GMC Member
• 2300 posts
• Version:GM7

Posted 05 February 2012 - 02:40 AM

Change

if !place_meeting(x,y-sign(vspd),obj_block)
direction we are going{y -= sign(vspd);

back to what it was. I.e. the two minuses to pluses.
• 0

### #10 Tom Fama

Tom Fama

GMC Member

• New Member
• 61 posts

Posted 05 February 2012 - 03:01 AM

Do you mind putting in the full code so I can copy and paste because that would just be a lot easier. Thanks you.
• 0

### #11 greep

greep

Menaces with Spikes

• GMC Member
• 2300 posts
• Version:GM7

Posted 05 February 2012 - 03:13 AM

Pffft, just cntrl-F and copy paste the code into it, then find it and change the signs. But just to humor you...

if !place_meeting(x,y-1,obj_block) // if we are in the air
{
grav = -0.5; // set the gravity to .5
vspd -= grav; // add .5 to our vspd once every step
}
else // else if we are on the ground
{
grav = 0; // set the gravity to 0
vspd = 0; // set vspd to 0 to stop moving
if keyboard_check_pressed(vk_up) // since we are on the ground, we can handle jumping here, so check if we pressed up
{
vspd = +10; // set the vspd to -8, which will make us jump
}
}
if keyboard_check_released(vk_up) // if we released the up button while in the in air
{
vspd *= -0.5; // divide our vspd by 2, creating a smooth type of variable jumping
}
if vspd < -10 // we don't want to fall too fast, so lets limit our vspd
{
vspd = -10; // note that if you want to be able to fall fast, you can remove this without affecting the code (but I wouldn't)
}
repeat(abs(vspd)) // we want to check for a collision every pixel, so we use a repeat() function to check every pixel while falling
{
if !place_meeting(x,y+sign(vspd),obj_block) // vspd can be negative or positive, so we check 1 pixel above or below, depending on which direction we are going
{
y += sign(vspd); // add to our y value 1 pixel at a time, letting use check for a collision every pixel
}
else // else if we hit a block
{
vspd = 0; // stop moving vertically
}
}
hspd = (-keyboard_check(vk_right)+keyboard_check(vk_left))*5;
// a nice little trick that will set our hspd to +4 or -4 depending what button we are holding
repeat(abs(hspd)) // same as the vspd, we want to check for a collision at every pixel we move
{
if !place_meeting(x-sign(hspd),y,obj_block) // if there is no block to our left or right
{
x -= sign(hspd); // add to our x value depending on which way we are going
}
}

Edited by greep, 05 February 2012 - 03:14 AM.

• 0

### #12 Tom Fama

Tom Fama

GMC Member

• New Member
• 61 posts

Posted 05 February 2012 - 03:29 AM

Thank you, but the player bounces on the floor and goes right through the ceiling. By the way, my game is very simple, just 32x32 blocks for the player and walls but I don't think that matters, but it does not work.
• 0

### #13 greep

greep

Menaces with Spikes

• GMC Member
• 2300 posts
• Version:GM7

Posted 05 February 2012 - 03:44 AM

Hrm... well I'm kinda confused about that, but I spotted another problem, change vspd *= -0.5 back to vspd *= 0.5 in the keyboard up release part.
• 0

### #14 Tom Fama

Tom Fama

GMC Member

• New Member
• 61 posts

Posted 05 February 2012 - 04:05 AM

Nope, still the same except it only goes through the walls if it touches them. Otherwise, it still bounces on the floor.
• 0

### #15 greep

greep

Menaces with Spikes

• GMC Member
• 2300 posts
• Version:GM7

Posted 05 February 2012 - 04:12 AM

Hrm... I haven't done many platformers so I'm probably going to not be much help. Umm... try setting...

if !place_meeting(x,y+sign(vspd),obj_block) back to if !place_meeting(x,y-sign(vspd),obj_block)

But definately keep the y += sign(vspd);
• 0

### #16 Manuel777

Manuel777

• GMC Member
• 2957 posts
• Version:GM:Studio

Posted 05 February 2012 - 04:25 AM

In the create event of the object add this line;
dir = 1;

Then, use this on the step event (it's the same as above; but fixed, shortened and with no comments, to make it easyer for me to read )
if !place_meeting(x,y+(1*dir),obj_block) {
grav = 0.5;
vspd += grav;
}
else {
grav = 0;
vspd = 0;
if keyboard_check_pressed(vk_up); {
vspd = -10;
}
}
if keyboard_check_released(vk_up) {
vspd *= 0.5;
}
if vspd > 10 {
vspd = 10;
}
repeat (abs(vspd)) {
if !place_meeting(x,y+sign(vspd*dir),obj_block) {
y += sign(vspd*dir);
}
else {
vspd = 0;
}
}
hspd = (keyboard_check(vk_right)-keyboard_check(vk_left))*5;
repeat(abs(hspd)) {
if !place_meeting(x+sign(hspd),y,obj_block) {
x += sign(hspd);
}
}

To invert gravity, use this;
dir *= -1;

It worked like a charm for me at 60fps, in GM8.1

Edited by manuel777, 05 February 2012 - 04:27 AM.

• 0

### #17 Tom Fama

Tom Fama

GMC Member

• New Member
• 61 posts

Posted 05 February 2012 - 04:49 AM

@manuel777, is that code for the normal gravity or inverted gravity? Either way, it does not work for me. Also, if the
dir *= -1;

code goes in the create event, then I keep getting error:

___________________________________________
ERROR in
action number 1
of Create Event
for object player_1_inv_grav:

Error in code at line 6:
dir *= -1;
^
at position 2: Unknown variable dir
I am mainly concerned with the inverted gravity, I do not wish to alter the regular one in any way as it works great.
• 0

### #18 Manuel777

Manuel777

• GMC Member
• 2957 posts
• Version:GM:Studio

Posted 05 February 2012 - 05:28 AM

Nope, use that code anywhere but in the step or create events, preferrably on a key press event.. the one you should place on the create event is the first line.

Ps; the dir variable handles the gravity direction, if its set to 1, the gravity goes up to down, if its set to -1 the gravity will go down to up.. its just to show the cool effect it creates changing the gravity direction over and over

Edited by manuel777, 05 February 2012 - 05:34 AM.

• 0

### #19 Tom Fama

Tom Fama

GMC Member

• New Member
• 61 posts

Posted 05 February 2012 - 02:55 PM

So, I put this:
dir = 1;
In the create event of the object with inverted gravity, and this:
if !place_meeting(x,y+(1*dir),obj_block) {
grav = 0.5;
vspd += grav;
}
else {
grav = 0;
vspd = 0;
if keyboard_check_pressed(vk_up); {
vspd = -10;
}
}
if keyboard_check_released(vk_up) {
vspd *= 0.5;
}
if vspd > 10 {
vspd = 10;
}
repeat (abs(vspd)) {
if !place_meeting(x,y+sign(vspd*dir),obj_block) {
y += sign(vspd*dir);
}
else {
vspd = 0;
}
}
hspd = (keyboard_check(vk_right)-keyboard_check(vk_left))*5;

repeat(abs(hspd)) {
if !place_meeting(x+sign(hspd),y,obj_block) {
x += sign(hspd);
}
}
In the step event of the inverted gravity object, and then I put this:
dir *= -1;
In the Key_Press_Up event. I think that should be correct, BUT I went back to the code in the step event and changed the second line from grav = 0.5; to grav -= 0.5; which was a HUGE difference. But now, when I press up, I get this error:
___________________________________________
ERROR in
action number 1
of  Step Event
for object player_1_inv_grav:

Error in code at line 8:
if keyboard_check_pressed(vk_up);
^
at position 38: Statement expected.
I have never gotten this error before when I pressed up, nor do I know what it means. If this could be fixed then I believe that should be all, provided that pressing up will perform an "upside-down jump".

EDIT: I fixed the error by removing the semicoln after the (vk_up) part so that is fixed. However, when I press up on the invert gravity object, it does not only jump, it sticks to the opposite wall. If you are on the ceiling and you press up, it kinda snaps to the floor and if you jump on the floor, it snaps to the ceiling. This is definitely better than anything I was able to do, but I would appreciate it if the object could just do an upside down jump on the ceiling only, with vertical speed being 10 rather than -10 like on normal gravity.

Edited by Tom Fama, 05 February 2012 - 03:05 PM.

• 0

### #20 Manuel777

Manuel777

• GMC Member
• 2957 posts
• Version:GM:Studio

Posted 05 February 2012 - 04:18 PM

Well, you are using the Up key already to jump, so to invert gravity you shouldnt use Up as well, and if you do you should change the jump key to a different one, thats why it gets stuck. The tests i made where using the Space key

Edited by manuel777, 05 February 2012 - 04:18 PM.

• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users