Painting Methods and Collision Detection
Hello, I am developing a real-time game which requires a lot of painting to be done. Since I have no prior knowledge of game programming - I decided to do the painting in a class which extends JPanel and just use the paintComponent method. This has proved as slow and insufficient for real-time games. Can anyone please help me find a way to do painting in a way suitable for real time games? Furthermore, in order to detect collision I have decided to descride terrain using a List of Polygons and then check if the player's rectangle intersects with any of the polygons - this seems a bit slow - is there some other way?
P.S - Can someone please delete the two new begging posts?
With Thanks,
laginimaineb.
# 2
Thanks for your reply. Anyway, the painting code is fine, the thing is I'm painting a background Image, two players and many other elements (and not to forget - terrain) every 25 milliseconds. I think my approach may by wrong and I was wondering whether there's a solution available for real-time games.
[ You're right about the posts :) ]
With Thanks,
laginimaineb.
# 4
Thanks again for your reply, however, I did not learn new techniques from the specified link. I would highly appreciate it if you could redirect me somewhere else.With Thanks,laginimaineb.
# 5
There are a couple ways of doing collision detecting. One way is using shapes, the other way is to use pixel collision detection. Both of these have an efficient (but "harder") way and an inefficient (but "easier") way.
The easiest way to do collision detecting is to create a Rectangle for the Objects colliding and check to see if they intersect. This will always work, but if your object is in anyway irregular and not rectangular, it will look bad. The other way to use Shapes is to create a Polygon that has the rough dimensions of your Object. This is slightly more efficient.
The alternate approach is similar to the Shape way, but instead of checking Points this approach is utilized by checking pixels. Most Images that you're using probably have some transparent parts (if the image is irregular). Using the getPixels() method (using either a BufferedImage or PixelGrabber) u can check the intersecting image and check to see if it contains any values that are not transparent. Running this though becomes highly inefficient time wise, but very precise. Another way of doing pixel collision detection is to make a List of Points on the Image that are not transparent (like the border of the actual nontransparent Image). This can be done when the Image is created. Now all you have to do is check to see if the intersecting Image has those points on it.
Hopefully this helped.
# 6
Thanks, I am already using Polygons which specify ground areas and I check collision between the player's rectangle (it is a rectangle) and each of these polygons. The problem is that every 25 milliseconds I have to test if the player's rectangle intersects with any of the polygons in my list. I was just wondering whether I am missing something since this seems very inefficient (and indeed the game seems to lag a bit).
With Thanks,
laginimaineb.
# 8
Yes, I did get to that conclusion... However, I am looking for a way to optimize my code - after all, other 2D Real-Time games don't lag and they have to perform the same actions so there must be something wrong with my code. Perhaps it will help if I explain it. Each time a unit moves - the game analyzes each point on the line between his last location and the present one and if it finds collision in a certain point it sets his location to the previous point on the line. The collision is checked by intersection with any of the Polygons in the list of Polygons which represent ground areas.
With Thanks,
laginimaineb.
# 9
To make it a bit more efficient, why dont you check collisions based on distance? you can roughly add your Polygons to a List, and group them based on points, and then only check the range of Polygons that collide.
Also the lag could just be the paint method. The paint() method was not meant to be for gaming, and can lag. look into fullScreenExclusive mode?