adding collisions to move and click movement

maranpis

Member
Hello guys:

I'm working on point and click movements. I have a box that moves where the mouse is clicking, but I don't know how to add collisions if I can't divide my speed to hspd and vspd.I created two objects
1. a box ,obj_enemy_CL3_PointAClick
2. the crosshair obj_crosshair

this the box codes:

create event: spd=16; //setting speed

step event:

Code:
if instance_exists(obj_crosshair)  // if the crosshair exists the box moves towards the crosshair
{
var mouse_distance = point_distance(x,y,obj_crosshair.x,obj_crosshair.y) ;

move_towards_point(obj_crosshair.x,obj_crosshair.y,min(spd,mouse_distance)) ;
 }
Global left pressed mouse event

Code:
if (!instance_exists(obj_crosshair))
                {
                    instance_create_depth(mouse_x,mouse_y,0,obj_crosshair) ;
                }
And this is the crosshair codes:

Step event

Code:
if (place_meeting(x,y,obj_enemy_CL3_PointAClick))
            {
image_alpha=0;

            }
            else
            {
            image_alpha=1;
            }
Global left pressed mouse event

Code:
x=mouse_x;
y=mouse_y;

Does anyone know where i should put the x and y collision and how can i set it if in this case i don't have vspd and hspd? or maybe how can i set vspd and hspd in this code?

thanks ! ;)
 

NightFrost

Member
In the step event, after taking point_distance also get direction with point_direction:
Code:
var mouse_direction = (x, y, obj_crosshair.x, obj_crosshair.y);
Instead of simply moving towards the target, get your vspd and hspd using lengthdirs by replacing move_towards_point:
Code:
var hspd = lengthdir_x(min(spd, mouse_distance), mouse_direction);
var vspd = lengthdir_y(min(spd, mouse_distance), mouse_direction);
Now you have your x/y movement deltas and can proceed to do collision checks.
 

Roalinn

Member
in step event;
Code:
var xx,yy;
if(instance_exist(obj_crosshair)){
xx = sign(x-obj_crosshair.x);  // x direction of movement -1 or 1 ;
yy = sign(y-obj_crosshair.y); // y direction of movement -1 or 1;
}
else{xx=0; yy=0;}

if(place_free(x+xx*speed,y)){x+=xx*speed}
if(place_free(x,y+yy*speed)){y+=yy*speed}
maybe you can use it but i don't know i didn't try it.
 
Last edited:

maranpis

Member
In the step event, after taking point_distance also get direction with point_direction:
Code:
var mouse_direction = (x, y, obj_crosshair.x, obj_crosshair.y);
Instead of simply moving towards the target, get your vspd and hspd using lengthdirs by replacing move_towards_point:
Code:
var hspd = lengthdir_x(min(spd, mouse_distance), mouse_direction);
var vspd = lengthdir_y(min(spd, mouse_distance), mouse_direction);
Now you have your x/y movement deltas and can proceed to do collision checks.
Thanks night NightFrost! the code works! but the Ai move towards one place sometimes trying to go trough the wall and get stuck forever trying. Do you think i can implement one code for that or should i use pathfinding?

thanks!
 

maranpis

Member
in step event;
Code:
var xx,yy;
if(instance_exist(obj_crosshair)){
xx = sign(x-obj_crosshair.x);  // x direction of movement -1 or 1 ;
yy = sign(y-obj_crosshair.y); // y direction of movement -1 or 1;
}
else{xx=0; yy=0;}

if(place_free(x+xx*speed,y)){x+=xx*speed}
if(place_free(x,y+yy*speed)){y+=yy*speed}
maybe you can use it but i don't know i didn't try it.
thanks Roalinn i tried to do it but at the moment i don't have enough level to fully understand this code.I got lost in the xx and yy of the place_free code
 

maranpis

Member

CROmartin

Member
Code:
if (place_meeting(x,y,obj_enemy_CL3_PointAClick))
{
image_alpha=0;

}
else
{
image_alpha=1;
}
If objects are solid they stuck on each other, so mp_potential_step will maybe work if you make crosshair destroy it self when in collison with obj_enemy_CL3_PointAClick. You should also read abouth solid and not solid object relantionship if you didnt already.
 
Top