Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Limit frame rate code-side only


Oen44
 Share

Recommended Posts

Hi,

I've made fading and floating text effect like this:

38622211_439668059856433_3442053363007488000_n.gif.513a2bdafc4d7c5f09fd2ab4c0fb68ba.gif

It's cool, I like how it ended up but... There is framerate issue. When making this, I was at 75FPS cap (VSync) and after unlocking to unlimited FPS, text started moving way too fast.

Here is the code

@SubscribeEvent
public void onRenderOverlayText(RenderGameOverlayEvent.Text event) {
	ScaledResolution sr = event.getResolution();
	EntityPlayer player = Minecraft.getMinecraft().player;
  
	GL11.glPushMatrix();
	GL11.glScalef(1.0F, 1.0F, 1.0F);

	for (int i = 0; i < 12; i++) {
		if (!this.isVisible(i))
			continue;

		this.alpha[i] -= 3;

		this.height[i] += 0.6f;
		if (this.height[i] > 50.0f) {
			this.hide(i);
			continue;
		}

		Minecraft.getMinecraft().fontRenderer.drawStringWithShadow("+" + this.xp[i] + " XP",
				sr.getScaledWidth() / 2 + 50, sr.getScaledHeight() / 2 - this.height[i],
				0xFFFFFF | (this.alpha[i] << 24));

		GL11.glScalef(0.5f, 0.5f, 0.5f);
		Minecraft.getMinecraft().renderEngine.bindTexture(icon);
		this.drawTexturedModalRect((sr.getScaledWidth() / 2 + 32) * 2,
				((sr.getScaledHeight() / 2) - 5 - this.height[i]) * 2, i * 32, (i > 7 ? 1 : 0) * 32, 32, 32);
	}

	GL11.glPopMatrix();
}

 

Is there any way to make this run at same pace? Like deltaTime?

Edited by Oen44
Link to comment
Share on other sites

6 minutes ago, Oen44 said:

this.height[i] += 0.6f;

You need to multiply this value by the deltaTime between frames (after dividing out the deltaTime at 75 fps).

I'm not sure where you'd get that value, though.

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

long oldTime = newTime;
long newTime = Minecraft.getMinecraft().getSystemTime();
long deltaTime = newTime - oldTime;

deltaTime ready.

this.alpha[i] -= 0.3 * deltaTime;
this.height[i] += 0.06f * deltaTime;

Calculations ready.

 

Working perfectly :)

Link to comment
Share on other sites

Doing your own delta time works, but there is another more common approach which I figured is worth mentioning. The game logic runs in a loop that "ticks" 20 frames per second. The render refresh rate can vary depending on your monitor, settings and power of your GPU.

 

So whenever you're doing animations in Minecraft you should set the positions and angles based on the game ticks, not the refresh rate. So you would really increment the position every tick rather than every event (which fires at the refresh rate). However, doing that has the side effect that it will look a bit jerky because it will only update position 20 times per second which is a bit less than needed for perception of smooth motion (traditional movie film used 24 fps). So to smooth it out, there is the concept of "partial ticks" that are passed to the rendering methods (and events).

 

So an alternative, arguably more common, way to do animations is to update the position using the ticks plus a bit of adjustment using the partial tick. For example, the game overlay event has a getPartialTicks() method to help with this.

 

Just thought you'd be interested. Point is that partial ticks are used to communicate difference between refresh rate and tick rate and are commonly used to control animation speed.

  • Like 1

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

14 minutes ago, jabelar said:

Doing your own delta time works, but there is another more common approach which I figured is worth mentioning. The game logic runs in a loop that "ticks" 20 frames per second. The render refresh rate can vary depending on your monitor, settings and power of your GPU.

 

So whenever you're doing animations in Minecraft you should set the positions and angles based on the game ticks, not the refresh rate. So you would really increment the position every tick rather than every event (which fires at the refresh rate). However, doing that has the side effect that it will look a bit jerky because it will only update position 20 times per second which is a bit less than needed for perception of smooth motion (traditional movie film used 24 fps). So to smooth it out, there is the concept of "partial ticks" that are passed to the rendering methods (and events).

 

So an alternative, arguably more common, way to do animations is to update the position using the ticks plus a bit of adjustment using the partial tick. For example, the game overlay event has a getPartialTicks() method to help with this.

 

Just thought you'd be interested. Point is that partial ticks are used to communicate difference between refresh rate and tick rate and are commonly used to control animation speed.

