## Simple mouse control for gun

Post your tutorials here.

### Simple mouse control for gun

Frequently new players asks about mouse control things.
Here it is simple and effective example:

Rating:
Filesize: 18.28 kB
Ratings: 4

Rating:
Filesize: 26.25 kB
Ratings: 4

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.
Last edited by Kilinich on Sun Jan 31, 2010 11:49 am, edited 2 times in total.
Dream of Algodoo as game development engine...

Kilinich
[Best bug reporter 2010]

Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

### Re: Simple mouse control for gun

That's actually pretty sweet, never ceasing to amaze.

Mystery

Posts: 2802
Joined: Thu Sep 03, 2009 1:16 pm
Location: Southern Australia

### Re: Simple mouse control for gun

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

KarateBrot

Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

### Re: Simple mouse control for gun

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

It can be fixed with more code, but my task is KISS (keep it simple, stupid)
Dream of Algodoo as game development engine...

Kilinich
[Best bug reporter 2010]

Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

### Re: Simple mouse control for gun

and that's what i like about it. it's simple.

KarateBrot

Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

### Re: Simple mouse control for gun

that's realy good you know! but i have a question. can jou made something that aim to a circle for example?
ianno

Posts: 42
Joined: Wed Sep 02, 2009 3:06 pm

### Re: Simple mouse control for gun

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.
Dream of Algodoo as game development engine...

Kilinich
[Best bug reporter 2010]

Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

### Re: Simple mouse control for gun

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!
.. ,__,_____
. / __.==--" - - - - - - - - ""
./#(-'
.`-' From http://www.ascii-art.de/. Modded by me to work in Arial.
a Mammoth wrote:be boring and interesting.

Mystery wrote:If you were jailbreaker you shouldn't have when't up the 3.1.3
I didn't know you could go up 3.1.3! Thanks Mystery person!

Dakta

Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm

### Re: Simple mouse control for gun

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.

Mystery

Posts: 2802
Joined: Thu Sep 03, 2009 1:16 pm
Location: Southern Australia

### Re: Simple mouse control for gun

Cool! Thanks!

I don't suffer from insanity. I enjoy every minute of it.

Rrobba
[Best Sig 2010]

Posts: 1480
Joined: Mon Aug 31, 2009 6:01 pm
Location: Gibraltar