"collision_circle" Detecting The Same Object

M

Marko Kajtazi

Guest
Hello everyone,
I have a problem with the collision_circle function. I want to have 2 variables with 2 different enemy objects, but when I do that, it detects the same enemy object.
Here is my code:
Code:
first_enemy = collision_circle(x, y, 192, oEnemys, true, true);
second_enemy = collision_circle(x, y, 192, oEnemys, true, true);

if((!first_enemy == 0)) {
    if(first_enemy.x < x) {
        left_target = first_enemy;
    }
  
    if(first_enemy.x > x) {
        right_target = first_enemy;
    }
}

if((!second_enemy == 0)) {
    if(second_enemy.x < x) {
        left_target = second_enemy;
    }
  
    if(second_enemy.x > x) {
        right_target = second_enemy;
    }
}
How can I set it to a different enemy object? Can anyone help me?
Thanks!!
 

Simon Gust

Member
You may get away with deactivating the first_enemy before checking for the second one. But I am not too sure because deactivation takes a step I believe.
Code:
first_enemy = collision_circle(x, y, 192, oEnemys, true, true);
instance_deactivate_object(first_enemy); // deactivate so it should be excluded from the collision loop

second_enemy = collision_circle(x, y, 192, oEnemys, true, true);
instance_activate_object(first_enemy); // reactivate it again

if((!first_enemy == 0)) { 
   if(first_enemy.x < x) {
       left_target = first_enemy;
   }
 
   if(first_enemy.x > x) {
       right_target = first_enemy;
   }
}

if((!second_enemy == 0)) {
   if(second_enemy.x < x) {
       left_target = second_enemy;
   }
 
   if(second_enemy.x > x) {
       right_target = second_enemy;
   }
}
Also, the next lines don't make a whole lot of sense and you're more or less just lucky they work.
collision_circle and other collision_ functions return either -4 (noone) or the instance id.
You should check like this
Code:
if (first_enemy != noone) {
 
M

Marko Kajtazi

Guest
You may get away with deactivating the first_enemy before checking for the second one. But I am not too sure because deactivation takes a step I believe.
Code:
first_enemy = collision_circle(x, y, 192, oEnemys, true, true);
instance_deactivate_object(first_enemy); // deactivate so it should be excluded from the collision loop

second_enemy = collision_circle(x, y, 192, oEnemys, true, true);
instance_activate_object(first_enemy); // reactivate it again

if((!first_enemy == 0)) {
   if(first_enemy.x < x) {
       left_target = first_enemy;
   }
 
   if(first_enemy.x > x) {
       right_target = first_enemy;
   }
}

if((!second_enemy == 0)) {
   if(second_enemy.x < x) {
       left_target = second_enemy;
   }
 
   if(second_enemy.x > x) {
       right_target = second_enemy;
   }
}
Also, the next lines don't make a whole lot of sense and you're more or less just lucky they work.
collision_circle and other collision_ functions return either -4 (noone) or the instance id.
You should check like this
Code:
if (first_enemy != noone) {
Thanks a lot. That worked great.
But now I have a problem with the objects, when I say point_direction(x, y, right_target.x, right_target.y) it says that the object is unknown. It doesn't set the variable in time to be used in other objects.
Ps: right_target is globalvar.
Do you now how to fix this?
 
Last edited by a moderator:

TheouAegis

Member
You need to verify if right_target was set or not. You only set the variable if there is an enemy to the right of the player. If there is no enemy to the right of the player, then it never gets set.

Furthermore, your code assumes that there is an enemy to the left and and an enemy to the right. What if both enemies are on the left? What if both enemies are on the right?
 
M

Marko Kajtazi

Guest
You need to verify if right_target was set or not. You only set the variable if there is an enemy to the right of the player. If there is no enemy to the right of the player, then it never gets set.

Furthermore, your code assumes that there is an enemy to the left and and an enemy to the right. What if both enemies are on the left? What if both enemies are on the right?
Of course,
but when I test it, I always put an enemy on the left and on the right (for testing purposes). The problem is that when I set the first enemy to the left_target or right_target, it actually doesn't set as the enemy Id.
Why is that?
 

TheouAegis

Member
Also, the next lines don't make a whole lot of sense and you're more or less just lucky they work.
It's not luck, it's a logical carry-over from legacy versions. The old documentation said they return "a negative value". (Not sure, but I could have sworn at one point in time they returned -1.) Since except for that one patch in Studio, GM has always treated anything less than 0.5, including negatives, as false and all else as 1. So it was the logical progression to check if the collision result was true, or if the complement of the result was false.


I still don't know why it's saying the object doesn' exist unless it has to do with the deactivation.

Another issue I have is tgis core only allows for 2 targets "randomly" selected. A list of all targets would be more suitable perhaps. Just using distance_to_object should suffice as well.

Code:
ds_list_clear(list); //create the list beforehand
var pq = ds_priority_create();
var dis,dir;
with oEnemys { 
   disd= distance_to_object(other.id);
   if dis < 192 {
      dir = point_direction(other.x,other.y,x,y) + dis*360;
      ds_priority_add(pq, id, dir);
   }
}
repeat ds_priority_size(pq)
   ds_list_add(list, ds_priority_delete_max(pq));
ds_priority_destroy(pq);
 
Top