Java-Gaming.org Hi !
 Featured games (84) games approved by the League of Dukes Games in Showcase (575) Games in Android Showcase (154) games submitted by our members Games in WIP (623) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
 Home Help Search Login Register
Pages: [1]
 ignore  |  Print
 not detecting collision with keybindings?  (Read 1502 times) 0 Members and 1 Guest are viewing this topic.
catsaremyreligion

Senior Newbie

 « Posted 2013-09-26 20:00:47 »

So in my little side-scroller Java game I'm attempting to make collisions for my character against walls. I am using keybindings instead of key listeners because of the focusing issues it often caused. Here's the code I'm having trouble with:

 1  2  3  4  5  6  7  8 ` Action moveleft = new AbstractAction(){      public void actionPerformed(ActionEvent e)      {          if (moving==true&&offsetX!=2){              offsetX=offsetX+2;          }      }    };`

The problem is that when a key is held down to make the character move, in the actionperformed method, it doesn't recheck the conditional statement that determines whether moving is true or false when the key is held down. This results in the character moving indefinitely through all walls until the key is released.

So why is this? What can I do to remedy it? If any more code is required from my program I can provide! Thanks!
wessles

JGO Kernel

Medals: 114
Projects: 4
Exp: 3 years

Enthusiast of amusement. Lover of code.

 « Reply #1 - Posted 2013-09-26 20:16:27 »

I wouldn't make moving left an abstractualized 'Action.' I would just have a method like this:

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15 `public void move(float movex, float movey) {    // Hypothetical positions that you could be in    Rectangle collisionx = new Rectangle(x+movex, y);    Rectangle collisiony = new Rectangle(x, y+movey);    // Check for hypothetical x    if(/* No Collision by collisionx */)        x = collisionx.getX();    // Check for hypothetical y    if(/* No Collision by collisiony */)        y = collisiony.getY();    // So what we did was 'if the new x value will make you collide, don't take x,' and did the same check for y.}`

That is how I handle collision (only I have delta, but you should be able to figure that out  ). Just methods. You could make an interface if you really want to encapsulate(?) it, like:

 1  2  3 `interface Moveable {    public void move(float movex, float movey);}`

Check out my twitter to hear more about RFLEX. My Twitter
opiop65

JGO Kernel

Medals: 166
Projects: 7
Exp: 4 years

JumpButton Studios

 « Reply #2 - Posted 2013-09-27 01:20:36 »

I wouldnt use an interface, just stick it in a entity class and let the sub classes inherit the move method. I dont know why, that what I always do. I've never used an interface in game programming before, however I have in other stuff.

 Games published by our own members! Check 'em out!
wessles

JGO Kernel

Medals: 114
Projects: 4
Exp: 3 years

Enthusiast of amusement. Lover of code.

 « Reply #3 - Posted 2013-09-27 02:24:15 »

I like to use them (not often, but) when I want to have some kind of trait for the object. Like it could inherit Textured, Moveable, and Mortal. That way, instead of with extending a class, I could reference it as a Textured, Moveable, or Mortal. I think of it as the class that keeps on extending (minus variables, but getters and setters work...).

Check out my twitter to hear more about RFLEX. My Twitter
opiop65

JGO Kernel

Medals: 166
Projects: 7
Exp: 4 years

JumpButton Studios

 « Reply #4 - Posted 2013-09-27 02:29:32 »

Yeah I guess, but I hate having two classes that could be molded into one. I see no huge difference between interfaces and extending when I'm working with a basic entity system, but maybe when I try something a little more advanced, interfaces will come in handy!

catsaremyreligion

Senior Newbie

 « Reply #5 - Posted 2013-09-27 14:55:29 »

Hey thanks a whole lot! This is doing the trick for right now, so I hope it'll continue to work. I know I'm probably setting up my movement in the least efficient way possible, but I actually have an arraylist of all blocks in the level and a for-loop statement that checks to see if the player's min x is touching the max y of each block inside the move method you just posted.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17 `public void mover(float movex, float movey) {        // Hypothetical positions that you could be in        int collisionx = (int)(offsetX+movex);        int collisiony = (int)(offsetY+movey);        for(int i=0;i

It's working for my purposes right now, but I'd really appreciate feedback on how to make it more, I don't know, professional I guess?
wessles

JGO Kernel

Medals: 114
Projects: 4
Exp: 3 years

Enthusiast of amusement. Lover of code.

 « Reply #6 - Posted 2013-09-27 19:55:06 »

Well, could you explain some about your code? Like what the for loop does, what it iterates, what the offset is, etc.

Check out my twitter to hear more about RFLEX. My Twitter
catsaremyreligion

Senior Newbie

 « Reply #7 - Posted 2013-09-27 20:07:40 »

Well, could you explain some about your code? Like what the for loop does, what it iterates, what the offset is, etc.

Yeah sure. So I have a text file that has the test level saved into it. The Level class reads the file and adds blocks to an arraylist. Each block contains its own coordinates. The player is stationary in my game, so I use the offsetX to scroll every object that isn't the player, including the background and everything. So the iteration that I posted goes through the arraylist to check and see if the player is making contact with any block. If it isn't, then the offset is changed to scroll the objects in whatever direction around the player. I hope this explanation was informative enough.
wessles

