• 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!

GML Pushing one or more objects

S

Simonbee10

Guest
I tried to create a way to make objects push eachother like this:

[O] = Object
-> = moving direction

Example:

[O] -> [O]
This will make them both go right, but a bit slower.

[O] -> <-[O]
This will make them stop, or the one with more speed and/or weight will overpower the other.

[O]->[O]->
This will make the right one go a little faster

[O]->[O]-> <-[O]
the two left one will probably win and push left at a certain speed.


I want to create this using weight and speed. If an object has more weight and speed it can push way more easily then an object that has less weight and/or speed. I'm also trying to create this without naming the objects so that I can create new object and give them the same parent/code and because of that they should behave the same.

This is my code so far (in step):
Code:
///Pushing stuff

if place_meeting(x+hsp,y,obMovable){

    //mobov is the other object(s)
    mobov = instance_place (x+hsp,y,obMovable);
 
        //when less weight object pushes heavier objects
    pushingF = mobov.weight / weight;
    if (pushingF > 1) {
        mobov.hsp = (mobov.hsp + hsp) / pushingF;
    }
        //same weight
    if (pushingF = 1) {
        mobov.hsp = (mobov.hsp + hsp);
    }
        //bigger pushes smaller objects
    if (pushingF < 1) {
        mobov.hsp =  (mobov.hsp + hsp) * (1-pushingF);
        show_debug_message("WOEKA");
    }
 
 
/*  A failed variant.
    force1 = weight * hsp;
    force2 = mobov.weight * mobov.hsp;
    force3 = force1 + force2;
    while(place_meeting(x+sign(hsp),y,obMovable)) {
    hsp = force3 / weight;
    show_debug_message(force3);
    }
    */
 
    //Don't go through the objects
    while(!place_meeting(x+sign(hsp),y,obMovable))
    {
        x += sign(hsp)
    }
    hsp = 0;
}
I managed to make the smaller object move me but I can't move the smaller object. I tried to move myself(a different object that doesn't move by itself) but it moves buggy.
I've also tested some things. I can't move the smaller object because of this code:

hsp = dir * -Espeed + fhsp

dir = direction
Espeed = extra speed
fhsp = friction speed
//shouln't matter though.

Before this code alot of different code is being run to see what dir, Espeed and fhsp is. After the code it just tests for walls and does:
x += hsp;
If I remove the whole hsp = di... then I'm able to move the smaller object although the smaller object doesn't work though.

I would really appriciate it if someone could help me with this.
Thank you in advance

I would also want to create a vertical version of this but that will come after I've completed this code.

[Note]: I,m still a beginner at game maker and english isn't my native language. Thanks for understanding
 
Last edited by a moderator:

kamiyasi

Member
I tried to create a way to make objects push eachother like this:

[O] = Object
-> = moving direction

Example:

[O] -> [O]
This will make them both go right, but a bit slower.

[O] -> <-[O]
This will make them stop, or the one with more speed and/or weight will overpower the other.

[O]->[O]->
This will make the right one go a little faster

[O]->[O]-> <-[O]
the two left one will probably win and push left at a certain speed.


I want to create this using weight and speed. If an object has more weight and speed it can push way more easily then an object that has less weight and/or speed. I'm also trying to create this without naming the objects so that I can create new object and give them the same parent/code and because of that they should behave the same.

This is my code so far (in step):
Code:
///Pushing stuff

if place_meeting(x+hsp,y,obMovable){

    //mobov is the other object(s)
    mobov = instance_place (x+hsp,y,obMovable);
  
        //when less weight object pushes heavier objects
    pushingF = mobov.weight / weight;
    if (pushingF > 1) {
        mobov.hsp = (mobov.hsp + hsp) / pushingF;
    }
        //same weight
    if (pushingF = 1) {
        mobov.hsp = (mobov.hsp + hsp);
    }
        //bigger pushes smaller objects
    if (pushingF < 1) {
        mobov.hsp =  5
        show_debug_message("WOEKA");
    }
 
  
/*  A failed variant.
    force1 = weight * hsp;
    force2 = mobov.weight * mobov.hsp;
    force3 = force1 + force2;
    while(place_meeting(x+sign(hsp),y,obMovable)) {
    hsp = force3 / weight;
    show_debug_message(force3);
    }
    */
  
    //Don't go through the objects
    while(!place_meeting(x+sign(hsp),y,obMovable))
    {
        x += sign(hsp)
    }
    hsp = 0; 
}
I managed to make the smaller object move me but I can't move the smaller object. I tried to move myself(a different object that doesn't move by itself) but it moves buggy.
I've also tested some things. I can't move the smaller object because of this code:

hsp = dir * -Espeed + fhsp

dir = direction
Espeed = extra speed
fhsp = friction speed
//shouln't matter though.

Before this code alot of different code is being run to see what dir, Espeed and fhsp is. After the code it just tests for walls and does:
x += hsp;
If I remove the whole hsp = di... then I'm able to move the smaller object although the smaller object doesn't work though.