So how ticks work compared to deltaTime?

Lower FPS (30):

Ticks - text is adapting to FPS and moves slow - not cool

Delta - text is smooth as for such low FPS and stays the same time as with high FPS - what I want

 

Higher FPS (around 600):

Ticks - text is smooth and very fast, again, adapting to FPS - not cool

Delta - text is smooth, perfect pace - what I want

 

Conclusion? Ticks are not good for that kind of animations.

Edited by Oen44
Link to comment
Share on other sites

10 minutes ago, Oen44 said:

That doesn't change anything. Ticks are not helpful here, it's just looking bad.

Since they happen at a set period of time after each other the game has a deltaTime(partialTicks) variable between each tick that is used for rendering things smoothly. So the game would update the position as to where it should be every tick and then render at that position with the partialTicks. Though your way of doing it works this is just the way minecraft handles these things.

  • Like 1

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

10 minutes ago, Oen44 said:

That doesn't change anything. Ticks are not helpful here, it's just looking bad.

If it looks bad, then you are doing something wrong. All animations in Minecraft are built using this concept and they work just fine.

It is a very simple process:

float currentValue = 0
float prevValue = 0

tick() { // 20 times a second, always, that's what ticks are
    prevValue = currentValue;
    currentValue = (currentValue + 1) % 100;
}

render(float partialTicks) {
    float actual = prevValue + (currentValue - prevValue) * partialTicks;
    // render at position actual
}

 

You do not have to use this, but saying that ticks are inadequate for this is not correct.

  • Like 1
Link to comment
Share on other sites

1 hour ago, diesieben07 said:

If it looks bad, then you are doing something wrong.

All I did was

this.alpha[i] -= (int) (0.4f * event.getPartialTicks());
this.height[i] += 0.1f * event.getPartialTicks();

And it's very slow on low FPS and getting faster with more FPS.

 

1 hour ago, diesieben07 said:

It is a very simple process:


float currentValue = 0
float prevValue = 0

tick() { // 20 times a second, always, that's what ticks are
    prevValue = currentValue;
    currentValue = (currentValue + 1) % 100;
}

render(float partialTicks) {
    float actual = prevValue + (currentValue - prevValue) * partialTicks;
    // render at position actual
}

Yeah, no idea where to put prevValue and currentValue. Should I make thread like this?

ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(new Runnable() {
  @Override
  public void run() {
    prevValue = currentValue;
    currentValue = (currentValue + 1) % 100;
  }
}, 0, 20, TimeUnit.SECONDS);

 

I just want to check how is that going to look like with what you wrote.

The thing is that RenderGameOverlayEvent is executed more times based on FPS so deltaTime is doing something to balance and it's easy to accomplish.

Edited by Oen44
Link to comment
Share on other sites

9 minutes ago, Oen44 said:

And it's very slow on low FPS and getting faster with more FPS.

Well, yes, that's not how you use partialTicks.

 

9 minutes ago, Oen44 said:

Yeah, no idea where to put prevValue and currentValue.

Anywhere you like. You can make them static fields if you want to.

 

10 minutes ago, Oen44 said:

Should I make thread like this?

Definitely not.

 

10 minutes ago, Oen44 said:

The thing is that RenderGameOverlayEvent is executed more times based on FPS so deltaTime is doing something to balance and it's easy to accomplish.

