• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

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

  • Thread starter Deleted member 16767
  • Start date
D

Deleted member 16767

Guest
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.
 
D

Deleted member 16767

Guest
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.
 
D

Deleted member 16767

Guest
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 by a moderator:
D

Deleted member 16767

Guest
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).
 
D

Deleted member 16767

Guest
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