开发者

How do I make a solid rectangle in Java? [closed]

开发者 https://www.devze.com 2023-04-02 03:24 出处:网络
It's difficult to tell what is being asked here. This ques开发者_StackOverflowtion is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current
It's difficult to tell what is being asked here. This ques开发者_StackOverflowtion 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 11 years ago.

How do I make a solid rectangle (or block) in java so my player can stand on it?

At the moment I am saving the last position, and once I detect the player's rectangle and the block's rectangle intersect...I reset the position to the previous one. But that doesn't work out well.

Is there a better way?


This is not a simple problem. There is no such thing as a solid rectangle; you have to create the illusion using a lot of code (unless you can borrow some from somewhere). My suggestion would be to use circles rather than rectangles. You can identify their location by where there centers are, and their size by their radius. (If you draw them as rectangles, my guess is no one will notice that they behave like circles.)

Now, whenever the center points of the two "rectangles" get closer than the sum of their diameters, you have a collision, and the amount you have to back each one up to prevent overlapping is easily calculated. With a bit of arithmetic (and geometry) you can make this look good. You can work back and determine when and where the collision occurred and figure the correct paths after the collision and hence the current correct location.

Get circles working and you can get back to rectangles--they're just circles with radii that change with direction. (I wouldn't bother.) But this is not easy until you have it working. Then you can use the code in a thousand places and forget what a pain it was to write it in the first place.


To draw a solid rectangle you woul go into your paint method and call fillRect.

public void paint(Graphics g) {
    g.fillRect (10, 10, 210, 230);  
}


You're asking, in a sense, how to implement some of the first basics of a two-dimensional Physics Engine.

Note that even large game studios are using engines others have built...big names like Halo, Bioshock, Assassin's Creed etc. all use "Havok":

http://en.wikipedia.org/wiki/Havok_(software)

Even in the much simpler world of 2-D platformers, it might be better to start from a bit of higher level stuff. You accept that you've been given a routine to draw rectangles and circles (instead of plotting pixels out by yourself). Why not let yourself use a sprite library someone's already made?

If you want to work with more basic physics and algorithms, there's also the likes of JBox2D, which can be fun:

http://www.jbox2d.org/

Beyond that, the answers to the following question might be useful, including some leads to platformer libraries:

Collision Detection between two images in Java

I'll also add that sometimes if you are trying to come up with a game, it can be more important to prototype and prove that your design is actually fun before going through all the trouble of writing it. Some good tools out there for that, but GameSalad is a big one I'm hearing about lately. Other resources:

http://en.wikipedia.org/wiki/Category:Video_game_creation_software


If you want to know whether two rectanges intersect you can use the Java class Rectangle. It defines a function intersect which takes another rectangle and tells you whether the two intersect. In a game you would usually calculate the new position of the player and obstacles before moving him. Then you check whether the newly calculated position would intersect with the player. If yes you do not move him if no, update the position to what you calculated.

Let us asume that you player is now at oldRect.

Rectangle oldRect;
Point moveVector;
Rectangle newRect = new Rectangle(oldRect.x+moveVector.x, oldRect.y+moveVector.y, oldRect.width, oldRect.height);
0

精彩评论

暂无评论...
验证码 换一张
取 消