Simple mouse control for gun

Post your tutorials here.

Simple mouse control for gun

Postby Kilinich » Sun Jan 31, 2010 11:28 am

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.
Last edited by Kilinich on Sun Jan 31, 2010 11:49 am, edited 2 times in total.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Simple mouse control for gun

Postby Mystery » Sun Jan 31, 2010 11:33 am

That's actually pretty sweet, never ceasing to amaze.
User avatar
Mystery
 
Posts: 2802
Joined: Thu Sep 03, 2009 1:16 pm
Location: Southern Australia

Re: Simple mouse control for gun

Postby KarateBrot » Sun Jan 31, 2010 11:48 am

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:
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Simple mouse control for gun

Postby Kilinich » Sun Jan 31, 2010 11:59 am

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
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Simple mouse control for gun

Postby KarateBrot » Sun Jan 31, 2010 12:36 pm

:D
and that's what i like about it. it's simple.
Image
User avatar
KarateBrot
 
Posts: 825
Joined: Mon Aug 31, 2009 7:32 pm
Location: Germany

Re: Simple mouse control for gun

Postby ianno » Wed Mar 17, 2010 11:44 am

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

Postby Kilinich » Wed Mar 17, 2010 11:50 am

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...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Simple mouse control for gun

Postby Dakta » Wed Mar 17, 2010 6:51 pm

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. Image
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!
User avatar
Dakta
 
Posts: 417
Joined: Sat Sep 12, 2009 4:36 pm

Re: Simple mouse control for gun

Postby Mystery » Thu Mar 18, 2010 9:49 am

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.
User avatar
Mystery
 
Posts: 2802
Joined: Thu Sep 03, 2009 1:16 pm
Location: Southern Australia

Re: Simple mouse control for gun

Postby Rrobba » Fri Mar 26, 2010 10:36 pm

Cool! Thanks!
Image
I don't suffer from insanity. I enjoy every minute of it.
User avatar
Rrobba
[Best Sig 2010]
 
Posts: 1480
Joined: Mon Aug 31, 2009 6:01 pm
Location: Gibraltar


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests

cron