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

Posted on 2010-11-24
Last Modified: 2012-05-10
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!
Question by:Valleriani
  • 2
  • 2
LVL 53

Assisted Solution

Infinity08 earned 200 total points
ID: 34205333
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.
LVL 37

Accepted Solution

TommySzalapski earned 300 total points
ID: 34205399
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
  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

LVL 53

Expert Comment

ID: 34205714
The easiest way to do this of course would be to start at 0 instead of at 7.

Author Closing Comment

ID: 34205776
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.
LVL 37

Expert Comment

ID: 34205789
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.

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question