JavaFX: Heavy laggs after a specific time - java

We're working on a little game with JavaFX for the GUI. The game is turn-based and after each turn we update our borderPane (.setCenter()) with the new board (this is an array which representes the tiles in the game). The tiles are drawn as JavaFX-Canvas objects.
We have the problem that every time after 155-160 turns the whole application begins to lag. CPU usage is increasing to 80% on my computer by one turn (e.g. turn 154: 1 % und turn 155: 80%). We also got an outOfMemoryError but not every time.
Additionally, we implemented some sysos for debugging and all the methods on our board array with the game logics are correctly and fastly iterated. The lag must be something in the JavaFX-Thread.
Can you help me?
Thanks a lot :)

The gc maybe isnt cleaning old objects. Did you try to use the daneobjects but not creating a new one every time?
Greets

Related

Infinite Runner Lags after a while

I am creating a simple infinite runner game for the android os using the libgdx framework, I have everything working fairly well but then I noticed that after a while mayne 2 or 3 minutes in the fps drops steadily until it goes all the way to 0... I am creating the game assets every 10 or so seconds and then setting their bodies active to false when they are out of screen, it was my understanding that it would not calculate for them after this but it slows down when I run the assets and it doesn't slow down when I don't run the assets...my question is is this normal and should I dispose of these assets a different way? how do other infinite runner games deal with this lag issue, also I am loading the texture from my asset manager so I believe it is only using the one texture instead of creating many....
okay so after testing all day I see that the game slows down after a while because its still rendering the images even after they leave the screen, I implemented a method to stop rendering them after a certain point and start rendering a new series of backgrounds, its a work around to be sure but im one step closer to my goal so ill take it as a win.

Moving objects smoothly, bad perfomance

I just finished developing my mini-game on Android and I got some questions about perfomance and generally "how to": If I have 25+ objects on the creeen (just a polygon with 7-8 angles) its starts to lag hard and CPU usage is very high (15-20% without colision detection, up to 50% with collision detection on samsung note 10.1 tablet).
So I have ~25 polygons (asteroids) and they are always "flying". So to move them I make a Timer, right? On TimerTask I got something like this
public void move() {
translate[1] += speed[1];
translate[0] += speed[0];
updateTranslateMatrix(); // updating translate matrix and then send it into vertex shader
updateAABBCoords(); // update coordinates of Axis-aligned bounding box
updateCoordsByTranslate(); // update coordinates of verticles (to be able to define exact collision (with bullets and starship))
}
Is there something unnecessary in this method?
Also I want to ask if it is OK to run this timer every 17 ms? Or 17 ms is too often? I feel smoothness of movement only at 17 ms, may be I am doing it wrong? And same Timer interval I got on starship/bullets movement and on Collision detection.
Help me, please. I feel like missing something huge and fundamental, because 50% CPU on note 10.1 in that simple Asteroids game is not normal.
One key issue, I believe, is that you are assigning an individual Timer to every object instead of employing a general purpose game loop. A vast majority of games use a game loop which runs continuously and can be split, typically, into two components: Update and Render.
An article on basic game loop design for Java/Android
An answer to a relevant question on game loop design on gamedev.stackexchange.com
When using a game loop you can not guarantee being able to update exactly every 17ms or some other arbitrary duration. Instead, the Update and Render methods receive a DeltaTime parameter which is the time since the last frame.
This is then used when, for example, getting new object positions by multiplying it with the object's velocity. Doing so allows for smooth interpolation of position and ensures that object speed is not affected by CPU speed.

