void placeBottle() { Random r = new Random(); float newXVal = 0.0f; Vector3 newPosition = Vector3.Zero; float playerLowerBound = 0.0f; float playerUpperBound = 0.0f; if (skeletons != null) { for (int i = 0; i < skeletons.Length; i++) { Skeleton skeleton = skeletons[i]; // Draw debug skeleton dots dots on the screen if player is being tracked by Kinect if (skeleton != null && skeleton.TrackingState != SkeletonTrackingState.NotTracked) { foreach (Joint joint in skeleton.Joints) { if (joint.TrackingState == JointTrackingState.Tracked) { if (joint.Position.X < playerLowerBound) playerLowerBound = joint.Position.X; else if (joint.Position.X > playerUpperBound) playerUpperBound = joint.Position.X; } } } } } playerUpperBound = Math.Min(playerUpperBound, 0.9f); playerLowerBound = Math.Max(playerLowerBound, -0.9f); if (Math.Abs(playerUpperBound) > Math.Abs(playerLowerBound)) newXVal = r.Next(-130, (int)(130.0f * playerLowerBound)); else newXVal = r.Next((int)(130.0f * playerUpperBound), 130); float newyVal = r.Next(-63, 63); /* modelHit = false; modelPosition = modelPositionOrig; modelRotation = 0.0f; */ GameObject bottleNew = new GameObject(myModel); GameObject bottlePieceTop = new GameObject(modelPieceTop, true); GameObject bottlePieceMiddle = new GameObject(modelPieceMiddle, true); GameObject bottlePieceBottom = new GameObject(modelPieceBottom, true); bottleNew.Position = new Vector3(newXVal, newyVal, 0.0f);//modelPositionOrig; bottlePieceTop.Position = bottleNew.Position; bottlePieceMiddle.Position = bottleNew.Position; bottlePieceBottom.Position = bottleNew.Position; bottleNew.Piece1 = bottlePieceTop; bottleNew.Piece2 = bottlePieceMiddle; bottleNew.Piece3 = bottlePieceBottom; bottles.Add(bottleNew); bottles.Add(bottleNew.Piece1); bottles.Add(bottleNew.Piece2); bottles.Add(bottleNew.Piece3); }
void hitBottle(GameObject bottle) { float shortestDist = 9999999.9f; Joint closestJoint = new Joint(); int closestSkeletonId = 0; Vector2 bottleMidPt = new Vector2((bottle.Hitpoints2D[0].X + bottle.Hitpoints2D[1].X) / 2.0f, (bottle.Hitpoints2D[0].Y + bottle.Hitpoints2D[1].Y) / 2.0f); for(int i=0; i<skeletons.Length; i++) { Skeleton skeleton = skeletons[i]; // Draw debug skeleton dots dots on the screen if player is being tracked by Kinect if (skeleton != null && skeleton.TrackingState != SkeletonTrackingState.NotTracked) { foreach (Joint joint in skeleton.Joints) { if (joint.TrackingState != JointTrackingState.NotTracked) { Vector2 position = SkeletonToDepthMap(joint.Position); position.X *= 2.0f * 1920.0f / 640.0f; //WINDOW_WIDTH position.Y *= 2.0f * 1080.0f / 480.0f; //WINDOW_HEIGHT float dist = (position - bottleMidPt).Length(); if (dist < shortestDist) { shortestDist = dist; closestJoint = joint; closestSkeletonId = i; } } } } } SkeletonPoint curPoint = skeletons[closestSkeletonId].Joints[closestJoint.JointType].Position; SkeletonPoint lastPoint = (skeletonsLastFrame[closestSkeletonId].Joints[closestJoint.JointType].Position.X != curPoint.X) ? skeletonsLastFrame[closestSkeletonId].Joints[closestJoint.JointType].Position : skeletonsLastFrame2[closestSkeletonId].Joints[closestJoint.JointType].Position; Vector2 newDir = (SkeletonToDepthMap(curPoint) - SkeletonToDepthMap(lastPoint)); //Vector3 newDirection = new Vector3(curPoint.X - lastPoint.X, curPoint.Y - lastPoint.Y, 0.0f); if (newDir.Length() <= 0) newDir.X = -1.0f; Vector3 newDirection = new Vector3(newDir.X, -newDir.Y, 0.0f); newDirection.Normalize(); bottle.IsHit = true; bottle.Direction = newDirection;// modelDirection; bottle.RotationDir = new Vector3(2.0f, 2.0f, 2.0f); Random r = new Random(); float randomDirModifier = ((float)(r.Next(50, 150))) / 100.0f; bottle.Piece1.Direction = newDirection * randomDirModifier; bottle.Piece1.RotationDir = new Vector3(r.Next(-8, 8), r.Next(-8, 8), r.Next(-8, 8)); bottle.Piece1.IsHit = true; randomDirModifier = ((float)(r.Next(50, 150))) / 100.0f; bottle.Piece2.Direction = newDirection * randomDirModifier; bottle.Piece2.RotationDir = new Vector3(r.Next(-8, 8), r.Next(-8, 8), r.Next(-8, 8)); bottle.Piece2.IsHit = true; randomDirModifier = ((float)(r.Next(50, 150))) / 100.0f; bottle.Piece3.Direction = newDirection * randomDirModifier; bottle.Piece3.RotationDir = new Vector3(r.Next(-8, 8), r.Next(-8, 8), r.Next(-8, 8)); bottle.Piece3.IsHit = true; bBottleResetTimerOn = true; bottleResetTimer = new TimeSpan(0, 0, 2); currentScore++; }