I would really appriciate it if someone could help me with this.
Thank you in advance

I would also want to create a vertical version of this but that will come after I've completed this code.

[Note]: I,m a beginner at game maker and english isn;t my native language. Thanks for understanding
This would be a lot easierif you used Box2D physics, which you enable by making your object a physics object and enable physics in your room.
 
S

Simonbee10

Guest
This would be a lot easierif you used Box2D physics, which you enable by making your object a physics object and enable physics in your room.
Box2D?...

Anyway. If I enable physics for the room and the object they won't behave as I programmed to. How do I make them to only use the physics to collide with other object that is a parent or has physics enabled?
 

kamiyasi

Member
Box2D?...

Anyway. If I enable physics for the room and the object they won't behave as I programmed to. How do I make them to only use the physics to collide with other object that is a parent or has physics enabled?
Look in the help file for physics objects. Physics objects will only collide with objects that are either in the same collision group or have a code block in the collision event. You can just put in a blank code block and it will work.
 
S

Simonbee10

Guest
Look in the help file for physics objects. Physics objects will only collide with objects that are either in the same collision group or have a code block in the collision event. You can just put in a blank code block and it will work.
My object don't seem to move at all when physics is enabled. I've searched the internet briefly but to no avail. I am not sure if I have come across the help file you were speaking of. Would you mind sending a like to that for me?

EDIT:
Ok. I've done some research and it seems that if I want to use the physics engine I would have to scrap all the platfom... I forgot to say that I was creating a platformer. Back to the point. I would have to scrap all the platformer mechanics that I've created because they don't go along with the physics engine. I have no idea if its better for me to use the physics engine or not, but I would like to start with normal coding. Just coding stuff would help me greatly in learning better ways to code since I'm still a beginner.

So I can't combine my code with the physics engine ( x += hsp;.. ect) and I still want to know how to do this(Enemy collision stuff). Any ideas? or should I nontheless use the physics engine of GM?
 
Last edited by a moderator:

kamiyasi

Member
My object don't seem to move at all when physics is enabled. I've searched the internet briefly but to no avail. I am not sure if I have come across the help file you were speaking of. Would you mind sending a like to that for me?

EDIT:
Ok. I've done some research and it seems that if I want to use the physics engine I would have to scrap all the platfom... I forgot to say that I was creating a platformer. Back to the point. I would have to scrap all the platformer mechanics that I've created because they don't go along with the physics engine. I have no idea if its better for me to use the physics engine or not, but I would like to start with normal coding. Just coding stuff would help me greatly in learning better ways to code since I'm still a beginner.

So I can't combine my code with the physics engine ( x += hsp;.. ect) and I still want to know how to do this(Enemy collision stuff). Any ideas? or should I nontheless use the physics engine of GM?
You could try replacing x with phy_x and phy_y and see if that's enough to make it work. It can be tricky to implement onto a platformer but not infeasable. If you're not going to use the built in physics options, there would be a lot of collison detection and math required, which is my weaker area but if it seems easier for you go for it
 
S

Simonbee10

Guest
You could try replacing x with phy_x and phy_y and see if that's enough to make it work. It can be tricky to implement onto a platformer but not infeasable. If you're not going to use the built in physics options, there would be a lot of collison detection and math required, which is my weaker area but if it seems easier for you go for it
I tried it and it worked! My object fell, but as soon as it hits the 'ground' the game crashed xD. I'm giving up physics for now. I'll keep trying to get the original way a go and make it work.

Thanks for the replys and help btw :p

PS I REALLY need to go to sleep GoodNight
 
S

Simonbee10

Guest
Ok I tried stuff and stuff happend. Doesn't work yet. Does anyone know a code that makes all objects push eachother when they move against eachother?
 
B

Boogery Boogers

Guest
I asked about this same problem some time ago but, nobody was able to give me any suggestions on how to tackle it at the time, perhaps its just too complex without using the physics. I have no solution either yet.
 
S

Simonbee10

Guest
That sucks :/

I almost had it too.
Thanks for the reply anyways
 
H

Hossein

Guest
I asked about this same problem some time ago but, nobody was able to give me any suggestions on how to tackle it at the time, perhaps its just too complex without using the physics. I have no solution either yet.
i know your answer. 2 years ago i saw a tutorial that subjected about "surface" in that game we where a cop that need to kill some zombies skeleton. (pushing with out physics). tutorial name was about "SURFACE" i dont remember exact code but i remember that used "other." command when a collision done. you can find that tutorial that was in a old game maker studio version with "angry cats", "isometric" and stuff like that. you can ask a mod for find it.
 

sp202

Member
Code:
m - mass
v - velocity
1 & 2 - the respective objects

initial m1v1 + m2v2 = final m1v1 + m2v2
Assume both objects have the same velocity after collision:
Code:
initial m1v1 + m2v2 = final m1v3 + m2v3
You know the values of m1, m2, v1 and v2. Just solve for v3.
 
Top