문제

I want to calculate an oscillating offset value based on the current time in seconds so that any object can call this method and would get an offset returned that is synchronized to any other object that calls the same method.

This is my setter for the locationOffset property:

- (float)locationOffset
{
    float currentTime = [[NSDate date] timeIntervalSince1970];
    locationOffset = sinf(currentTime);
    CCLOG(@"--- current time = %1.9f, location offset = %1.1f", currentTime, locationOffset);

    return locationOffset;
}

I'm calling this a 60Hz and the output looks like this:

--- current time = 1316013568.000000000, location offset = -0.1

...and it stays this way. Should everything after the decimal be zero?

I expect that I can adjust the frequency by scaling currentTime and the "wavelength" by scaling the offsett, but I just need to get my sinusoidal locationOffset to swing back and forth based on the time.

It seems like the location offset should cycle from -1 to 1 every 2pi seconds.

EDIT: Adding the method that works now as it should.

- (float)locationOffset
{
    double currentTime = [[NSDate date] timeIntervalSinceReferenceDate];
    locationOffset = sin(2.0f*currentTime) * 12.0f;
//    CCLOG(@"--- current time = %1.9f, location offset = %1.1f", currentTime, locationOffset);

    return locationOffset;
}

It cycles every pi seconds and has a magnitude of +/- 12.

올바른 솔루션이 없습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top