Jump to content


Photo

Collision_line detection problem


  • Please log in to reply
5 replies to this topic

#1 Nuktu

Nuktu

    GMC Member

  • GMC Member
  • 19 posts
  • Version:GM8

Posted 11 March 2012 - 05:43 PM

Hi all,

Im creating a tank game, and I want the enemy tanks to see the players tank if its on the enemies X or Y axis.

I am using Collision_line to check the X and Y axis for the player. However the enemy tanks just get stuck when they spawn and dont move.

Can anybody help?

Walls are solid
Player's tank and enemy tank are not solid.
This code is placed in step event.

//Checks if the players tank is on the Y axis +200
var coldown;    coldown     = collision_line( obj_enemy_tank3.x+16,obj_enemy_tank3.y+16, x,y+200, obj_tank, 0, 1) ;

//Checks to see if there is a wall in the way, on the same Y axis
var waldown;    waldown     = collision_line( obj_enemy_tank3.x+16,obj_enemy_tank3.y+16, x,y+200, obj_BrickWall, 1, 1) ;

//Store the instance of the players tank. Not sure if this should be 1 or 0. Only 1 instance is ever created
player_inst = instance_find(obj_tank,1);

//If the collision line down, is the same as the players instance
if(coldown == player_inst) 
{
    //If the collision line down, returns noone(-4) then there is no wall, and continues.
    if(waldown < 0)
    {
        //turn down and shoot
        sprite_index = spr_enemy_1_down;
        motion_set(270,2);
        enemy_dir = 3; //For shooting direction
        move_snap(16,16);
    }
}

//This is repeated for the other directions.

  • 0

#2 Anzkji

Anzkji

    Seer of Space

  • GMC Member
  • 443 posts
  • Version:GM8

Posted 11 March 2012 - 06:02 PM

collision_line doesn't actually have anything to do with movement, so the error must be in the REAL collision, not the enemy detection. You don't need to find the player tank, if it is in that instance's code, just use
self
. Or if it is in another object's code, just use obj_player_tank_whatever if there is only one of them.

BTW:
I'd use code more like this:

//Checks if the players tank is on the Y axis
if (abs(obj_tank.y-y) < y_axis_diff && distance_to_object(obj_tank) < 200) {
	if (collision_line(x,y,obj_tank.x,obj_tank.y,obj_WallPar,1,1) < 0) {
        //turn up or down and shoot
    	var dir; dir = round(point_direction(x,y,obj_tank.x,obj_tank.y)/90)*90
    	sprite_index = spr_enemy_1_down;
        motion_set(dir,2);
        enemy_dir = dir/90; //For shooting direction
        move_snap(16,16);  } } 

//Checks if the players tank is on the X axis
if (abs(obj_tank.x-x) < x_axis_diff && distance_to_object(obj_tank) < 200) {
	if (collision_line(x,y,obj_tank.x,obj_tank.y,obj_WallPar,1,1) < 0) {
        //turn up or down and shoot
    	var dir; dir = round(point_direction(x,y,obj_tank.x,obj_tank.y)/90)*90
    	sprite_index = spr_enemy_1_down;
        motion_set(dir,2);
        enemy_dir = dir/90; //For shooting direction
        move_snap(16,16);  } }

Edited by Anzkji, 11 March 2012 - 06:04 PM.

  • 0

#3 starfire_64

starfire_64

    Galactic Flame

  • GMC Member
  • 109 posts
  • Version:GM:Studio

Posted 11 March 2012 - 06:27 PM

Tip to Remember:

collision_line returns an instance id of an object on that line.
  • 0

#4 TheouAegis

TheouAegis

    GMC Member

  • GMC Member
  • 4700 posts
  • Version:GM8

Posted 11 March 2012 - 07:02 PM

I'd personally avoid using collision_line() in this case unless I read the code wrong. Seems like it would be too slow of a function compared to simple embedded relative coordinate checks in this situation.
  • 0

#5 Nuktu

Nuktu

    GMC Member

  • GMC Member
  • 19 posts
  • Version:GM8

Posted 11 March 2012 - 07:40 PM

collision_line doesn't actually have anything to do with movement, so the error must be in the REAL collision, not the enemy detection. You don't need to find the player tank, if it is in that instance's code, just use

self
. Or if it is in another object's code, just use obj_player_tank_whatever if there is only one of them.


Thanks! This code is in the enemy tank object, so I cannot use self to find the players tank

Im not quite sure what you mean by this
if (abs(obj_tank.y-y) < y_axis_diff
and this might not work for me, as I use 4 separate sprites for the enemy object and then I change between them depending on their direction.
I know its not best practice but i dont have much experience with this.
    	var dir; dir = round(point_direction(x,y,obj_tank.x,obj_tank.y)/90)*90


Tip to Remember:

collision_line returns an instance id of an object on that line.


Yes, thats why I find the player's tanks instance id and use that for comparison. Thanks.

I'd personally avoid using collision_line() in this case unless I read the code wrong. Seems like it would be too slow of a function compared to simple embedded relative coordinate checks in this situation.


I have not come across anything like that in manual or tutorials, so if you know anything that might be easier please let me know! Thanks.

Edited by Nuktu, 11 March 2012 - 07:50 PM.

  • 0

#6 Nuktu

Nuktu

    GMC Member

  • GMC Member
  • 19 posts
  • Version:GM8

Posted 11 March 2012 - 08:00 PM

I have removed all other instances of enemy tanks and left only one, now the tank moves without any problems.
No idea what the issue was, because the collision_line argument notme was set to true, which indicates that the calling instance should not be checked.

Ive also changed
player_inst = instance_find(obj_tank,1);
to
player_inst = instance_find(obj_tank,0);

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users