The Daily Click ::. Forums ::. Klik Coding Help ::. Smooth dragging
 

Post Reply  Post Oekaki 
 

Posted By Message

-J-



Registered
  05/10/2008
Points
  228

VIP MemberThe Cake is a Lie
29th August, 2009 at 06:25:58 -

Ok, I've got some objects that can be dragged with the mouse. The user clicks and holds onto them; while this is happening, one of the object's flags is set to ON, indicating that it's being dragged. While the flag is on, the object is simply set to stay at the mouse's position, (with an offset, depending on where the mouse was when the user clicked on the object, this makes it so the object doesn't snap to the mouse when clicked).

This all works really well, but I'm trying to make the movement a lot smoother. It's kind of like "smooth scrolling" vs "always > centre display on player", I don't like how it "sticks" to the mouse, so I'm trying to make it follow the mouse smoothly, almost as if it were connected to the mouse by an elastic string (if you can imagine that).

So rather than just having:

Internal flag 0 = ON
>Set X position to Xmouse
>Set Y position to Ymouse

I need a way to move the object smoothly towards the mouse.

I've tried using a linear movement: P1 + (P2 - P1) * t

Making Point 1 the object's position, and point 2 the mouse's position, and then keeping t at 0.3, which gives the kind of smooth effect I'm after but the problem with using that method is that it never quite gets to the destination completely, it always seems to stop short of reaching the mouse.

I'd greatly appreciate any help with this, maybe an alternate method for smooth movement? I'm stuck on this one

 
n/a ...

nim



Registered
  17/05/2002
Points
  7233
29th August, 2009 at 07:25:05 -

lolnevermind
Heh, sorry, I replied with the same method you posted.

Another method would be simply to use a bouncing ball movement (always > look at object at xmouse,ymouse) and set its speed to sqr((x-x)²+(y-y)²)

About your current method:

Internal Flag 0 = ON
- [Active] Set X to XMouse-(XMouse-X( "Active" ))/1.2
- [Active] Set Y to YMouse-(YMouse-Y( "Active" ))/1.2

I like this method so I've used it in the past with just a little tweaking, like pushing the active 1 pixel up/down/left/right depending on where the mouse is.

Edited by nim

 
//

MBK



Registered
  07/06/2007
Points
  1578

VIP Member
29th August, 2009 at 18:00:34 -

You could have the mouse follower active always look towards whatever the mouse cursor is currently over.
Just set a flag for the object to be on when the mouse is over something, and off when it is not over it.
(you'll need as many events as actives you want to be seen when scrolled over)
And when the flag is on also set the mouse follower active's speed to what you want it to go.

If you want something that constantly follows the mouse no matter what it is over but that just doesn't stick to it, couldn't you set it to look towards the mouse cursor's position, and set its speed and then just set it to stop when it is over the mouse cursor position?

Snap to Grid Part:
When you need it to snap to a grid or an active section or whatever you could set it to move toward the position (once you click for it to snap) at whatever speed you want and if it gets so close have it slow down slightly as it smoothly goes into place.

Edit:
Just have so it doesn't stop based on collisions, but instead base it on positional testing.
If the position matches up, have it stop, if not then keep it moving until it does match up.


Edited by MBK

 
Click Me! http://www.create-games.com/project.asp?view=main&id=1444

http://www.mediafire.com/download.php?aoo1dnnlq5i

Blood of the Ancient One, Seen only as Shadow, Faster than Lightning, Fierce as the Greatest Dragon, Nearly Invisible, Floating in a Dream, Entered through the Demon Door, Destroyer of Evil in a Realm with a Red Sky Scarred, Who could I be ?

nim



Registered
  17/05/2002
Points
  7233
29th August, 2009 at 18:42:01 -


Originally Posted by nim
Another method would be simply to use a bouncing ball movement (always > look at object at xmouse,ymouse) and set its speed to sqr((x-x)²+(y-y)²)



Just an extra note about that method. I used it in a game once, so you can see it in action and see if it's the effect you're looking for.

http://lowflyingcow.googlepages.com/wire.zip

Edit: Here's an example mfa: http://lowflyingcow.googlepages.com/follow_example.mfa

Edited by nim

 
//

UrbanMonk

BRING BACK MITCH

Registered
  07/07/2008
Points
  49567

Has Donated, Thank You!Little Pirate!ARGH SignKliktober Special Award TagPicture Me This Round 33 Winner!The Outlaw!VIP MemberHasslevania 2!I am an April FoolKitty
Picture Me This Round 32 Winner!Picture Me This Round 42 Winner!Picture Me This Round 44 Winner!Picture Me This Round 53 Winner!
29th August, 2009 at 18:47:06 -

Ohh, I like that game nim! How come I've never seen it before?
It would be a great online arcade game.

Edited by UrbanMonk

 
n/a

nim



Registered
  17/05/2002
Points
  7233
29th August, 2009 at 18:53:51 -

It was made for the March Mouse Control competition. Check out the other games!
http://lowflyingcow.googlepages.com/mousecomp.htm

 
//

-J-



Registered
  05/10/2008
Points
  228

VIP MemberThe Cake is a Lie
30th August, 2009 at 03:29:31 -

Thanks for everyone's help.

I've realised that for my game to work, I actually need the draggable objects to be stuck to a grid with 20px * 20px squares. Still keeping the smooth movement idea in my mind, I came up with this:

http://sites.google.com/site/julianmsmithies/Home/image-1/Drag_Example.mfa?attredirects=0

That's the effect I'm after now, but it still bears the same problem. If you play around with it you'll notice that when dragging Right or Down, the block never seems to lock quite in place.

It's basically the same method I was using before, but I've added the grid.
I took the time to heavily comment that example so hopefully you can make some sense out of my otherwise chaotic code.

On line 15, changing value I to a number higher than 0.4 will make the movement more accurate, but also less smooth. (Setting it to 1 will just be like snap-to-grid)

Also, the boundary stuff isn't really important, as it's just the movement I need to fix, I can adjust it later to suit.

Any help would be greatly appreciated!

 
n/a ...

Spitznagl

Cabinetmaker

Registered
  06/12/2008
Points
  4260

The SpinsterVIP MemberHas Donated, Thank You!Picture Me This Round 29 Winner!Picture Me This Round 31 Winner!Picture Me This Round 36 Winner!
30th August, 2009 at 21:31:17 -

Smooth grid-dragging wich support multiple instances->
http://www.mediafire.com/file/oeztzcrezf0/UltimateSmoothDragTest.mfa

That should be what you're looking for.


 
...

nim



Registered
  17/05/2002
Points
  7233
9th October, 2009 at 01:09:11 -

Great example, Spitznagl Thanks

 
//
   

Post Reply



 



Advertisement

Worth A Click