Mouse Control

For questions about using Classic.

Post » Mon Aug 16, 2010 1:43 am

Hi all,

I've been setting my custom mouse pointer sprite to be at mousex, mousey every tick. I want to instead not let the mouse coordinates define it, but move in response to the mouse being moved - thus allowing me to set a variable sensitivity and effectively control the speed of the mouse. Would be ideal if it used sin/cos to smooth the movement slightly too. Can anyone tell me if there's a way to do this?

Cheers
B
15
S
6
G
6
Posts: 103
Reputation: 2,968

Post » Mon Aug 16, 2010 6:08 am

of course theres a way to do this!

first youll need to find the mousex?, mousey? as i call them

? just meaning change in but thats not important

to find these, your going to need to make 2 new variables, they can be global or object, doesnt matter really, personally id store them in the cursor sprite just to keep things organised

then your going to need your everyday vanilla "always" condition.
make your two variables called MDeltaX and MDeltaY
then set MDeltaX, to mousex, and MDeltaY to mousey
simple enough as it is
now youre going to want to make your event to move the cursor object, ill call the object CursorS since i dont know your cap
Make sure that you remove all events that change the position of this object in anyway in the events following this, or else it might not work
so make a new action of "set position" for CursorS

set X: .x+(mousex-'MDeltaX')*('MouseSpeed')
set y: .y+(mousey-'MDeltay')*('MouseSpeed')

this should be ABOVE the set MDeltaX,MDeltaY actions
MouseSpeed is another variable of CursorS, if you set it to 1, the mouse speed will be normal, 2 will be twice as fast, 0.5 half as fast, etc.

your code should look as follows:

