Author Topic: Novint Falcon Controller; Raycast from sprite position  (Read 5512 times)

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Novint Falcon Controller; Raycast from sprite position
« on: October 28, 2013, 05:11:39 PM »
Hello,

I was given a Novint Falcon controller to play with in a unity project. Sadly the controller doesnt actually replace the regular mouse so i'm having to roll my own controller.
A sprite is functioning as a cursor that i move around using a delta given by the Falcon. I have also written a button handler but i can't seem to get the raycasting sorted.

Is there an easy way to get a raycast going in NGUI using a sprites position instead of the mouse?

  1. if( Haptics.isButton0Down() && !middleButtonPressed )
  2. {
  3.         middleButtonPressed = true;
  4.         //Raycast
  5.         RaycastHit hit = new RaycastHit();
  6.         if( UICamera.Raycast(  UICamera.mainCamera.WorldToScreenPoint( mTrans.localPosition ) , out hit ) )
  7.         {
  8.                 Debug.Log( hit.collider.name );
  9.                 //SendMessage( "OnClick");
  10.                 //SendMessage( "OnPress", true );
  11.         }
  12. }
  13. else if( !Haptics.isButton0Down() && middleButtonPressed )
  14. {
  15.         middleButtonPressed = false;
  16.         //SendMessage( "OnPress", false );
  17. }

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #1 on: October 29, 2013, 12:37:02 AM »
Look into UICamera.onCustomInput

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #2 on: October 29, 2013, 11:00:38 AM »
Not sure what to do with this. The docs are pretty vague.

The falcon controller doesnt take over the mouse so the only input from that is some delta and a bool. So i just move a sprite around to act as a cursor. But raycasting from it's position doesnt seem to work.

Could you clarify how i can use OnCustomInput?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #3 on: October 29, 2013, 04:45:52 PM »
OnCustomInput feature was added back in 2.3.0 and it lets you add custom functionality to UICamera without actually modifying the class. You just set this delegate to your own function and it will be executed while processing events.

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #4 on: October 29, 2013, 04:49:00 PM »
But i dont think I need to go that far? I mean.. the sprite is already in position correctly.. How can I do the raycast from that position? The above code doesnt appear to work.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #5 on: October 29, 2013, 04:50:26 PM »
Raycast expects a screen position, not a local position. As in, Input.mousePosition for example. Just look at the code and how it's used. First thing that happens with it is "ScreenToViewportPoint". Makes it obvious that it should be a screen coordinate, no?

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #6 on: October 29, 2013, 04:55:53 PM »
If you look at the code I posted I convert the localpostion using WorldToScreenPoint before putting it into the raycast, is this the incorrect way of going about it?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #7 on: October 29, 2013, 04:57:51 PM »
Not quite. You're using localPosition and feed it to WorldToScreenPoint.

transform.position would be world position. localPosition isn't.

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #8 on: October 29, 2013, 05:00:25 PM »
Aaaah right! Nice one. I will report back if I run into anything else. Thanks for spotting that!

Cheers

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #9 on: October 30, 2013, 07:41:12 AM »
It's working now, that's cool. But the currentTouch will be null.. i guess that's why the CustomInput thing exists?

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #10 on: November 04, 2013, 08:12:04 AM »
Hello Aren,

I've been looking at different posts you made on the subject but it's not entirely clear to me how to do it.

The custom cursors moves now with the Falcon control and I send out clicks and the raycasts work fine. But obviously i need have an issue with MouseOrTouch not existing and returning null.

What's the prefered way to handle this? If it's the CustomInput on the camera, could you explain how that is meant to work?

I understand that it's a delegate calling my own methods, but what variables would i need to set on UICamera to get all my bits working correctly? Right now i'm meant to click, drag and drop items.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #11 on: November 04, 2013, 12:12:43 PM »
You'd have your own MouseOrTouch in your custom event handling script. Set UICamera.currentTouch to it before doing event handling, and set it back to null after you're done.

Zophiel

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 54
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #12 on: November 05, 2013, 06:40:31 AM »
Ah I see, I was doing that outside of such a function so it'd be a good idea to wrap it all together in a single method. Cool. I get that.

How about UIcamera.lastHit? Can i just set that or do i need to do anything fancy to stop that from using the mouse?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Novint Falcon Controller; Raycast from sprite position
« Reply #13 on: November 05, 2013, 07:14:36 AM »
UICamera.lastHit is up to you to fill, if you want to fill it.