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

Legacy GM Collision when using speed + direction

zingot

Member
I am looking at the best solution for checking collisions in GML (or not colliding but checking). Looking at various examples online many are adding to x or y

Code:
if !place_meeting(x + 1, y, obj_solid)  {
       x = x + 1;
}
but I want to simply set speed or direction. Any suggestions guys?
 
  • Wow
Reactions: Mut
The method you're talking about can support speed for sure:

Code:
repeat (abs(xspeed)) {
      if (!place_meeting(x + sign(xspeed), y, obj_solid))  {
            x += sign(xspeed);
      } else break;
}
 

TheouAegis

Member
Just an FYI, speed, hspeed, and vspeed are all the same thing. And using amerpandbear's suggestion (modified how you need) works, but care needs to be taken.

In the case of using the built-in variables, you can check if there is a collision ahead and then decrease toward 0 your speed until there is no collision.
 

zingot

Member
Cool suggestions. I like the idea ampersandbear suggests of using repeat { } to move closer to the solid surface preventing stopping at a distance away. Also nightfrost I'd forgotten I can use the lengthdir as calculable vectors to check distance. This all gives me food for thought. Thanks all ;-)
 

NightFrost

Member
If you're going to work with vectors, it is a good idea to write a library of vector scripts that do the drudge work for you: vector create, vector add and substract, normalization, dot product, etc.
 

zingot

Member
Here is effectively what I'm using in my new project. I created a separate project with everything stripped out. Here is my gmz file (controls are left / right cursors). Any thoughts on what I can do here to get a better collision system without the stickiness would be appreciated. I tried amending to accommodate the ideas given above but I couldn't get things working correctly and everything was either worse or failed totally. Thanks in advance for your guys time and testing my gmz file for guidance, it's much appreciated.

https://ufile.io/3wsi0 < gmz file.

I appreciate many of you won't want to download a gmz file so I have broken down the code of obj_ball below.

Code:
[CREATE]
/// Init. Ball Variables
REAL_MAX_SPEED = 3.0;
REAL_DEG_TURN  = 2.5;
REAL_THROTTLE  = 0.05;
REAL_BRAKING   = 0.025;
NEW_DIRECTION  = 270;
direction      = NEW_DIRECTION; //force initial direction!
Code:
[STEP]
/// Ball Logic Stuff.....
// Increase Ball Speed
speed = speed + REAL_THROTTLE;    
if (speed > REAL_MAX_SPEED) { speed = REAL_MAX_SPEED; }

// Change Player Direction (Over Time)
gradually_turn(obj_ball, NEW_DIRECTION, REAL_DEG_TURN);
image_angle = direction;
Code:
[END STEP]
/// Dodgy Collision!
if place_meeting(x+hspeed, y, obj_solid) { x = xprevious; } // Right Collision
if place_meeting(x-hspeed, y, obj_solid) { x = xprevious;  } // Left Collision
if place_meeting(x, y+vspeed, obj_solid) { y = yprevious; } // Bottom Collision
if place_meeting(x, y-vspeed, obj_solid) { y = yprevious;  } // Top Collision
NEW_DIRECTION is changed by 90 degrees with left/right cursors and moved by my gradually_turn script depicted here.

Code:
[SCRIPT]
///gradually_turn(OBJ_TO_TURN, TARGET_DIR, TURN_SPEED);

var OBJ_TO_TURN = argument0;
var TARGET_DIR  = argument1;
var TURN_SPEED  = argument2;

// Get the facing direction
var OBJ_DIR = OBJ_TO_TURN.direction;

// Calculate the difference between TARGET_DIR direction and facing direction
var facingMinusTarget = OBJ_DIR - TARGET_DIR;
var angleDiff = facingMinusTarget;

if(abs(facingMinusTarget) > 180) {
    if(OBJ_DIR > TARGET_DIR) {
        angleDiff = -1 * ((360 - OBJ_DIR) + TARGET_DIR);
        } else {
        angleDiff = (360 - TARGET_DIR) + OBJ_DIR;
    }
}

// Gradually rotate object
if(angleDiff > 0) {
    OBJ_TO_TURN.direction -= TURN_SPEED;
    } else if (angleDiff < 0) {
    OBJ_TO_TURN.direction += TURN_SPEED;
}
 
Last edited:
Top