[quote:qyf8c0xp][color=#4040FF:qyf8c0xp][size=150:qyf8c0xp]Always:[/size:qyf8c0xp][/color:qyf8c0xp]
:arrow: [color=#00BF00:qyf8c0xp]CursorS: set position:[/color:qyf8c0xp][quote:qyf8c0xp][color=#008000:qyf8c0xp]set X:[/color:qyf8c0xp] [color=#FF8000:qyf8c0xp].x+(mousex-'MDeltaX')*('MouseSpeed')[/color:qyf8c0xp]
[color=#008000:qyf8c0xp]set Y:[/color:qyf8c0xp] [color=#FF8000:qyf8c0xp].y+(mousey-'MDeltay')*('MouseSpeed')[/color:qyf8c0xp]
[/quote:qyf8c0xp]
:arrow: [color=#00BF00:qyf8c0xp]CursorS: Set 'MDeltaX' to:[/color:qyf8c0xp][quote:qyf8c0xp][color=#FF8000:qyf8c0xp]MouseX[/color:qyf8c0xp][/quote:qyf8c0xp]
:arrow: [color=#00BF00:qyf8c0xp]CursorS: Set 'MDeltaY' to:[/color:qyf8c0xp][quote:qyf8c0xp][color=#FF8000:qyf8c0xp]MouseY[/color:qyf8c0xp][/quote:qyf8c0xp][/quote:qyf8c0xp]

thats it!

As for smoothing... you wouldnt really need cos or sin, but youd have to make 2 more variables and make them "follow" MdeltaX and MdeltaY, so that they can only gradually change over time

its a bit complicated:

youd need 'Mfx' and 'Mfy', and youd need to change the set position event to use those instead of 'MdeltaX/MdeltaY'

then youd add two new actions above the set position event for CursorS

set Mfx to: 'Mfx'+('MdeltaX'-'Mfx')*0.5
set Mfy to: 'Mfy'+('MdeltaY'-'Mfy')*0.5

you could play around with the 0.5, if you made it closer to 0 it would smooth slower, if you made it closer to 1 it would smooth faster

your code should look as follows:

[quote:qyf8c0xp][color=#4040FF:qyf8c0xp][size=150:qyf8c0xp]Always:[/size:qyf8c0xp][/color:qyf8c0xp]
:arrow: [color=#00BF00:qyf8c0xp]CursorS: Set 'Mfx' to:[/color:qyf8c0xp][quote:qyf8c0xp][color=#FF8000:qyf8c0xp]'Mfx'+('MdeltaX'-'Mfx')*0.5[/color:qyf8c0xp][/quote:qyf8c0xp]
:arrow: [color=#00BF00:qyf8c0xp]CursorS: Set 'Mfy' to:[/color:qyf8c0xp][quote:qyf8c0xp][color=#FF8000:qyf8c0xp]'Mfy'+('MdeltaY'-'Mfy')*0.5[/color:qyf8c0xp][/quote:qyf8c0xp]
:arrow: [color=#00BF00:qyf8c0xp]CursorS: set position:[/color:qyf8c0xp][quote:qyf8c0xp][color=#008000:qyf8c0xp]set X:[/color:qyf8c0xp] [color=#FF8000:qyf8c0xp].x+(mousex-'Mfx')*('MouseSpeed')[/color:qyf8c0xp]
[color=#008000:qyf8c0xp]set Y:[/color:qyf8c0xp] [color=#FF8000:qyf8c0xp].y+(mousey-'Mfy')*('MouseSpeed')[/color:qyf8c0xp]
[/quote:qyf8c0xp]
:arrow: [color=#00BF00:qyf8c0xp]CursorS: Set 'MDeltaX' to:[/color:qyf8c0xp][quote:qyf8c0xp][color=#FF8000:qyf8c0xp]MouseX[/color:qyf8c0xp][/quote:qyf8c0xp]
:arrow: [color=#00BF00:qyf8c0xp]CursorS: Set 'MDeltaY' to:[/color:qyf8c0xp][quote:qyf8c0xp][color=#FF8000:qyf8c0xp]MouseY[/color:qyf8c0xp][/quote:qyf8c0xp][/quote:qyf8c0xp]

if any of this doesnt work just ask, i or u might of made a mistake.
B
52
S
7
G
6
Posts: 1,945
Reputation: 7,610

Post » Mon Aug 16, 2010 7:53 am

Cheers for that explanation there Quazi. I have been pondering this question all afternoon, having Aha! moments only to figure out a reason shortly after as to why it wouldn't work. I knew it would come down to finding the change though.

Is there anyway to get the physical mouses movements read? There is an unfortunate limitation that everything is based off the mouse cursors movement, which stops if you hit the edge of your monitor.
B
9
S
2
G
4
Posts: 346
Reputation: 2,726

Post » Mon Aug 16, 2010 8:07 am

oh forgot about that, i believe the mouse and keyboard object can set the mouse cursors position, all you have to do is set it to the center of the monitor after everything in the always and it should work fine
B
52
S
7
G
6
Posts: 1,945
Reputation: 7,610

Post » Mon Aug 16, 2010 6:23 pm

Thanks Quazi, this works brilliantly! Very in depth explanation, thanks for going to the trouble :D

As for what Steven said, the Mouse/Keyboard object can set the X and Y position of the mouse but this limits the movement of the new 'custom' pointer aswell, and causes it to spring back to the origin.
B
15
S
6
G
6
Posts: 103
Reputation: 2,968

Post » Mon Aug 16, 2010 9:06 pm

don't get too excited, cuz it's only maybe, and as for when it wouldn't be too soon
but keep an eye on construct discussion forum from time to time, because I was considering making a mouse plugin that can track mouse movement as separate from cursor movement
it might also just be a part of the next custom controls plugin if and when that arrives
Spriter Dev
B
87
S
21
G
12
Posts: 3,240
Reputation: 16,461

Post » Mon Aug 16, 2010 11:04 pm

dammit overlooked another thing.................. AGGHH!

yea sorry about that, i guess i should test things out before writing them, it was early in the morning and i hadnt though about the boundry and backlash that are causing these problems, heres a perfect version:

http://dl.dropbox.com/u/1010927/Mousemovement.cap
B
52
S
7
G
6
Posts: 1,945
Reputation: 7,610

Post » Mon Aug 16, 2010 11:20 pm

When I ran this Quazi, the sprite was constantly drifting left... also lost any ability to close it, I'm thankful for Alt+F4 haha :P
B
15
S
6
G
6
Posts: 103
Reputation: 2,968

Post » Tue Aug 17, 2010 12:06 am

[quote="untune":1oopgoei]When I ran this Quazi, the sprite was constantly drifting left... also lost any ability to close it, I'm thankful for Alt+F4 haha :P[/quote:1oopgoei]

it because of your version, i forgot about the changes to the window object, and it seems something is bugged about those changes with the windows x position in regards to the windows width, one thing fixed another kinda broken i guess.

anyways, i fixed it so it SHOULD work in newer versions, just re-download it

And alt-f4 or through the application itself is the only way it'll ever be closable, since your effectively highjacking the windows cursor
B
52
S
7
G
6
Posts: 1,945
Reputation: 7,610

Post » Tue Aug 17, 2010 12:27 am

Still drifting, maybe my installation is screwed, somebody else should test it on v92 :)
B
15
S
6
G
6
Posts: 103
Reputation: 2,968

Next

Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 1 guest