• 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 Best way to ping-pong object (SOLVED)

jujubs

Member
Hey everyone. I'm currently working on an enemy for my game which moves down the screen whilst swaying left and right. I achieved this by making it spawn two other objects that sit beside it and send it flying to the other. This works, but makes things needlessly complicated, I guess. I can't put two of those enemies together, for instance, because they'll collide with each others' sub-objects and go flying everywhere. It also keeps me from adding smooth movement, which is something I think would look really cool.

So, what's the best way to do this?
 
I don't know how to link the page - but look on the forum under 'tutorials' for this:

"A beginners intro to the fun of sine waves: giving sprites fake 3d effects"

A sine wave is an undulating wave that would give you the downward movement, and the alternation from left to right. The video on that page shows the maths behind it.

EDIT: Unfortunately they don't give a direct explanation of how to do what you're asking, though it does show an example where you can see how the sine wave could be applied for your purposes.
 
Last edited:

jujubs

Member
I don't know how to link the page - but look on the forum under 'tutorials' for this:

"A beginners intro to the fun of sine waves: giving sprites fake 3d effects"

A sine wave is an undulating wave that would give you the downward movement, and the alternation from left to right. The video on that page shows the maths behind it.

EDIT: Unfortunately they don't give a direct explanation of how to do what you're asking, though it does show an example where you can see how the sine wave could be applied for your purposes.
Thanks, dude. I'll have a look.
 
np

sine_1.pngsine_2.png

The screenshot on the left is taken from the video, where they show how a sine wave looks. The one on the right is just to illustrate how you'd need to figure out implementing it for your purposes.
 
Last edited:

Bentley

Member
Hey everyone. I'm currently working on an enemy for my game which moves down the screen whilst swaying left and right. I achieved this by making it spawn two other objects that sit beside it and send it flying to the other. This works, but makes things needlessly complicated, I guess. I can't put two of those enemies together, for instance, because they'll collide with each others' sub-objects and go flying everywhere. It also keeps me from adding smooth movement, which is something I think would look really cool.

So, what's the best way to do this?
Edit: I wrote this kind of fast so don't there might be something wrong, but hopefully the idea is useful.

I think this should do what you asked: move side to side smoothly while falling.
Code:
// Destination
if (x == targ_x)
{
    facing = -facing;
 
    // Set a new destination
    targ_x = x + sway_dist * facing;
}
else
{
    // Move horizontally
    if (facing == 1)
    {
        x = min(x + accel, targ_x);
    }
    else // if (facing == -1)
    {
        x = max(x - accel, targ_x);
    }
 
    // Move vertically
    // grav = min(grav, grav_max); // This line is only if you want to fall faster and faster and cap it
    y += grav;
}
 
Last edited:

jujubs

Member
Thanks, guys. I ended up using the code from the video. It looks like this:

Create:
Code:
sin_value = 0;
steps = 60;
Step:
Code:
sin_value +=(pi*2)/steps;

if(sin_value >= pi*2)
{
    sin_value = 0;
}

x += sin(sin_value)*3;
I also used y -= 1 to make the object fall. It looks very nice :3
 
Top