We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.

Dragging Object To “Snap” to Grid

Home Forums Puzzles Dragging Object To “Snap” to Grid

Tagged: 

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #28609
    GLiFTeK
    Customer

    Hi,
    I’m just brainstorming the best way to have my draggable object “Snap” to a grid.

    Was first inclined to have a grid of empties, put them in a list, and loop through to find whichever is closest to the dragged object,(besides the previous location’s empty) then snap to its coords.

    Is there a less complex method of doing this anyone might know of, possibly with a short equation?

    Thanks!

    #28684
    GLiFTeK
    Customer

    Well the array of empties was way too large of a list (1000+) to loop through every frame…

    I’m tracking the dragged object’s coords, comparing them to its previous position coords.

    If if that difference between those 2 x/y coords land within a buffer value k area +/- around the next virtual grid space, then the dragged object snaps to those coords.

    Wish I retained all that trigonometry grin school. Probably would be simpler using x/y vector comparisons with those new numbers puzzles features.

    :wacko:

    Gonna try to drag a “ghost” empty that gets resolved to tell the stationary object to snap…

    #28710
    GLiFTeK
    Customer

    conclusion:
    meh.. screw all that noise..

    just found a JS script that did the job.
    did it once on the X once on the Y. job done.

    snapToGrip = function(val,gridSize){
        var snap_candidate = gridSize * Math.round(val/gridSize);
        if (Math.abs(val-snap_candidate) < 2) {
            return snap_candidate;
        }
        else {
            return null;
        }
    };

    :good:

    #28716

    Yes, it’s a good way. I think this can be implemented with puzzles, but I’ve failed to do this few times then leave this attempts :wacko: .

    Co-founder and lead graphics specialist at Soft8Soft.

    #28745
    GLiFTeK
    Customer
    #28850
    yasse
    Customer

    Hello dear GlifTek.

    I Hope you doing good :) please i have been playing with the snap to grid function but it didn’t worked as it was supposed to be, but I’m wondering if it worked for you ?

    please could you check the screenshot of the execution which certainly a mistake I have made,

    thank you so much :)

    #28868
    GLiFTeK
    Customer

    Hello dear GlifTek.
    I Hope you doing good :) please i have been playing with the snap to grid function but it didn’t worked as it was supposed to be, but I’m wondering if it worked for you ?
    please could you check the screenshot of the execution which certainly a mistake I have made,
    thank you so much :)

    Hi Yasse.

    First thing is that your JS Call puzzle should go AFTER your move puzzle, because it’s snapping your object to what info you give it, which would be the coords it gets after you dragged something, which i’ll show here…

    So here’s what my basic setup is:
    This is taking the x and y coordinates of your dragged object and then processing them through the script according to your grid size and whether snapping is on/off. (holding down the “z” key. (maybe make it a different key since that messes with undo sometimes)

    My puzzles are like this:

    .

    And I’m using this in the (yourApp).js file’s prepareExternalInterface portion:

        app.ExternalInterface.snapToGrid = function(val,gridSize){
            var snap_candidate = gridSize * Math.round(val/gridSize);
            if (Math.abs(val-snap_candidate) < 2) {
                return snap_candidate;
            }
            else {
                return null;
            }
        };

    Where I got that script from was using “griP” with a “p” so i made it a “d”. (you still have that “p” and that might cause unseen issues if you use “d” in the JS Call puzzle’s name, which you didn’t but I am, so just pointing that out.)

    Note that i just copied the puzzles over to a different part of my screen to show you their basic arrangement, not all intertwined into my project, so they may be a bit more understandable, directly copying them may not work per your 3d app file etc.. but it’s a basic layout.

    I found I had to use ’round’ twice, though i most likely will go over that and find away to make it back to only once. (was issues with how fast the user would interact)

    I’ll post a zip file in tutorials with it all setup and working in it’s own app folder for people to make their own changes on in a few days, with some other very useful “database query”, “print to console toggle” and “dictionary factory” puzzle setups I’ve been meaning to share.

    I hope this helps! :good:
    -GlifTek

    #28869
    GLiFTeK
    Customer

    i’m not gonna dare edit that post because it might get erased.

    so EDIT: also, you’d just put your own object in the “when dragged over” and “drag move” puzzles,

    then the get x/y position puzzles
    and the set position puzzle in the other bottom procedures.

    when i post the zip in tutorials it will all be simplified. just thought i’d get back to you on this quick so you can move forward.

    :good:

    #37423
    Petro
    Customer

    Hi GlifTek. Thank you for sharing. Did you post the zip file you mentioned ?

    #37426
    GLiFTeK
    Customer

    Hi GlifTek. Thank you for sharing. Did you post the zip file you mentioned ?

    Hmm that was awhile ago.
    Can’t find that specific zip..
    Should have posted it back then, but got sidetracked with other work.

    However, I’m making it into a custom puzzle for sale I’ll be premiering in a few days.

    In the meantime, what part of the script are you having trouble with? Maybe I can help you to get it to work.
    (Try using it in Ann exec script puzzle?) :good:

    EDIT: odd that my puzzles image above is going. Maybe I can find that.

    #38204
    Petro
    Customer

    :) I did manage to make work many aspects… I am configuring furniture objects in a kitchen by dragging them… The most annoying part is it sometimes you need to insist to touch the wall edge with the dragging. I am thinking of a way to place as you suggested somewhere else some invisible spots on the floor where the object can be placed. Thx

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.