• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

anti gravity not wanted

P

piksil_demon

Guest
so below ive posted my gravity code. for some reason the object flies away, ignoring all collision. this code is modified from my players code which works just fine. any idea why its giving anti gravity?
Code:
//gravity
if (vspd<13){
vspd+=grav
     //same thing whether its a + or -
}

//horizontal collision
    if (place_meeting(x+hspeed,y,obj_solid)) {
while (!place_meeting(x+sign(hspeed),y,obj_solid)) {
x+=sign(hspeed);
}
hspeed*=-1;
}
x += hspeed;

//vertical collision
    if (place_meeting(x,y+vspd,obj_solid)) {
while (!place_meeting(x,y+sign(vspd),obj_solid)) {
y+=sign(vspd);
}
vspd=0;
}
 

jo-thijs

Member
I just asked a question, can't be wrong on that.

Anyway, the code you've given us has nothing to change y for as long as no vertical collision would occur if vspd would have changed y.
I'm suspecting you're setting vspeed to some value when you jump and never add vspd to y.

As for your question, yes it is the latter, you should add it at the end of your code.
If it is not working, what does happen instead?

Could you maybe give us the object information (text that appears when you click on "object information" when editing an object) of this object?

Also, why are you using hspeed instead of hspd?
 
P

piksil_demon

Guest
I just asked a question, can't be wrong on that.

Anyway, the code you've given us has nothing to change y for as long as no vertical collision would occur if vspd would have changed y.
I'm suspecting you're setting vspeed to some value when you jump and never add vspd to y.

As for your question, yes it is the latter, you should add it at the end of your code.
If it is not working, what does happen instead?

Could you maybe give us the object information (text that appears when you click on "object information" when editing an object) of this object?

Also, why are you using hspeed instead of hspd?
my bad, i didnt see the question you asked. but yes it is wrong. all it changes is now it bounces around after falling a bit heres what i mean- http://giphy.com/gifs/l0HlNqoYJolSDcltm

im using hspeed because its just a bullet, dosnt need complex code
 
P

piksil_demon

Guest
I see. That can have a couple of reasons.
It would be helpful if I could see the object information.
i already gave you all that has to do with its movement but if you insist-
Code:
[B]Information about object: obj_cat_bomb[/B]
Sprite: spr_cat_bomb
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent: 
Children: 
Mask: 
No Physics Object
Create Event:
execute code:

hspeed=obj_player.hspeed+2
vspeed= obj_player.vspeed-12
vspd=vspeed
grav=.75

Step Event:
execute code:

//gravity
if (vspd<13){
vspd+=grav
}

//horizontal collision
   if (place_meeting(x+hspeed,y,obj_solid)) {
while (!place_meeting(x+sign(hspeed),y,obj_solid)) {
x+=sign(hspeed);
}
hspeed*=-1;
}
x += hspeed;

//vertical collision
    if (place_meeting(x,y+vspd,obj_solid)) {
while (!place_meeting(x,y+sign(vspd),obj_solid)) {
y+=sign(vspd);
}
vspd=0;
}

y+=vspd
 

jo-thijs

Member
The issue is almost what I thought it'd be.
You've got 2 movement systems cancelling each other out.
You move your object vertically based on the value of vsp,
but you've also got vspeed set to something different from 0,
which makes GameMaker move the object again.

Get rid of 1 of the 2.
I'd suggest setting vspeed to 0 in the create event after you've initialized vspd.

You kind of have the same problem moving horizontally, but it is more camouflaged.
You should introduce a variable hspd and set hspeed to 0.
 
P

piksil_demon

Guest
The issue is almost what I thought it'd be.
You've got 2 movement systems cancelling each other out.
You move your object vertically based on the value of vsp,
but you've also got vspeed set to something different from 0,
which makes GameMaker move the object again.

Get rid of 1 of the 2.
I'd suggest setting vspeed to 0 in the create event after you've initialized vspd.

You kind of have the same problem moving horizontally, but it is more camouflaged.
You should introduce a variable hspd and set hspeed to 0.
im not sure what you mean, can you edit my code to show me? i use a similar code for player movement with no problem
 

jo-thijs

Member
Sure:
Create event:
Code:
hspd = obj_player.hspeed + 2;
vspd = obj_player.vspeed - 12;
grav = 0.75;
Step event:
Code:
//gravity
vspd = min(vspd + grav, 13);

//horizontal collision
if (place_meeting(x + hspd, y, obj_solid)) {
    while (!place_meeting(x + sign(hspd), y, obj_solid))
        x += sign(hspd);
    hspd = -hspd;
} else
    x += hspd;

//vertical collision
if (place_meeting(x, y + vspd, obj_solid)) {
    while (!place_meeting(x, y + sign(vspd), obj_solid))
        y += sign(vspd);
    vspd = 0;
} else
    y += vspd;
EDIT:
By the way, my point was: don't use both vspd and vspeed!
 
P

piksil_demon

Guest
Sure:
Create event:
Code:
hspd = obj_player.hspeed + 2;
vspd = obj_player.vspeed - 12;
grav = 0.75;
Step event:
Code:
//gravity
vspd = min(vspd + grav, 13);

//horizontal collision
if (place_meeting(x + hspd, y, obj_solid)) {
    while (!place_meeting(x + sign(hspd), y, obj_solid))
        x += sign(hspd);
    hspd = -hspd;
} else
    x += hspd;

//vertical collision
if (place_meeting(x, y + vspd, obj_solid)) {
    while (!place_meeting(x, y + sign(vspd), obj_solid))
        y += sign(vspd);
    vspd = 0;
} else
    y += vspd;
cool, that works. thanks
 
Top