Page 1 of 1

Simple mouse control for gun

PostPosted: Sun Jan 31, 2010 11:28 am
by Kilinich
Frequently new players asks about mouse control things.
Here it is simple and effective example:

Rating: rated 6.5
Filesize: 18.28 kB
Comments: 1
Ratings: 4
download

Rating: rated 6.5
Filesize: 26.25 kB
Comments: 1
Ratings: 4
download

How to build:

You need one global variable - scene.my.mspd (motor speed) - type scene.my.mspd = 0 in console.
Now put and fix box on gun at hinge center and hit it with laser (or collide in phun).
All script is in onHitByLaser (onCollide in phun) event of that box.
Set hinge motor "on" and set motorSpeed = {scene.my.mspd}.

Now is most interesting part - script. To determine direction of rotation we need position of gun (e.pos), mouse position (app.mousepos) and current normal vector (e.normal). Idea is to find is mouse on the left side from gun or on the right. It's pretty simple - you need to multiply normal vector on normalized vector from gun to mouse - result is projection (positive or negative) indicating which side to turn.

R = app.mousepos - e.pos (vector from gun to mouse)
Rn = R / |R| (normalized vector with length = 1, |R| - length of R vector)
P = e.normal x R (projection of R on e.normal)
So if P is positive - turn right, negative - left. P can be in -1..+1 interval so it indicates speed of turn too.

Finally, formula:
Code: Select all
scene.my.mspd = - 7 * (e.normal(0) * (app.mousepos(0) - e.pos(0)) + e.normal(1) * (app.mousepos(1) - e.pos(1))) / (((app.mousepos(0) - e.pos(0)) ^ 2 + (app.mousepos(1) - e.pos(1)) ^ 2) ^ 0.5)

where - 7 is speed of rotation multiplier (tune it to rotate faster without jitter).

So put formula in onHitBylaser (onCollide in phun) and that's it. Mobile, fast, mouse-controlled gun.

Re: Simple mouse control for gun

PostPosted: Sun Jan 31, 2010 11:33 am
by Mystery
That's actually pretty sweet, never ceasing to amaze.

Re: Simple mouse control for gun

PostPosted: Sun Jan 31, 2010 11:48 am
by KarateBrot
i don't like the fact that it starts spinning very slow if it has to turn nearly 180°. that's why i prefer to calculate the difference of angles between the vector e.normal and (app.mousepos - e.pos). but nevertheless it's cool because this code can be copy pasted quickly and easily :thumbup:

Re: Simple mouse control for gun

PostPosted: Sun Jan 31, 2010 11:59 am
by Kilinich
KarateBrot wrote:i don't like the fact that it starts spinning very slow if it has to turn nearly 180°. that's why i prefer to calculate the difference of angles between the vector e.normal and (app.mousepos - e.pos). but nevertheless it's cool because this code can be copy pasted quickly and easily :thumbup:

It can be fixed with more code, but my task is KISS (keep it simple, stupid) :D

Re: Simple mouse control for gun

PostPosted: Sun Jan 31, 2010 12:36 pm
by KarateBrot
:D
and that's what i like about it. it's simple.

Re: Simple mouse control for gun

PostPosted: Wed Mar 17, 2010 11:44 am
by ianno
that's realy good you know! but i have a question. can jou made something that aim to a circle for example?

Re: Simple mouse control for gun

PostPosted: Wed Mar 17, 2010 11:50 am
by Kilinich
ianno wrote:that's realy good you know! but i have a question. can jou made something that aim to a circle for example?


Put laser probe on circle, put position to some scene.my. variable in use it instead of app.mousepos in my code.

Re: Simple mouse control for gun

PostPosted: Wed Mar 17, 2010 6:51 pm
by Dakta
Mystery wrote:That's actually pretty sweet, never ceasing to amaze.


Says the guy who poo-poo'd scripting in another thread not too long ago.

@kilinich: Very nicely done! I really appreciate it when people post up these sorts of simple, to the point examples for people to learn from. Great job!

Re: Simple mouse control for gun

PostPosted: Thu Mar 18, 2010 9:49 am
by Mystery
Dakta wrote:
Mystery wrote:That's actually pretty sweet, never ceasing to amaze.


Says the guy who poo-poo'd scripting in another thread not too long ago.


I "Poo-Poo'd" Scientology but i'm still amazed how well they have conned people into giving them money.

And i still "Poo-Poo" Thyme, can you honestly not say phun was better without thyme when we were making rockets the good old fashion way.

Re: Simple mouse control for gun

PostPosted: Fri Mar 26, 2010 10:36 pm
by Rrobba
Cool! Thanks!