Solved

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

Posted on 2010-11-24
5
331 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
  • 2
  • 2
5 Comments
 
LVL 53

Assisted Solution

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

Accepted Solution

by:
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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This is an explanation of a simple data model to help parse a JSON feed
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
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.

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now