Are my programming methods for java games sufficient [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
so I'm making a 2d Java game with the slick2d and MarteEngine libraries. This is the biggest project since I made checkers for my Java class. I am still pretty new to programming concepts and using optimal ways to get stuff done.
The basic structure of my game is you are a player/hero in a zombie apocalypse and you can gather survivors to help you. There are a many areas that I'm concerned about in my programming. I'm not sure if my methods are a good choice for what I want. This game also does not currently have a grid/tile system.
I've looked at some open source java games and they don't really answer my questions to my methods. So I'm going to make a list here of what I'm uncertain about and I hope you guys can confirm/deny if my methods are appropriate. Sorry if this list gets too long, I'm thinking of the questions as I type.
Targeting/Attacking - Survivors will automatically attack zombies once they get within the gun's target range. To do this, I have every survivor get the distance (using distance formula) to every zombie and find the closest one to attack. I check for this constantly so if a faster zombie gets closer, the survivor will change targets. For zombies, they acquire a target and stick to it (for now). The zombies constantly check if they are within the attack range (around 50 pixels) using the distance formula. If they are within range, stop and attack, otherwise, move towards the target.
2D Camera - So a camera in a 2D environment moves the world around instead of you. My current method is have my zombies/survivors/any entities on the map stored in array lists. First the background is adjusted, then all the lists are cycled through and every entity's x and y values are modified. This seems to work alright but some stuff you can really notice sliding around on the background. Not really sure how to avoid this.
User Interface - I really have no clue how to work with UI. What I've been doing so far is simply using a background and then generating button objects and manually lining them up. Then, I check if the mouse is over any of the button's areas and if there is a click while moused over the button. I have three different backgrounds and buttons that I switch out with booleans. I'm going to recode that area though, using objects with the background and buttons. Is this the correct way to do UI?
Path Finding - I have no path finding system yet. Do I have to stick to a grid system? I really rather my entities move freely along the terrain and not in a weird square to square motion.
Selecting - I have it so you can select survivors, upgrade them, and other random stuff. My current method for selecting is constantly check where the mouse X and Y is. I get the distance from the mouse to every survivor and check if it is within 30 pixels. Then, I check if there is a click, if so, select the survivor and unselect all others. I'm still trying to figure out how to unselect all survivors if I click on open space. Is there a better way to go about doing this?
Picking stuff up - Same way as said before. I check the distance from the player to every item that can be picked up. If the item is within 30 pixels of the player, it picks it up. It seems to work fine for the moment I suppose. Maybe there really is no other way to do this.
Animations - I understand the how to animate with sprites, but I just want to make sure. So if I have 7 different guns to be shot, do I need to manually make functions that have precise timing on each sprite. Say if I have a shotgun, it needs a recoil, pump forward, brief pause, pump back, and ready again. For a pistol I need just the recoil really. So I'd have to make unique functions for each of these animations?
Sorry to type this long list of questions. I try to gather information on this stuff as much as possible and I haven't been able to find many examples on this stuff. I greatly appreciated any answers, even just a yes or no answer. Thanks in advance!
2D Camera:
Not exactly sure what you are doing when you say you modify each entity's position, but the way I'd do it is have a Camera object that has its own x, y, width, height and methods to move the camera around also, and then in your Draw cycle:
for (every Object on the map)
{
if (Object is within Camera bounds)
{
// Draw the Object at the Object-xy minus the Camera-xy
// This will draw the Object at its position relative to the camera
// and won't waste time drawing things that are not within camera bounds
}
}
Targeting/Attacking
You have the right idea, but checking every zombie against every survivor will take a lot of computing and (depending on how many zombies and survivors) can cause the game to slow down a lot. It's the same deal with collision detection, checking every object against every other object to see if they collide takes a lot. There are ways to not have to check everything against everything, I suggest you read into 'Spacial Partitioning'. I have not used slick2d but perhaps it has such a thing already implemented for you.
Picking stuff up
Same deal as Targeting/Attacking, if there are too many items or things that can pick up items, it will end up slowing frame rate a lot.
Selecting
This isn't way you should be doing selecting, but I myself have not had to use selection much at all so I am not completely sure of the best way, you should probably try searching around for ways to do this. Either way your current way can be improved by only checking on a click, you don't need to check every single frame, only check when there has been a click.
This is all I can help with currently, I hope it has been of some use to you at least.

2d platformer - only move objects currently on the screen?

Now the answer doesnt have to be centered with the programming language I am using (Java), it is really a general question. I am making a 2d platformer, and am autogenerating a terrain with over 30000 tiles (300x100 map). Now, this is obviously causing so so so much lag and flickering.
The one way I can think of to prevent this is to only move objects on screen, but this is really difficult for me to think it through.
It is a side scroller, the guy moves until he reaches the middle, and when hes in the middle, the platforms begin to move, and that is when it gets SO intensive. It has to do those for loops for a whole 30000 tiles every time the swing timer ticks.
Is anyone willing to enlighten me? Just maybe a nudge in the right direction would be great.
Thank you!
You'll likely be using some variation on MVC for this, and you'll have no choice but to move everything in the model -- the non-GUI logical representation of your program -- that needs moving, but the overhead for this shouldn't be huge. You'll only need to move things in the view -- the GUI portion of your program -- that are within the view's viewport (on the screen), and this will be a very limited subset of the objects on your map.

How to generate simple 2D graphics in real time?

For my internship on Brain-Computer Interfacing I need to generate some very fast flickering squares on a CRT monitor (flickering = alternating between two colors). The monitor's refresh rate is 85Hz and we would like this to be the bottleneck, which means that repainting all squares can take at most 1000/85 = 11ms.
My language of preference for GUI/graphics programming is Java, so I tried making a prototype with AWT, because it's synchronous (unlike Swing). I now seem to have two problems: the first is that time measurements show that the repainting of even 9 squares simply takes too long. My algorithm takes the desired frequency and calculates the times at which the system should repaint in advance and then uses a loop (with no sleep/wait delay) that checks everytime if the next 'time' was reached and if so, loops through all the squares to repaint them. The way I implemented it now is that the squares are Panels with background color A and are contained in another Panel with background color B and the flickering happens because the Panels' visibility is changed. I figured that this would be faster than one Panel that has to draw Rectangles all the time.
I don't have a decent profiling tool (can't get Eclipse TPTP or NetBeans profiler to work) so I can't be sure, but I have the feeling that the bottleneck is actually not in the repainting, but in the looping (with conditional checking etc.). Can you recommend anything about what I should do?
The second problem is that it seems like the squares are rendered top-to-bottom. It's like they unroll really fast, but still visibly. This is unacceptable. What I'm wondering though, is what causes this. Is it Java/AWT, or Windows, or just me writing a slow algorithm?
Can you recommend some things for me to try? I prefer to use Java, but I will use C (or something) if I must.
I would avoid any kind of high-level "components", like JPanels and the like. Try getting a Graphics2D representing the window's contents, and use its fillRect() method.
Failing that, you could probably do this easy enough in C and OpenGL. rasonly.c is a standard template program that sets up OpenGL to work as a "rasterizer" only, i.e. 2D mode. Using this as a starting point, you should be able to get something running that draws your desired "squares" without too much trouble.
You don't describe your desired scene very well, it sounds from that equation as if you want to draw 100 squares, each having a different color. For maximum performance in OpenGL, you should draw all squares of the same color together, to minimize the "state changes" between drawing calls. This is probably a purely theoretical point though, as drawing 100 2D squares at 85 Hz really shouldn't tax OpenGL.
UPDATE: Oh, so it's been a bunch of years, and nowadays you probably need to take the above with a grain of salt and read some modern tutorial. Things have changed. Look up the Vulkan API.
(I remember a demonstration of this using the BBC micro and palette switching, though that would be 50fps rather than 85, as it was a British domestic TV)
I'd switch to jogl and use display lists. They get very high fps rates in Java.

Categories