C++/Math: How to get the closest number that would 'hit' 0.

I have a game that uses filmstrips as frames for ship rotation. Let us say there are 180 'ship images' in one film strip. (aka 360 degrees at 2 degrees per frame).. Let us also say that FRAME 0 would be the ship nose pointing upwards.

I've recently changed it so that ship can be 'faster' then the amount of frames, which in turn works fine, but I want it to start on a angle that would actually HIT frame 0. What I mean is:

If I have a ship thats rotation speed of is at 1, it would have 180 rotations (or 180 frames)..  Lets say I change ship while my original ship is sitting on is on frame 7. Let us say the new ship has a rotation speed of 0.5, or 90 rotations out of the 180 rotations it has. So this would mean its skipping a frame per rotation, aka two rotations per frame. Since the frame is preserved at frame 7, it would NEVER hit frame 0.

I was thinking of having it adjust itself to the nearest even number, so it would be wither 6 or 8 in this scenario, but this won't work becuase if the rotation rate i 0.25, or 45 frames, this would mean that it could potentially be a even OR odd number, aka skipping 3 frames now, 0 3 6 9 12 15 .. etc.

I didn't want to reset it to 0 all the time because it looks weird when it is reset completely, of course.

===

I apologies if it doesn't make sense, what I'm trying to state is I want the closest lowest number depending on the multipler, that would actually 'hit' the number '0'. Here would be a simple math example I suppose:

So if I had a number of 7 and a multiplier of 3, the number it would move too would be either 6 (0 3 6)...
If I had a number of 15, and a multiplier of 4, the number it would move too would be 12. (0 4 8 12)..
If I had a number of 6, and a multiplier of 2, the number wouldn't change of course, since its already on a number that works to become 0. (0 2 4 6)...
If had a number of 3 and a multipier of 5, the number would end up being 0. etc


I hope this explains it a bit!
LVL 7
VallerianiAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
TommySzalapskiConnect With a Mentor Commented:
To get to the closest number use

multiplier = (int)(1.0/rotation_speed + .5) // rounds

if(initial_position%multiplier > multiplier/2)
  new_position = initial_position - initial_position%multiplier + multiplier
else
  new_position = initial_position - initial_position%multiplier

Open in new window

You have a big problem however if the multiplier is not a factor of 180 (7,8,11,etc)

If that's the case, then you can't pick a number because it will depend on the direction of rotation. You could just do something like this though. While it's being rotated, if it gets as close to 0 as it's going to, make it 0.

if abs(current_position) <= multiplier/2
  current_position = 0

Open in new window

0
 
Infinity08Connect With a Mentor Commented:
Something like this ?

        int multiplier = 4;
        int initial_position = 7;
       
        int new_position = multiplier * (initial_position / multiplier);

Note however that if the multiplier does not evenly divide 180, this might not work the way you intended.
0
 
Infinity08Commented:
The easiest way to do this of course would be to start at 0 instead of at 7.
0
 
VallerianiAuthor Commented:
Thanks, and yeah, just looks odd i I keep starting at 0 if the ship isn't currently at frame 0 ;) But this works good thanks both.
0
 
TommySzalapskiCommented:
Um, my last code piece was assuming positive and negative rotation from -90 to 90. If you go from 0 to 179 then use this
if current_position <= multiplier/2 or current_position >= 180 - multiplier/2
  current_position = 0

Open in new window


This is simple and will work in any case for any speed. Note: This would go after the code that sets the position after rotating. Any time the position is changed, this should run.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.