RenderGameOverlayEvent (careful: this fires many many times every frame!) is the render part in my code. You would need ClientTickEvent (check TickEvent#phase or your code will run twice per tick) for the tick part.

Link to comment
Share on other sites

Maybe this would be clearer. 

 

In a tick handling event like ClientTickEvent, in one phase of that event, you would update the position in a public static field. This would have a very regular steady movement.

 

In the render handling event, you would get the position from that public static field and add a multiple of the partial ticks times the increment. 

 

Note that the tick value can be also updated in other ways. For example, the world keeps track of total time, so you could record the world time when the GUI was just opened and then calculate how many ticks it was open in the render event. 

 

I didn't mean to cause confusion. Just wanted to point out the way Minecraft normally handles the conversion from ticks to render frames for animations.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

15 minutes ago, diesieben07 said:

RenderGameOverlayEvent (careful: this fires many many times every frame!) is the render part in my code. You would need ClientTickEvent (check TickEvent#phase or your code will run twice per tick) for the tick part. 

Of course there is TickEvent... I should have think about that before, ohh my.

 

float prevValue;
float currentValue;

@SubscribeEvent
public void onClientTick(ClientTickEvent event) {
	if(event.phase == Phase.END) {
		prevValue = currentValue;
		currentValue = (currentValue + 1) % 100;
	}
}

float actual = prevValue + (currentValue - prevValue) * event.getPartialTicks(); // in RenderGameOverlayEvent.Text

Well... It's not working well. It's starting slow and then goes very fast and then slow again and so on (because of modulo i guess).

Edited by Oen44
Link to comment
Share on other sites

Not quite. If you assign previous value to current value, the increase current value and take modulo, then every once in a while the prev value is going to be way higher. Basically the previous value will roll over to zero one tick after the current value. 

 

What it should be is more like this:

- forget the previous value, you just need current value. So start current value at zero, and keep your line where you add one and do modulo. So current value is going to cycle from 0 to 99 over and over.

- just add partial ticks to current value. That will give the total number of ticks (the current value has the whole number of ticks, plus the partial ticks).

- multiply that result by whatever speed you want.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

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

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Although that too has issues. The forge defined configuration files are loaded after registration
    • Unless I have misunderstood your requirements, I don't think what you are trying to do is going to work.   The closest things in minecraft are banner patterns or paintings. But these are registered in code (not what you want) on both the client and the server. Then when it loads resource packs on the client it looks up the texture for each one.   It does it this way because the game defines what should exist, then lets the user/modpack developer give them textures (with you providing a default texture).   You seem to want to populate a registry from what textures exist in a resource pack? So kind of the reverse. This isn't going to work for at least 2 reasons: * By the time resource packs are loaded, the registries are frozen - It is too late. * Servers don't even load resource packs, so there is no way it can use this mechanism    You need some other mechanism to populate your registry. e.g. a configuration file holding the names of what should exist, but it doesn't sound like what you want?
    • For the most part, this worked, somehow more mods were outdated and i need to fix them
    • You have a version of oculus from the beta (1.2.5) The latest version is mc1.19-1.2.5a, if that doesn't work contact the mod author https://www.curseforge.com/minecraft/mc-mods/oculus/files  
    • ---- Minecraft Crash Report ---- // Hi. I'm Minecraft, and I'm a crashaholic. Time: 8/10/22 7:20 PM Description: Mod loading error has occurred java.lang.Exception: Mod Loading has failed     at net.minecraftforge.fml.CrashReportExtender.dumpModLoadingCrashReport(CrashReportExtender.java:85) [?:?] {re:classloading}     at net.minecraftforge.fml.client.ClientModLoader.completeModLoading(ClientModLoader.java:188) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.lambda$null$1(Minecraft.java:508) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft$$Lambda$3569/1174919051.run(Unknown Source) [?:?] {}     at net.minecraft.util.Util.func_215077_a(Util.java:320) [?:?] {re:classloading}     at net.minecraft.client.Minecraft.lambda$new$2(Minecraft.java:504) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft$$Lambda$3352/424832797.accept(Unknown Source) [?:?] {}     at net.minecraft.client.gui.ResourceLoadProgressGui.func_230430_a_(ResourceLoadProgressGui.java:113) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.GameRenderer.func_195458_a(GameRenderer.java:481) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.func_195542_b(Minecraft.java:977) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:607) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.main.Main.main(Main.java:184) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51] {}     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_51] {}     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_51] {}     at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_51] {}     at net.minecraftforge.fml.loading.FMLClientLaunchProvider.lambda$launchService$0(FMLClientLaunchProvider.java:51) [forge-1.16.5-36.2.34.jar:36.2] {}     at net.minecraftforge.fml.loading.FMLClientLaunchProvider$$Lambda$420/566698125.call(Unknown Source) [forge-1.16.5-36.2.34.jar:36.2] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.1.3.jar:?] {} A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- Head -- Thread: Render thread Stacktrace:     at net.minecraftforge.fml.CrashReportExtender.lambda$dumpModLoadingCrashReport$7(CrashReportExtender.java:88) ~[?:?] {re:classloading} -- MOD jei -- Details:     Mod File: jei-1.16.5-7.7.1.152.jar     Failure message: fml.modloading.dupedmod     Mod Version: 7.7.1.152     Mod Issue URL: https://github.com/mezz/JustEnoughItems/issues?q=is%3Aissue     Exception message: MISSING EXCEPTION MESSAGE Stacktrace:     at net.minecraftforge.fml.CrashReportExtender.lambda$dumpModLoadingCrashReport$7(CrashReportExtender.java:88) ~[?:?] {re:classloading}     at net.minecraftforge.fml.CrashReportExtender$$Lambda$3599/485371408.accept(Unknown Source) ~[?:?] {}     at java.util.ArrayList.forEach(ArrayList.java:1249) ~[?:1.8.0_51] {}     at net.minecraftforge.fml.CrashReportExtender.dumpModLoadingCrashReport(CrashReportExtender.java:86) [?:?] {re:classloading}     at net.minecraftforge.fml.client.ClientModLoader.completeModLoading(ClientModLoader.java:188) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.lambda$null$1(Minecraft.java:508) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft$$Lambda$3569/1174919051.run(Unknown Source) [?:?] {}     at net.minecraft.util.Util.func_215077_a(Util.java:320) [?:?] {re:classloading}     at net.minecraft.client.Minecraft.lambda$new$2(Minecraft.java:504) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft$$Lambda$3352/424832797.accept(Unknown Source) [?:?] {}     at net.minecraft.client.gui.ResourceLoadProgressGui.func_230430_a_(ResourceLoadProgressGui.java:113) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at net.minecraft.client.renderer.GameRenderer.func_195458_a(GameRenderer.java:481) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.func_195542_b(Minecraft.java:977) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:607) [?:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}     at net.minecraft.client.main.Main.main(Main.java:184) [?:?] {re:classloading,pl:runtimedistcleaner:A}     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_51] {}     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_51] {}     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_51] {}     at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_51] {}     at net.minecraftforge.fml.loading.FMLClientLaunchProvider.lambda$launchService$0(FMLClientLaunchProvider.java:51) [forge-1.16.5-36.2.34.jar:36.2] {}     at net.minecraftforge.fml.loading.FMLClientLaunchProvider$$Lambda$420/566698125.call(Unknown Source) [forge-1.16.5-36.2.34.jar:36.2] {}     at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.Launcher.run(Launcher.java:82) [modlauncher-8.1.3.jar:?] {}     at cpw.mods.modlauncher.Launcher.main(Launcher.java:66) [modlauncher-8.1.3.jar:?] {} -- System Details -- Details:     Minecraft Version: 1.16.5     Minecraft Version ID: 1.16.5     Operating System: Windows 10 (amd64) version 10.0     Java Version: 1.8.0_51, Oracle Corporation     Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation     Memory: 507993864 bytes (484 MB) / 1791492096 bytes (1708 MB) up to 3817865216 bytes (3641 MB)     CPUs: 4     JVM Flags: 4 total; -XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump -Xss1M -Xmx4096m -Xms256m     ModLauncher: 8.1.3+8.1.3+main-8.1.x.c94d18ec     ModLauncher launch target: fmlclient     ModLauncher naming: srg     ModLauncher services:          /mixin-0.8.4.jar mixin PLUGINSERVICE          /eventbus-4.0.0.jar eventbus PLUGINSERVICE          /forge-1.16.5-36.2.34.jar object_holder_definalize PLUGINSERVICE          /forge-1.16.5-36.2.34.jar runtime_enum_extender PLUGINSERVICE          /accesstransformers-3.0.1.jar accesstransformer PLUGINSERVICE          /forge-1.16.5-36.2.34.jar capability_inject_definalize PLUGINSERVICE          /forge-1.16.5-36.2.34.jar runtimedistcleaner PLUGINSERVICE          /mixin-0.8.4.jar mixin TRANSFORMATIONSERVICE          /forge-1.16.5-36.2.34.jar fml TRANSFORMATIONSERVICE      FML: 36.2     Forge: net.minecraftforge:36.2.34     FML Language Providers:          javafml@36.2         minecraft@1         kotori_scala@2.13.8-build-3     Mod List:          forge-1.16.5-36.2.34-client.jar                   |Minecraft                     |minecraft                     |1.16.5              |NONE      |Manifest: NOSIGNATURE         forge-1.16.5-36.2.34-universal.jar                |Forge                         |forge                         |36.2.34             |NONE      |Manifest: 22:af:21:d8:19:82:7f:93:94:fe:2b:ac:b7:e4:41:57:68:39:87:b1:a7:5c:c6:44:f9:25:74:21:14:f5:0d:90
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.