Help with code for seperating collided objects

Pkirkby

Member
Hey everyone, I currently have a simple top-down RPG with a jump command, I've made it so when you jump, it negates the collide command, and with my depth working, I'm able to jump over enemies. However, once this happens I can potentially land on the enemy, getting stuck in them, as my code requires (zsp = 0) in order for the collision to happen.

I'm trying to figure out how I can automatically push my Player object off of the collision object, whether this is a rock, or an enemy etc. Thanks in advance!

Edit: I've almost figured it out with this code:

if (place_meeting(x,y,oCollision)) && (zsp = 0)
{
hsp = sign(oCollision);
vsp = sign(oCollision);
}

However, it only pushes me off the collision to the right, if I use "sign(-oCollision)" then it only pushes me to the left. Can anyone guide me make it so it pushes me left or right depending the position of my collision. I tried SIGN for this but cant seem to figure it out.

Also, another simple thing is increase the speed in which I'm pushed off of the collision. Thanks again.
 
Last edited:

NightFrost

Member
Assuming your sprite origins are centered, which would be logical for top-down game, subtract player x and y from enemy x and y, and take sign() from those operations. This gives you the directions to move out of collisions. Though it might of course happen that there's not enough free space nearby, and the player will "float" around until it finds some, perhaps through walls even (since your push code will not stop in any event).
 

Pkirkby

Member
I see what you're saying, I tried this:

if (place_meeting(x,y,oCollision)) && (zsp = 0)
{
hsp = sign(oCollision.x) - sign(oPlayer.x);
vsp = sign(oCollision.y) - sign(oPlayer.y);
}

and it doesnt push anything away, keep in mind I've got alot to learn, so perhaps I haven't done this right, any suggestions? Thanks again.
 

Pkirkby

Member
Bump, still trying to solve this. Thanks.

Here's my code currently, it seems to push me off to the right of the object, and I'm trying to figure out how to do it faster, and have it push me off whatever direction is closest.
 

FrostyCat

Member
You would have solved this half a month ago if you knew the difference between an object and an instance.
NEVER access a single instance by object ID if multiple instances of the object exist. This includes attempts to reference or set object.variable (which is inconsistent across exports) and using with (object) to apply actions to it (this encompasses all instances of the object instead of just the one you want). Verbally, "Dog's colour" makes sense with one dog, but not with multiple dogs.
Collision-checking functions: instance_place() and instance_position() are the instance-ID-oriented analogues of place_meeting() and position_meeting(). Functions that start with collision_ all return instance IDs. Save that instance ID into a variable (e.g. var inst_enemy = instance_place(x, y, obj_enemy);), then use that as the subject to work with (e.g. inst_enemy.hp -= 10;). Note that these functions return noone upon not finding a collision. Always account for this special case whenever you handle collisions this way.
And is it really that difficult to tell the difference between sign(x2)-sign(x1) and sign(x2-x1)?
 

Pkirkby

Member
You would have solved this half a month ago if you knew the difference between an object and an instance.



And is it really that difficult to tell the difference between sign(x2)-sign(x1) and sign(x2-x1)?
Since I'm very early on, and have a lot to learn about not only programming, but game maker, I tend to miss redundancies and misuse some functions, as you can see.

OCollision is a parent object for certain collisions, so it controls my enemies for now, it's just a starting point for learning. To a small extent, I do understand the difference between instances and objects, but I was simply trying to find a way to have my player slide off an enemy when he lands him. It works with this code, but I'm not sure where I'm going wrong.
 

FrostyCat

Member
Please don't tell me you've been a sitting duck for 4 days just because you can't be bothered with the difference between objects and instances. oCollision.x and oCollision.y should have been immediate red flags for anyone who knows the difference. And your plane geometry needs work if you didn't get my other clue.
Code:
var iCollision = instance_place(x, y, oCollision);
if (iCollision != noone && zsp == 0)
{
  hsp = sign(x-iCollision.x);
  vsp = sign(y-iCollision.y);
}
 

Pkirkby

Member
Please don't tell me you've been a sitting duck for 4 days just because you can't be bothered with the difference between objects and instances. oCollision.x and oCollision.y should have been immediate red flags for anyone who knows the difference. And your plane geometry needs work if you didn't get my other clue.
Code:
var iCollision = instance_place(x, y, oCollision);
if (iCollision != noone && zsp == 0)
{
  hsp = sign(x-iCollision.x);
  vsp = sign(y-iCollision.y);
}
Not sure whats with the attitude but I do appreciate the response regardless. I work on trying to learn other things while I'm stumped and come back to it, I'll give this a go when I'm back to it. Again, I AM NOT a pro and don't claim to be, this forum is a good learning tool for me to learn practical uses from the kindness of strangers.
 
Top