Reputation: 289
Im trying to code a 2D pong game with java. The problem that i have is about fluidity. Im using images (As most of the 2D games do) for background and paddles and the ball. Created the game with the Actionlistener and attached it with a timer to the game. For now i just have this code in the game loop for collision.
@Override
public void actionPerformed(ActionEvent e) {
if (ball.y <= 0 || ball.y + ball.height + 35 > HEIGHT) { // Horizontal
// Walls
// collision
ballspeedY = -ballspeedY;
}
if (ball.x <= 0 || ball.x + ball.width + 5 >= WIDTH) { // Vertical Walls
// collision
ballspeedX = -ballspeedX;
}
ball.y += ballspeedY;
ball.x += ballspeedX;
renderer.repaint();
}
And this code for drawing
public void repaint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(backgroundImage, 0, 0, WIDTH, HEIGHT, null);
g2d.drawImage(ball.image, ball.x, ball.y, ball.width, ball.height, null);
g2d.drawImage(paddle.image, paddle.x, paddle.y, paddle.width, paddle.height, null);
}
The problem is, when i increase the ball's speed, it starts to move not smoothly. Ive done a research and tried to cancel drawing the background image. And the ball started to move a bit more smooth. The thing im confused about is, what if i have moving background or 100 balls or something else. Im doing something wrong and please someone tell me which way i should use to move these objects on the screen smoothly. The games which are using a lot bigger images or backgrounds or even 3D games move so fluidly but my simple pong game doesn't. What am i doing wrong?
SOLVED: The problem is just with this line: g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
This line cracks the animations fluidity. When i delete it, everthing runs pretty fast and smooth
Upvotes: 0
Views: 128
Reputation: 1234
I can't fully answer your question, because that is actually a complicated subject. There are multiple different aspects to talk about, but each of them is in itself studying material. I'll try to cover the basic ideas:
Multithreading
So first of all, your code is probably executed all sequential. Each time actionPerformed
runs, it will call redraw
at the end. Now, basically this will define your 'framerate'. So the more time consuming (more complex code) your actionPerformed
is, the lower your framerate will get.
To counter this, one thread on it's own can take care to draw the actual state of the game.
Ball speed is not related to the framerate
I am not sure how to read your code. But from what I see, your ballspeed is an integer. Take in account the thing I wrote above. So in your current implementation ballspeed actually defines how far the ball moves per frame. That is a problem, because it would mean, that the game runs faster/slower depending on the complexity of actionPerformed
. Even if I read your code wrong, this is a thing to consider.
One way to solve it, is to separate calculation and drawing with multiple threads like mentioned above. But you should probably also have a look at frame independent physics.
100 balls problem
If you would calculate the positions of all x balls in your actionPerformed
method, your framerate becomes increasingly slower the bigger x gets. This, again, is because of the things above. But I guess at one point you have to do further optimizations, otherwise your recalculation process can't keep up. Though the number should be quite high - but this is where I lack experience.
Graphics2D is not OpenGL
I am not too familiar with how the Java Graphics2D works, but I have basic knowledge about OpenGL. For real games OpenGL is almost a must. Even webgames start to use WebGL (browser supported interface for similar functionality). This is where you actually use the graphics card for its capabilities. Especially for 3D games, but even for simple 2D games it is really the best for performance. By the way, smartphones games as well.
Covering that would be impossible here, so I just leave it as is. If you are interested in those things, you would need to learn them more in depth. I guess your next step would be to use a framerate independent calculation. I'm not sure about the multithreading stuff, because it usually comes along with a bunch of difficulties...
Upvotes: 2