Help Needed: How to check the angle of an object

omiwebdesignomiwebdesign Member, PRO Posts: 69
edited December 2014 in Working with GS (Mac)

Hey Guys,

I am currently working on a little game where you can win prizes by spinning a wheel. Comparable to Wheel of Furtune. The Wheel in my little game is an image with 8 numbers on it.

Now when i hit a button, I use the Rotate "clockwise" behaviour to rotate the Wheel and a timer to slowly slow down the rotate speed till the Wheel stops.

This all works fine, however the point of doing this is to win a certain prize. So when the number 100 is pointing up, I would like to add 100 points to the total score. Now the question is, how do I calculate the angle of the image to add the correct prize when the image is on an certain angle?

I tried something like this:
When attribute self.rotation = more than 0 and less than 45 add 100.
When attribute self.rotation = more than 45 and less than 90 add 200. etc

But this does not seem to work. It looks like the self.rotation always stays on 0, because when I test if self.rotation = less than 1, it always gives me the prize. When do self.rotation = more than 1 I don't get a prize. So is the rotate behaviour really changing the rotation attribute or should I use an other behaviour to make this work?

Any help is appreciated.

Thanks,
Chris

Comments

  • SocksSocks London, UK.Member Posts: 12,822
    edited December 2014

    @omiwebdesign said:
    I tried something like this:
    When attribute self.rotation = more than 0 and less than 45 add 100.
    When attribute self.rotation = more than 45 and less than 90 add 200. etc

    You need to use equal to or more than 45 and less than 90 . . . . otherwise if the wheel lands on exactly 45 you win nothing.

    @omiwebdesign said:
    But this does not seem to work. It looks like the self.rotation always stays on 0, because when I test if self.rotation = less than 1, it always gives me the prize.

    Yes, GameSalad uses the standard maths convention of angles starting at 0deg (3 o'clock) and increasing in value as you go counterclockwise . . . you are starting at 0deg and moving clockwise, so values start at 0 and then get smaller (going into negative values), so less than 1.

    To see what is happening just stick a display text behaviour on your wheel, get it to display self.rotation so you can see what is going on . . . actually use floor(self.rotation) to get rid of all the decimal places, might make things easier to work out.

  • omiwebdesignomiwebdesign Member, PRO Posts: 69
    edited December 2014

    Thanks for the fast reply. Sorry for not mentioning, however I did use equal to or more.

    @Socks said:
    To see what is happening just stick a display text behaviour on your wheel, get it to display self.rotation so you can see what is going on . . . actually use floor(self.rotation) to get rid of all the decimal places, might make things easier to work out.

    Thanks for the tip. I added a display.text and now it shows the rotation. However there is one big problem. The number continues to go down. (so it isn't going to a maximum of minus 360 "full round" but continues to go down, ending up with numbers in the -2000 (since the wheel is spinning around for a couple of times before it stops). Is there any solution for this problem?

    Thanks @Socks

    EDIT: I just added the rule if self.rotate = equal or more than -360 change self.rotate to 0. Seems to do the trick. I will now try to calculate the prizes. Thanks for the help.

  • SocksSocks London, UK.Member Posts: 12,822
    edited December 2014

    @omiwebdesign said:
    Is there any solution for this problem?

    Sure, there are numerous solutions, it isn't, of course, really a 'problem' (somewhere in another dimension someone who wants their rotation value to continue to decrease as their actor rotates is complaining that it keeps 'resetting' at -360°) :smile:

    A simple solution would be to throw a little maths at the rules that are checking angles, so rather than - for example - saying if the angle is X° you could ask whether the angle is a calculation that extracts a 360° range from the (larger than 360) final value and calculates the remainder . . . hope that makes sense !

    Example, I'm sure someone can come up with something a bit neater . . . ((rotation/360)-floor(rotation/360)) *360 . . . this will calculate how many loops the actor has turned (rotation/360) . . . and then chop off the whole (complete) loops (-floor(rotation/360)) . . . and then calculate the remainder ( *360).

    Or . . . constrain an 'angle' attribute to the rotation, and use that for your prize calculations (an 'angle' attribute will always stay within a range of 0-360) - example file below.

    Or you could use the modulus function to 'loop' the decreasing values around a 0-360 range . . so for example if you final angle was 1530 (which is 4 rotations + an extra 90°) the function would look like this . . . mod(1530,360) . . . and it would return '90' . . . so your rules would be . . . . if mod(self.rotation,360) => 45 and < than 90 . . .etc.

  • SocksSocks London, UK.Member Posts: 12,822

    Here is an example project with a couple of methods, the above calculation (((rotation/360)-floor(rotation/360)) *360) and also a modulus version.

  • gyroscopegyroscope I am here.Member, Sous Chef, PRO Posts: 6,598
    edited December 2014

    @omiwebdesign Hi Chris, great advice from @Socks ...

    But if you want to carry on with the mode of programming you have,

    Then make an angle attribute, let's call it WheelAngle.

    Constrain Attribute Wheelangle to actor.WheelOfFortune.Rotation

    Then similar to what you have:

    I tried something like this: When attribute self.rotation = more than 0 and less than 45 > add 100. When attribute self.rotation = more than 45 and less than 90 add 200. etc

    When attribute WheelAngle > 0 and < 45 then add 100 etc

    ""You are in a maze of twisty passages, all alike." - Zork        temp domain http://spidergriffin.wix.com/alphaghostapps

Sign In or Register to comment.