ios game center submit time and show in leader board - iphone

in my app i need to submit the time to the game center and i need to show that in Elapsed Time - To the hundredth of a second format.
00:00:00.00
this is the format i want to show in leader board.
In my app im getting the time in following format
ss.SS
ss = seconds
SS = hundredth of a second
i converted the value to double before send it to the game center
double newScoreDouble = [newScore doubleValue];
But when i sending the double score to the game center it asking me to convert it to int64_t format. But when i convert it to that format it loses some part of the double value.
double intPart = 0;
double fractPart = modf(newScoreDouble, &intPart);
int isecs = (int)intPart;
int min = isecs / 60;
int sec = isecs % 60;
int hund = (int) (fractPart * 100);
int64_t time_to_send_through_game_center = min*6000 + (sec*100 + hund);
this is the way i convert double to int64_t
Can any one say how to send whole double value to the game center and display it in Elapsed Time - To the hundredth of a second format.
Thanks

I've done this before. When you're recording a score in the to the hundredth of a second format. You would multiply your seconds with a hundred before submitting.
So let's say the user scored 1minute 44 seconds 300 milliseconds : 1:44:30 = 104.3 seconds. Then you would set your value property of GKScore object equal to 104.3 * 100 = 10430 ,and submit it like that.
Give it a try :)

Related

MIDI tick to millisecond?

I realize that there are many questions here concerning converting MIDI ticks to milliseconds (ex: How to convert midi timeline into the actual timeline that should be played, Midi Ticks to Actual PlayBack Seconds !!! ( Midi Music), Midi timestamp in seconds) and I have looked at them all, tried to implement the suggestions, but i am still not getting it.
(Did I mention I am a little "math phobic")
Can anyone help me work a practical example? I am using the Bass lib from un4seen. I have all the data I need - I just don't trust my calculations.
Bass Methods
Tick
// position of midi stream
uint64_t tick = BASS_ChannelGetPosition(midiFileStream, BASS_POS_MIDI_TICK)
PPQN
//The Pulses Per Quarter Note (or ticks per beat) value of a MIDI stream.
float ppqn;
BASS_ChannelGetAttribute(handle, BASS_ATTRIB_MIDI_PPQN, &ppqn);
Tempo
//tempo in microseconds per quarter note.
uint32_t tempo = BASS_MIDI_StreamGetEvent( midiFileStream, -1, MIDI_EVENT_TEMPO);
My Attempt at Calculating MS value for tick:
float currentMilliseconds = tick * tempo / (ppqn * 1000);
The value I get appears correct but I don't have any confidence in it since I am not quite understanding the formula.
printf("tick %llu\n",tick);
printf("ppqn %f\n",ppqn);
printf("tempo %u\n",tempo);
printf("currentMilliseconds %f \n", currentMilliseconds);
Example output:
tick 479
ppqn 24.000000
tempo 599999
currentMilliseconds 11974.980469
Update
My confusion continues but based on this blog post I think I have the code right – at least the output seems accurate. Conversely, the answer provided by #Strikeskids below yields different results. Maybe I have an order of operations problem in there?
float kMillisecondsPerQuarterNote = tempo / 1000.0f;
float kMillisecondsPerTick = kMillisecondsPerQuarterNote / ppqn;
float deltaTimeInMilliseconds = tick * kMillisecondsPerTick;
printf("deltaTimeInMilliseconds %f \n", deltaTimeInMilliseconds);
.
float currentMillis = tick * 60000.0f / ppqn / tempo;
printf("currentMillis %f \n", currentMillis);
Output:
deltaTimeInMilliseconds 11049.982422
currentMillis 1.841670
Tempo is in beats per minute. Because you want to be getting a time, you should have it in the denominator of your fraction.
currentTime = currentTick * (beats / tick) * (minutes / beat) * (millis / minute)
millis = tick * (1/ppqn) * (1/tempo) * (1000*60)
to use integer arithmetic efficiently do
currentMillis = tick * 60000 / ppqn / tempo
This works:
float kMillisecondsPerQuarterNote = tempo / 1000.0f;
float kMillisecondsPerTick = kMillisecondsPerQuarterNote / ppqn;
float deltaTimeInMilliseconds = tick * kMillisecondsPerTick;
printf("deltaTimeInMilliseconds %f \n", deltaTimeInMilliseconds);

how to print the decimal value in iphone?

