[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2010-11-24
5
Medium Priority
?
339 Views
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!
0
Comment
Question by:Valleriani
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 800 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.
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 1200 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
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
 
LVL 53

Expert Comment

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

Author Closing Comment

by:Valleriani
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.
0
 
LVL 37

Expert Comment

by:TommySzalapski
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
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…
Introduction to Processes

649 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