0

I'm trying to make whack a mole game using project tango.

When user start the game, the program will create holes at random point for the moles to come out. Right now, I already can make the hole and spawn the mole at random, though I have a problem with the created hole.

The hole sometimes spawn at an edge contour of table and stuff and make the hole partially floating in the air. Is there any way to check if the centerPlane is near an edge or not?

Here's the screenshot of the problem I meant. I want the "hole" not to spawn on area that doesn't fit with it's height and width. Currently, I'm using farandole release.

enter image description here

EDIT 1:

I'm trying to do as Hristo suggest. But it doesn't work, the FindClosestPoint always return -1, even when I use the center of the screen. Here's the script I used. And for some additional info, I'm using the unitySDK and unity 5.5.2f1

bool CheckTheCorner(Camera cam,Vector3 planeCenter){
    Vector2 firstPointInScreen = WorldToScreenConverter(cam,new Vector3 (planeCenter.x,planeCenter.y,planeCenter.z-holeheight));
    Vector2 secondPointInScreen = WorldToScreenConverter(cam,new Vector3 (planeCenter.x,planeCenter.y,planeCenter.z+holeheight));
    Vector2 thirdPointInScreen = WorldToScreenConverter(cam,new Vector3 (planeCenter.x-holewidth,planeCenter.y,planeCenter.z));
    Vector2 fourthPointInScreen = WorldToScreenConverter(cam,new Vector3 (planeCenter.x+holewidth,planeCenter.y,planeCenter.z));

    DebugText.text = m_pointCloud.FindClosestPoint (cam, new Vector2(Screen.width / 2, Screen.height / 2), 1).ToString ();

    Vector3 firstPoint = m_pointCloud.m_points[m_pointCloud.FindClosestPoint(cam, firstPointInScreen, 1)];
    Vector3 secondPoint = m_pointCloud.m_points[m_pointCloud.FindClosestPoint(cam, secondPointInScreen, 1)];
    Vector3 thirdPoint = m_pointCloud.m_points[m_pointCloud.FindClosestPoint(cam, thirdPointInScreen, 1)];
    Vector3 fourthPoint = m_pointCloud.m_points[m_pointCloud.FindClosestPoint(cam, fourthPointInScreen, 1)];
    return false;
}

Vector2 WorldToScreenConverter(Camera cam,Vector3 worldPos){
    Vector3 screenPos = cam.WorldToScreenPoint (worldPos);
    return new Vector2 (screenPos.x,screenPos.z);
}

Ah yes, don't mind the return false one for the moment, I just put it there to avoid error since I'm still figuring out the FindClosestPoint.

1 Answers1

0

What you can do is take the 4 corners on your plane and decide if they are all laying on a surface in the real world, if not you can make the plane elsewhere.

That can happen with the use of FindClosestPoint() method in the TangoPointCloud.cs. What that method does is makes a Raycast from your camera trough a certain point on the screen landing in the real world environment. The method then returns the index of that point. The list to search with the indexes is called m_points


So lets split it in steps:

  1. Make 4 Vectors using the `FindClosestPoint().
  2. Check if all 4 vectors are on the same plane (simple math).
  3. If step 2 is true -> Instantiate your GameObject on that plane.

To get one of the vectors your code should be something like this:

Vector3 firstPoint = m_pointCloud.m_points[m_pointCloud.FindClosestPoint(Camera.main, new Vector2(Screen.width / 2, Screen.height / 2), 1)];

In this example I'm using the center of the screen as my Vector2 parameter. However you don't want the center of the screen . Instead you want the position of one corner from your plane translated as a screen point.

Hope that solves your problem. Cheers.

Hristo
  • 1,805
  • 12
  • 21
  • If it doesn't work, please provide script. Thanks :) – Hristo Mar 17 '17 at 09:51
  • It doesn't work. the findclosestpoint always return -1. See my edit version of post for the script – Tommy Hart'no Mar 18 '17 at 07:23
  • Hey Tommy, do you get -1 in the editor or when debugging with the tango phone? Did you try building an apk and testing the values on the phone, your code seems to be okay. – Hristo Mar 18 '17 at 09:49
  • In editor and in phone. The debugtext in the code is for testing in phone. I already testing the point too, and they seem ok (still within the screen height and width). But it still return -1. – Tommy Hart'no Mar 18 '17 at 10:08
  • I think there is a problem with the PointCloud. Can you please share the full script or even the project, I will gladly take a look at it. I'm using the same functionality and there is no problem for me. Thanks! – Hristo Mar 18 '17 at 10:21
  • Here you go. This is the link of the project. Thank in advance https://drive.google.com/open?id=0B6sQD3ioh0efeUdaeFBDMHFmWU0 – Tommy Hart'no Mar 18 '17 at 10:43
  • Hey Tommy, since _Pointcloud, color camera or ADF feature are not available through the editor mode. To test these features, you have to push it to the device._ I can't properly test. My phone is at work so I can't build as well. What i deducted is the `m_pointsCount` = 0 in the `TangoPointCloud.cs` in the **Unity editor** but this value should NOT be null on the Tango device. I will check your project again on Monday and reply to you here with the solution. Thanks! – Hristo Mar 18 '17 at 11:29
  • Yes, when I debug it, there's about 30k (or is it 300k?) point (I'm using m_pointCount). But it's return -1. Is it possible there's bug in the unity version? – Tommy Hart'no Mar 20 '17 at 01:48
  • Sorry, there's other project on deadline. Hmm, after I tested it some more (with some code tweaking to return an array of int). I found that the code did sometimes return a result but only for a split second, Is there's anything you change in the code to make it work or any different unity version or tango sdk? – Tommy Hart'no Mar 22 '17 at 13:16
  • Hey Tommy, I did no changes to the project. The tango sdk version is **TangoSDK_Eisa_Unity5** and my Unity version is 5.5.1f i believe. What do you exactly want to achieve? Do you want to redraw the plane/hole every frame or you want to make it upon button click? – Hristo Mar 22 '17 at 17:21
  • What I want to achieve is when the user click the start button. The hole will be made once according to the maxhole assigned on the gamecontroller scripts and at random place. If the place is not "possible" to create the hole it will check again until it created n number of hole according to the maxhole. – Tommy Hart'no Mar 23 '17 at 01:57