• Hey! Guest! The 40th (!!!) GMC Jam will take place between February 25th, 12:00 UTC to March 1st 12:00 UTC. Why not join in this very special anniversary jam! Click here to find out more!

SOLVED Grids are not following the mouse fast enough (gif inside)

mikix

Member
What you need to do is keep track of the previous position of the mouse each step and then use a line algorithm (like bresenham's) to join the current position and previous position together, rather than just using whatever grid the mouse is over each step.
This sounds like advanced mathematics. o_O So there's no built-in function in GML that can do this?
 
Not that I'm aware of. I imagine there's already a bresenham's line algo somewhere out there for GMS, I know I have made one in the past. Maybe check GMLscripts.com and see if they have a script for it.
 

mikix

Member
Not that I'm aware of. I imagine there's already a bresenham's line algo somewhere out there for GMS, I know I have made one in the past. Maybe check GMLscripts.com and see if they have a script for it.
Too bad the only one I found on the GM marketplace was for the GM 1.4. :\ I'll see what I can do with the link you sent me.
 

mikix

Member
So I changed it a bit... and it just creates too many grids on the screen.

GML:
function line(x0, y0, x1, y1)
{
    deltax = x1 - x0;
    deltay = y1 - y0;
    deltaerr = abs(deltay / deltax);    // Assume deltax != 0 (line is not vertical),
          // note that this division needs to be done in a way that preserves the fractional part
    error = 0.0 // No error at start
    gr_msy = y0
   

   
    for (gr_msx = x0; x0 < x1; x0++)
    {
    for (gr_msy = y0; y0 < y1; y0++)
    {
        gridlasso[# x0, y0]=1;
    }
    }
        error = error + deltaerr
        if error >= 0.5 then
            gr_msy = gr_msx + sign(deltay)
            error = error - 1.0

}
 
Last edited:

mikix

Member
Okay, back again. So I think that I am progressing a bit here. The problem is that it starts at the top left corner. Here is a gif and the code.


engine6.gif


GML:
if mouse_check_button_pressed(mb_left) && global.lasso = true
{
    
lxx=floor(mouse_x/sizelassox)
lyy=floor(mouse_y/sizelassoy)

line(lxx,lyy,mouse_x,mouse_y)

old_mouse_x11 = mouse_x
old_mouse_y11 = mouse_y

}

if mouse_check_button(mb_left) && (mouse_x != old_mouse_x11 || mouse_y != old_mouse_y11)
&& global.lasso = true
{
    



//Get vector from old mouse pos to new
vxx2 = mouse_x - old_mouse_x11
vyy2 = mouse_y - old_mouse_y11

//Get distance
dd2 = point_distance(0, 0, vxx2, vyy2)

//Normalize the vector
vxx2 /= dd2
vyy2 /= dd2

//Create temp position starting at the old pos, which will be incremented until it reaches the new pos
pxx11 = old_mouse_x11
pyy11 = old_mouse_y11

//Set old mouse to the current pos
old_mouse_x11 = lxx
old_mouse_y11 = lyy

repeat dd2
{
//increment temp pos by the vector
pxx11 += vxx2
pyy11 += vyy2


lxx=floor(pxx11/sizelassox)
lyy=floor(pyy11/sizelassoy)

line(lxx,lyy,mouse_x,mouse_y)
}

}
 
By the way, I think your main problem is:
Code:
lxx=floor(mouse_x/sizelassox)
lyy=floor(mouse_y/sizelassoy)

line(lxx,lyy,mouse_x,mouse_y)
For one, you should reverse the order of that code, check the line before you update the old mouse variables. Secondly, you are converting the old mouse coordinates to grid coordinates (which you can do more efficiently by mouse_x div sizelassox, rather than flooring the division) but you are not converting the new mouse coordinates to grid coordinates (at least, that's what I think is happening, the code is a bit hard to read).
 

mikix

Member
The problem was
old_mouse_x11 = mouse_x
old_mouse_y11 = mouse_y


It is working now but I need to isolate them somehow to make the fill pixel perfect and not edgy. I marked this as solved since it was about getting it move as fast as the mouse.
 
Top