Help with your scripting

Get Entity to Follow Mouse

Get Entity to Follow Mouse

Postby Shando » 10 Oct 2017, 06:35

Hi All,

I have made some progress with this, but just can't get the Entity to follow the Mouse with any reasonable precision :(

This is my current code:
Code: Select all
local mY = screen.getMouseY ( )
local mX = screen.getMouseX ( )
local sH = screen.getHeight ( )
local sW = screen.getWidth ( )
local bUpdate = true

if ( mY < 1 or mY > sH or mX < 1 or mX > sW ) then
   bUpdate = false
end

if ( bUpdate ) then
   local xRay, yRay, zRay = sky.raycastToCursor ( )
   local zOrig = zRay
   local camX, camY, camZ = entity.getPosition ( entity.getIDFromTag ( "SCC_Cam" ) )

   xRay = camX - xRay
   yRay = camY - yRay
   zRay = camZ - zRay

   xRay, yRay, zRay = vector3.normalisedCopy ( xRay, yRay, zRay )

   local fMultX = ( mX / sW ) * 75
   local fMultZ = ( mY / sH ) * 75

   xRay = xRay * -fMultX
   zRay = zRay * fMultZ

   if ( zOrig < -100 ) then
      yRay = yRay + 10
   else
      yRay = yRay
   end

   entity.setPosition ( tempID, xRay, yRay, zRay )
end


which sort of works, but not very well.

The issues are many, such as:
    entity offset in X and a bit in Z
    entity swaps to opposite side of mouse at certain points
    entity only goes so far in Z (restricted by fMultX & fMultZ, but changing this bigger/smaller makes things worse)

So, unless anyone has any bright ideas, I might put this aside for later and just implement "click to place" instead.

Thanks for reading

Shando

[UPDATE]
Well blow me down with a feather, I've got it working 8-)

The problem was that I had set a query flag on the board models and this meant that the mouse ray wasn't detecting them at all!! Remove the query flag and voila! works perfectly :D

[UPDATE 2]
Looks like I spoke too soon :( removing the query flag now means I can't detect which model I'm hitting with the ray as isColliding will no longer work :cry:
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Get Entity to Follow Mouse

Postby SolarPortal » 10 Oct 2017, 12:58

only the object your trying to place should have the query flag set to "do not select" and reset back on when you place the model in the scene. This is how you get around the raycasting to self... as for the inaccuracies.. will try your code a bit later and see what results i get..

Note: If your plane is in that scene and you turn it off, does the inaccuracy disappear?
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer

Re: Get Entity to Follow Mouse

Postby Shando » 10 Oct 2017, 22:36

Hi SP,

only the object your trying to place should have the query flag set to "do not select"


Sorry, should have been clearer :oops: I have only set this on the Model being placed. For the other Models, I used:

Code: Select all
entity.setQueryFlag ( tempID, 1 )

and then used this in the isColliding function.

If I delete the Plane, then the raycasts return 0, 0, 0 when not over another model.

Shando

[UPDATE} I wrote a script to check the normal raycastToCursor against the board (as I know where all board "squares" are located - stored in a table), so everything I need with regards to this is working fine now :mrgreen:
Ryzen 7 4800H 16GB GTX1650 Win 11 64
Love, Hope, Strength http://www.lovehopestrength.co.uk
User avatar
Shando
Skyline Moderator
Skyline Moderator
 
Posts: 560
Joined: 06 Mar 2013, 22:35
Location: Moffat Beach, Queensland
Skill: Programmer
Skill: Scripter
Skill: Level Designer

Re: Get Entity to Follow Mouse

Postby SolarPortal » 11 Oct 2017, 10:48

oh, thats great to hear that its working m8 :)
Skyline Game Engine - Lead Dev.
Please provide as much info as possible when asking for help.


Specs: OS: Win 10 64bit, CPU: Intel i7 4770 3.4ghz x 4 core(8 threads), GPU: Nvidia GTX 1060 6GB, Ram: 16gig DDR3, Windows on 250gb Samsung Evo 860

Twitter: @SolarPortal
Instagram: @SolarPortal
User avatar
SolarPortal
Skyline Founder
Skyline Founder
 
Posts: 3631
Joined: 29 Jul 2012, 15:56
Location: UK
Skill: 3D Modeller
Skill: 2D Artist
Skill: Programmer
Skill: Level Designer


Return to Lua Scripting

Who is online

Users browsing this forum: No registered users and 8 guests

cron