I have a double value as 47. I divide it by 60, but when I try to print it it shows 0.0000.
It should display 0.7858
How can I do that? Any help?
Here is my code:
int minutes = decimal * 60;
float min = (minutes)/60;
NSLog(#"%.4f",min);
Try this :
double decimal = 47.0 / 60.0; // Just so minutes is 47
double minutes = decimal * 60.0;
double min = minutes / 60.0;
NSLog(#"%.4f",min);
In Objective C (as well as C and C++), integer division is applied when both left and right operands are integers, so minutes / 60 = 0. If you want a floating point operation, use floating points literals.
try this:
int minutes = 40;
float min = (minutes * 1.0f)/60.0f;
NSLog(#"%.4f",min);
or
int minutes = 40;
float min = (float)minutes/60.0f;
NSLog(#"%.4f",min);

UIGestures translated to velocity

So I'm looking at some code and basically there are a row of images and you swipe on the row, and the card images move in the row according to the swipe. Like you give it a flick, and then it moves 5 cards down the list. You give it a small flick, then you move 1 card down the list. It also has the ability to enter a card number, like if you were starting at card 1, and wanted to go to card 52, you type in 52, and it scrolls through all the 51 cards to get to card 52 and stops. Both the swipe gesture, and goToCard code call the below method:
- (void)doAnimation {
double elapsed = CACurrentMediaTime() - startTime;
if (elapsed >= runDelta) [self endAnimation];
else {
[self updateAnimationAtTime:elapsed];
}
}
- (void)updateAnimationAtTime:(double)elapsed
{
int max = 52;
if (elapsed > runDelta) elapsed = runDelta;
double delta = fabs(startSpeed) * elapsed - FRICTION * elapsed * elapsed / 2;
if (startSpeed < 0) delta = -delta;
offset = startOff + delta;
}
It looks like the developer was trying to match physics velocity equation of
DeltaX = Vot + (1/2) accel * time^2
It works pretty good when I test it in that the card images speed up according to your swipe in the beginning, and then slows down as you get closer to the runDelta (endpoint).
There's another function that calls updateAnimationAtTime like this:
- (void)goToCard:(int)pos {
int max = 52;
startSpeed = sqrt(fabs(pos - startOff) * FRICTION * 2);
runDelta = fabs(startSpeed / FRICTION);
startTime = CACurrentMediaTime();
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:#selector(doAnimation) userInfo:nil repeats:YES];
}
It looks like in the above function, the startSpeed is calculated like it would be in physics:
Vf^2 = Vo^2 + 2ug(Xf - Xi)
where u is the coefficition of friction and they are assuming gravity is equal to 1.
So the code works, but when the # of cards gets increased from 52 to 300, the animation looks funny since it's cycling through 300 cards to go from start to finish. I've never really implemented physics related stuff in code and was wondering if anyone had any suggestions or tips in how to change the code to still give the user the effect that we're cycling through all the cards, but not cycle through all of them when it goes past 50 as that's where the animation starts to look funny as it starts to spin so fast that the original effect of cycling through cards is lost.
Please let me know if anything needs to be clarified in my question. Thanks!

Calculate Pace To String

I am trying to calculate pace (min/mi) and format it as mmmm:ss.
So far I calculate my pace into a float by taking 60 and dividing it by my average speed. At an average speed of 76mph, my average pace is displayed as 0.79. I want to format it so that it converts my 0.79 minutes to mmmm:ss (thus showing my average pace as 0000:47). How can I do this?
double milesPerHour = 76.0;
int secondsPerMile = (int)round(3600.0 / milesPerHour);
NSString *paceString = [NSString stringWithFormat:#"%04d:%02d", secondsPerMile / 60, secondsPerMile % 60];
Not sure if I get your question right, but this is pretty much just math.
You can get the minutes by rounding your value (0.79 in this case) down and you can get seconds by taking your value, subtracting the minutes from it and multiplying that by 60.
So if you'd need 2.35 minutes for a mile, you'd have 2 minutes and 0.35*60 = 21 seconds.

% operator for time calculation

I am trying to display minutes and seconds based on a number of seconds.
I have:
float seconds = 200;
float mins = seconds / 60.0;
float sec = mins % 60.0;
[timeIndexLabel setText:[NSString stringWithFormat:#"%.2f , %.2f", mins,seconds]];
But I get an error: invalid operands of types 'float' and 'double' to binary 'operator%'
And I don't understand why... Can someone throw me a bone!?
A lot of languages only define the % operator to work on integer operands. Try casting seconds and mins to int before you use % (or just declare them int in the first place). The constant values you use will also need to be int (use 60 instead of 60.0).
As others have pointed out, you should be using integers. However, noone seems to have spotted that the result will be incorrect. Go back and have another look at modulo arithmetic, and you'll realize you should be doing
int seconds = 200;
int mins = seconds / 60;
int sec = seconds % 60;
Note the last line, seconds % 60 rather than mins % 60 (which will return the remainder of the minutes divided by 60, which is the number of minutes to the hour, and completely unrelated to this calculation).
EDIT
doh, forgot the ints... :)
The 60.0 forces a conversion to double
try:
float seconds = 200;
float mins = seconds / 60;
float sec = mins % 60;
Use ints instead. At least in your example, seems like they're enough (it will also be faster and clearer).
Also, in this case you would get 3.3333... mins, and not 3 minutes as expected. You could use Math.ceil(x) if you need to work with floats.
Do like this:
float seconds = 200.5;
float mins = floor(seconds / 60.0);
float sec = seconds - mins * 60.0;