IGNORED

# Time delta in milliseconds?

## Recommended Posts

Is there a way to calculate the time delta between updates?  I've tried using rapSetClock(0) and rapClockHex, but that appears to be an integer value of whole seconds.  Is there a way to keep time at the millisecond?  I'm using JagStudio C.

Thanks!

##### Share on other sites

You will need to use rapNTSCFlag to determine if you are running on a PAL or NTSC console. This tells you if it should be 50 or 60 ticks per second.

Then you'll need to look the the current rapTicks value. If you track the difference of rapTicks from your last frame, you can then divide with the 50 or 60 from above to work out timing.

##### Share on other sites

You will need to use rapNTSCFlag to determine if you are running on a PAL or NTSC console. This tells you if it should be 50 or 60 ticks per second.

Then you'll need to look the the current rapTicks value. If you track the difference of rapTicks from your last frame, you can then divide with the 50 or 60 from above to work out timing.

Perfect, thank you!

##### Share on other sites

On 10/7/2023 at 4:28 AM, trufun101 said:

Is there a way to calculate the time delta between updates?  I've tried using rapSetClock(0) and rapClockHex, but that appears to be an integer value of whole seconds.  Is there a way to keep time at the millisecond?  I'm using JagStudio C.

Thanks!

If 20 or 17 ms is to rough, you can always use one of th HW timers. Setting these up is no rocket science.

##### Share on other sites

2 hours ago, 42bs said:

If 20 or 17 ms is to rough, you can always use one of th HW timers. Setting these up is no rocket science.

It might be from BASIC....

##### Share on other sites

6 hours ago, CyranoJ said:

It might be from BASIC....

I see there is peek and poke.

##### Share on other sites

• 2 weeks later...
On 10/7/2023 at 9:00 AM, Sporadic said:

You will need to use rapNTSCFlag to determine if you are running on a PAL or NTSC console. This tells you if it should be 50 or 60 ticks per second.

Then you'll need to look the the current rapTicks value. If you track the difference of rapTicks from your last frame, you can then divide with the 50 or 60 from above to work out timing.

I think I'm doing something wrong.  I'm trying to calculate the time delta (as a fraction of a second) and multiply that by the movement speed, but the character isn't moving.

Here's what I've got:

```int ticksPerSec = 60;  //NTSC by default
int lastTicks = 0;

if (!rapNTSCFlag)
{
ticksPerSec = 50;  //PAL
}

//Main Loop
for(;;)
{
float delta = (rapTicks - lastTicks) / ticksPerSec;
lastTicks = rapTicks;

...

//for movement...
sprite[spriteIndex].x_ -= playerMoveSpeed * delta; //playerMoveSpeed == 40```

So, let's say the delta is 3 ticks / 60 tps or 0.05.  I would expect the character to move 2 pixels (40 * 0.05) with each update.

##### Share on other sites

2 hours ago, trufun101 said:

I think I'm doing something wrong.  I'm trying to calculate the time delta (as a fraction of a second) and multiply that by the movement speed, but the character isn't moving.

Here's what I've got:

```int ticksPerSec = 60;  //NTSC by default
int lastTicks = 0;

if (!rapNTSCFlag)
{
ticksPerSec = 50;  //PAL
}

//Main Loop
for(;;)
{
float delta = (rapTicks - lastTicks) / ticksPerSec;
lastTicks = rapTicks;

...

//for movement...
sprite[spriteIndex].x_ -= playerMoveSpeed * delta; //playerMoveSpeed == 40```

So, let's say the delta is 3 ticks / 60 tps or 0.05.  I would expect the character to move 2 pixels (40 * 0.05) with each update.

float delta = (rapTicks - lastTicks) / ticksPerSec;

Above statement is always 0 since you're doing integer division, change it to this...

float delta = (float)(rapTicks - lastTicks) / (float)ticksPerSec;

##### Share on other sites

4 hours ago, JagMod said:

float delta = (rapTicks - lastTicks) / ticksPerSec;

Above statement is always 0 since you're doing integer division, change it to this...

float delta = (float)(rapTicks - lastTicks) / (float)ticksPerSec;

Oh, duh.  Thank you.  I can't tell you how many times I've made that mistake in the past.  It's just tougher without being able to step through the code.

Speaking of which, is there a way to print the value of the int using js_r_textbuffer?

##### Share on other sites

Do not use float  on the Jaguar. I really recommend to not even start using it for simple tasks like this.

As a wild guess, it needs about 300 or more cycles.

Rather use fixed point math or a completely different solution.

##### Share on other sites

To elaborate on what @42bs just wrote, use rapTicks directly, just adjust your move speed directly.

` sprite[spriteIndex].x_ -= playerMoveSpeed * rapTicks;`
##### Share on other sites

50 minutes ago, LordKraken said:

To elaborate on what @42bs just wrote, use rapTicks directly, just adjust your move speed directly.

` sprite[spriteIndex].x_ -= playerMoveSpeed * rapTicks;`

I think you missed the delta.

`x -= playerMoveSpeedPerTick * delta;`

Of course we do not want float, so

playerMoveSpeedPerTick = (40*256+20)/60;

or

playerMoveSpeedPerTick = (40*256+20)/50;

then

`x -= (playerMoveSpeedPerTick * delta) >> 8;`

##### Share on other sites

yes my bad, of course i meant the number of ticks elapsed since last frame, not the absolute tick number

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×