JGO Kernel

Medals: 114
Projects: 4
Exp: 3 years

Enthusiast of amusement. Lover of code.

 « Reply #8 - Posted 2013-09-27 20:26:43 »

The player is stationary in my game, so I use the offsetX to scroll every object that isn't the player, including the background and everything.
What? Sorry, but I think that you should know that doing one movement equation is way better than doing one for every object including the background. Seriously, just move the player. I guarantee that it will be more organized and faster.
Then, you could just do pretty much exactly what I gave as an example.

If you are working in java2d, see this for moving a camera (with an entertaining 'thank you').

Alternatively, (and IMO easier) lwjgl has a glTranslate function, if you are using lwjgl.

Hope this helps!

EDIT:
Pro-Tip : Use delta!

Check out my twitter to hear more about RFLEX. My Twitter
Jimmt
« League of Dukes »

JGO Kernel

Medals: 159
Projects: 4
Exp: 3 years

 « Reply #9 - Posted 2013-09-28 21:03:45 »

Moving the player and moving the background provide two different types of games. They are not the same thing.
 Games published by our own members! Check 'em out!
wessles

JGO Kernel

Medals: 114
Projects: 4
Exp: 3 years

Enthusiast of amusement. Lover of code.

 « Reply #10 - Posted 2013-09-29 03:12:00 »

Quote
scroll every object that isn't the player,
He is moving the background and the player.

Check out my twitter to hear more about RFLEX. My Twitter
Jimmt
« League of Dukes »

JGO Kernel

Medals: 159
Projects: 4
Exp: 3 years

 « Reply #11 - Posted 2013-09-29 03:17:49 »

"The player is stationary in my game"
catsaremyreligion

Senior Newbie

 « Reply #12 - Posted 2013-10-03 20:18:08 »

Sorry I don't know if this was a mis-communication or something, but yeah the player stays at the same coordinates on the screen constantly, while other blocks and the background are moving. Like, I dunno, Super Mario I believe?
wessles

JGO Kernel

Medals: 114
Projects: 4
Exp: 3 years

Enthusiast of amusement. Lover of code.

 « Reply #13 - Posted 2013-10-03 20:29:49 »

Wait, do you mean screen or universal coordinates when you say he doesn't move?
If it is screen coords, then I stand corrected.

Check out my twitter to hear more about RFLEX. My Twitter
catsaremyreligion

Senior Newbie

 « Reply #14 - Posted 2013-10-03 20:32:37 »

The player stays at the same screen coordinates.
opiop65

JGO Kernel

Medals: 166
Projects: 7
Exp: 4 years

JumpButton Studios

 « Reply #15 - Posted 2013-10-03 20:37:02 »

Well, he technically does. You should still be changing his coordinates though. I would recommend using AABBs, HeroesGraveDev posted a good explanation on them here. They basically just check the player position against the position of a axis aligned object. Its really very simple!

catsaremyreligion

Senior Newbie

 « Reply #16 - Posted 2013-10-03 20:50:29 »

Alright thanks a lot. I checked it out and have a few questions because I can be super slow sometimes. So would the AABB in the explanation be like the player and the blocks in my game? Do I find the center and size of them based on the universal coordinates?
opiop65

JGO Kernel

Medals: 166
Projects: 7
Exp: 4 years

JumpButton Studios

 « Reply #17 - Posted 2013-10-03 21:52:28 »

Yes, the player and the tiles and whatever else in you game that isn't rotated at all can use the AABB method. Thats very important, you cant use it if you rotate the object at all, or else you'll come up with bad results and you'll want to perform pixel perfect collisions then. And by coordinates for them, do you mean the coordinates of the tile? If thats what you mean, you only need 4 coordinates(2 actually). You'll need the x and y world coordinates, and then you'll need the width and height of the tiles. Those are the only coordinates you'll need. I could help if up posted some code!

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 ClaasJG (21 views) 2015-04-27 13:36:51 BurntPizza (33 views) 2015-04-23 03:42:11 theagentd (35 views) 2015-04-22 16:23:07 Riven (50 views) 2015-04-16 10:48:47 Duke0200 (59 views) 2015-04-16 01:59:01 Fairy Tailz (42 views) 2015-04-14 20:13:12 Riven (45 views) 2015-04-12 21:36:37 bus hotdog (61 views) 2015-04-10 02:39:32 CopyableCougar4 (66 views) 2015-04-10 00:51:04 BurntPizza (71 views) 2015-04-06 22:06:58
 theagentd 23x BurntPizza 17x wessles 15x alwex 11x 65K 11x kingroka123 11x kevglass 8x Olo 7x Ecumene 7x ra4king 7x Rayvolution 7x Riven 7x chrislo27 7x Hanksha 7x KevinWorkman 6x Spasi 6x
 How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00List of Learning Resources2014-08-05 19:33:27
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org