0

I am making a Rollette game and I want to play sound in each crossing section like if 1 is crossing it should find some trigger or collision detection and ping one tick sound. My problem is that I am not able to find the collision detection on image. Below is the approaches that I have done.

  1. I have taken LayoutView and placed a Rollette wheel image inside it.
  2. In each section (0-9) has taken a green small button which will be used to detect the collision with the arrow. Once it collides there will be a Tick sound with up-down animation in arrow. (Image attached).

Problem.

  1. I am not able to find the new co-ordinate of views in each rotation. It is returning the same static location every time and hence collision is never happening.

Rotation Code..

    final RotateAnimation rotate = new RotateAnimation(0, 360f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f, RotateAnimation.RELATIVE_TO_SELF, 0.5f);
    rotate.setDuration(10000);
    rotate.setFillAfter(true);

Collision detection code...

    Rect arrowBtnRect = new Rect();
    arrowBtn.getGlobalVisibleRect(arrowBtnRect);


    Rect btn0Rect = new Rect();
    btn0.getGlobalVisibleRect(btn0Rect);

   if(arrowBtnRect.intersect(btn0Rect)) {
       System.out.println("Collision detected "+numberSelected);
       numberSelected = "0";
       return true;
   }

enter image description here

Ajay_Kumar
  • 1,381
  • 10
  • 34
  • 62
  • 1
    I think you have check collisions the other way. Not by button's position, but by the angle of Roulette view after according each button position with it's relative angle to Roulette before starting animation... – khalid3e Sep 11 '20 at 10:44
  • Please elaborate more, I am checking collision with one button which is present in green in each section corner with the arrow button which is in blue. Finding both view rect from the getGlobalVisibleRect function and checking the intersection point. – Ajay_Kumar Sep 11 '20 at 10:58
  • getGlobalVisibleRect gives you the 'static' position of view since it's not really moving. See my posted answer. – khalid3e Sep 11 '20 at 11:38

1 Answers1

1

Your idea is brilliant!

But I have certain remarks on your approach:

  • setFillAfter() is not helping, according to documentations:

If fillAfter is true, the transformation that this animation performed will persist when it is finished.

  • I recommend using ObjectAnimator instead of RotateAnimation. While RotateAnimation is just making the view looks as if it is rotating until you call fillAfter (which happens after animation stops), ObjectAnimator makes the view really change its actual angle while rotating. See this: https://stackoverflow.com/a/29465710/10005752

Usage:

    Random random = new Random(); 
    float randomAngle = (float) (random.nextInt(360) + 1); 
    ObjectAnimator rouletteAnimator = ObjectAnimator.ofFloat(rouletteView, View.ROTATION, 0f, randomAngle);
    rouletteAnimator.setDuration(10000);
    rouletteAnimator.start();

Also, to elaborate more on my comment I recommend that get all angles that have buttons on them before starting animating, and when animation starts keep using rouletteView.getRotation(); to check what angle is at the top.

Example:

Roulette example

Button1 is at angle 90 and rouletteView is rotating Counter Clockwise. Then if rouletteView.getRotation() == 360 - 90 is true then Button1 is touching the arrow.

I hope I'm clear!

khalid3e
  • 399
  • 3
  • 14
  • Thanks for reply... will try this logic.. could you please tell me how will I make a random rotation of the wheel in ObjectAnimator because every time it is stopping on the same number. – Ajay_Kumar Sep 11 '20 at 11:59
  • I am no sure I understand what you mean by "random" rotation, do you need a random number to use as an degree/angle? Or a random number of rotations? Or something else? – khalid3e Sep 11 '20 at 13:18
  • I want. to rotate the wheel and it should stop on any random number of sections. – Ajay_Kumar Sep 11 '20 at 14:24
  • See my answer now, I added a sample code with `ObjectAnimator` that does the same as your previous `RotateAnimation` code. – khalid3e Sep 11 '20 at 16:04
  • Your reference code is always stoping on the initial position of the image, example on every time it is stoping on 0th sec. I think because of 360 deg rotation. I want it to stop on randomly. It should rotate 360 but should stop at random. – Ajay_Kumar Sep 11 '20 at 16:09
  • I added a code of CountDownTimer timer and when timer is complete it is calling rotate.cancel() method to stop the rotation. – Ajay_Kumar Sep 11 '20 at 16:12
  • 1
    Hi Ajay, see my latest edit, I added a Random number that will be between 1 to 360. – khalid3e Sep 11 '20 at 20:10
  • Hi Khalid.. thanks for answer.. I implemented the logic and I getting the values but some time because of below error calculated value is not reflecting.. "Skipped 96 frames! The application may be doing too much work on its main thread" I implemented this approx logic to get the current sector and collision --- rouletteView.getRotation() == 360 - 90 .. – Ajay_Kumar Sep 11 '20 at 21:20