0

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?

chrypthic
  • 579
  • 1
  • 6
  • 15
  • 4
    Please provide an [sscce](http://sscce.org/) that exhibits the problem you describe. – trashgod Sep 04 '11 at 17:07
  • Its not a problem, I just want a solid block. – chrypthic Sep 04 '11 at 17:17
  • Yes, it *is* a problem, since we don't understand *in detail* and *exactly* what you want. – Roland Illig Sep 04 '11 at 17:19
  • I wrote how you can draw a solid rectangle. But you are talking about two rectangles. What is the second rectangle for and why do you want them to intersect. If you explain with a little more detail we might be able to help you better. – gebirgsbärbel Sep 04 '11 at 17:24
  • One rectangle is the player and one is a normal level tile (i call it a block) – chrypthic Sep 04 '11 at 17:30
  • @gebirgsbaerbel has shown you how to render a _view_ of a solid block, but you have yet to show us how you are trying to _model_ the block's solidity. – trashgod Sep 04 '11 at 17:33
  • @trashgod "At the moment I am trying to reset the position to a previous one as soon as the two rectangles intersect" – chrypthic Sep 04 '11 at 17:37
  • Please provide an [sscce](http://sscce.org/) that exhibits the problem with how "the two rectangles intersect." Alternatively, you can argue about the considerable value of reducing the problem to an [sscce](http://sscce.org/). – trashgod Sep 04 '11 at 17:45
  • @trashgod I started from scratch, so I cant give you an sscce, because there is no problem. I just want to know how to make the block solid. (when both rectangles collide, then reset the players position to where the player was before the collision). Here is my project folder from the eclipse workspace: http://cl.ly/9rJ8 – chrypthic Sep 04 '11 at 18:13
  • Here is something I just tried: for(int x=0; x < blocks.size(); x++) { Block m = (Block)blocks.get(x); Rectangle b1 = m.getBounds(); if(playerC.intersects(b1)){ crash=true; bgX=ix; }else{ ix=bgX;} } //And it doesnt work... – chrypthic Sep 04 '11 at 18:42

4 Answers4

4

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.

RalphChapin
  • 3,108
  • 16
  • 18
  • I am petty new to java and I dont get your explanation at all (sorry). Is there some sample code or could you modify my workspace thingy (above somewhere) accordingly? – chrypthic Sep 04 '11 at 18:48
  • Also where could I borrow the code from if I wanted? I have searched google for 1 day now and havent found anything. What do I have to search for? – chrypthic Sep 04 '11 at 18:50
  • Another thing: my player is 50x100px so do circles with radii even work? My tile is 30x30px. – chrypthic Sep 04 '11 at 19:07
  • @chrypthic: try looking for Collision Handling. To get a better answer than the one you have now you are going to need to know some geometry and some simple physics. There ought to be a simple book on the subject, but I don't know about it. (Does anyone else? "Game Physics for Poets"? Try googling "Game Physics", maybe.) As I said above, this is not a simple problem. – RalphChapin Sep 04 '11 at 19:35
  • @chrypthic: answering the 3rd question: circles will make everything more stable. With a player radius of 50 and a tile of radius 15 vertical stacking will be perfect. Horizontal will be worse and diagonal worse yet, but it may look okay. But you are *not* ready to try to calculate the effects of a rectangle collision. Make the circles work first. – RalphChapin Sep 04 '11 at 19:47
  • Another newb question: how do I convert everything to circles and how do I do the circle stuff? Can you give me some sample code? – chrypthic Sep 04 '11 at 19:54
  • Where can I borrow some code from? – chrypthic Sep 04 '11 at 19:55
  • @chrypthic: They're not so much circles as points. If the player is at x,y and the obstacle is at w,z, the distance between them is Math.sqrt( Math.pow(x-w,2), Math.pow(y-z,2) ). If that is less than 65 (50 + 15) you have a collision. You'll need to adjust x,y so the distance is precisely 65 to stop the player. I don't know of any code other than what's been mentioned. – RalphChapin Sep 04 '11 at 20:30
2

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);  
}
gebirgsbärbel
  • 2,327
  • 1
  • 22
  • 38
  • Not really what I mean.... I'm trying to not make the player through the block, but instead I want it to act solid (the player can stand on top of it, because the position keeps being reset). – chrypthic Sep 04 '11 at 17:41
  • ok, so you want to have a player that moves around and a block that does not let him pass? – gebirgsbärbel Sep 04 '11 at 17:44
  • Yes, thats what I mean. I have posted my workspace above. – chrypthic Sep 04 '11 at 18:16
0

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

Community
  • 1
  • 1
  • I am not allowed to use any external libraries for my project. – chrypthic Sep 04 '11 at 19:04
  • If you are doing "homework" then there is a tag for that you should use. Some people don't mind helping with it, others do mind. But if someone is going to decide to help, then they at least need to know what the rules of the assignment are. Having arbitrary rules handed down to you changes the advice someone would bother giving drastically. – HostileFork says dont trust SE Sep 04 '11 at 19:11
  • It's not homework though. It's for a project that me an my friend are making for work. – chrypthic Sep 04 '11 at 19:18
  • The rules: Don't use any external libraries, make a 2D Java game. – chrypthic Sep 04 '11 at 19:24
  • That's ridiculous. And if you *actually* work for a company that hands down arbitrary and unexplained limitations that are not explained with any business logic, then you're really just still taking a class...whose essential lesson may just be "know better than to work at places like this". – HostileFork says dont trust SE Sep 06 '11 at 08:58
0

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);
gebirgsbärbel
  • 2,327
  • 1
  • 22
  • 38
  • That's what I have at the moment, but I don't know how to move the player to the position before the collision. – chrypthic Sep 04 '11 at 21:00
  • I added a short example for you, where I use the oldRect and move it in the direction of some vector. Hope that helps. – gebirgsbärbel Sep 04 '11 at 21:31