Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
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  
  LWJGL character collision  (Read 1436 times)
0 Members and 1 Guest are viewing this topic.
Offline GamerIDGoesHere

Junior Duke





« Posted 2014-02-16 05:17:37 »

Hi,

I am using a randomly generated simplex noise world for a 2D sidescroller, I cant seem to draw my character on the scene, and also I dont know where to start with collision,

Here is my world/random terrain drawing code:
http://pastebin.java-gaming.org/715c20a3f8e

Here is my character code:
http://pastebin.java-gaming.org/2afe5413c8f

Any help is greatly appreciated as I am new to LWJGL and using SimplexNoise maps,
Thanks,
- Dan

EDIT: I have the player drawing now, and I nearly have the collision working, but not quite. Scroll down for everything.
Offline nerb
« Reply #1 - Posted 2014-02-16 05:29:31 »

Re: The character. You appear to launch into your main loop from the RandomTerrain constructor. The main loop calls RandomTerrain.tick() and RandomTerrain.render(). I can't see anywhere in the provided code, particularly in the loop, where you call MainPlayer.draw(), possibly why it isn't rendering.

By the way, I wouldn't launch the loop from within the RandomTerrain constructor, it seems bad form to me. You could remove the call from within the constructor, and call it from elsewhere once the object has been instantiated. I.e. the object that makes the new RandomTerrain();/your main class could subsequently call randomTerrain.loop();, and launch the loop.
Offline GamerIDGoesHere

Junior Duke





« Reply #2 - Posted 2014-02-16 05:39:08 »

Re: The character. You appear to launch into your main loop from the RandomTerrain constructor. The main loop calls RandomTerrain.tick() and RandomTerrain.render(). I can't see anywhere in the provided code, particularly in the loop, where you call MainPlayer.draw(), possibly why it isn't rendering.

By the way, I wouldn't launch the loop from within the RandomTerrain constructor, it seems bad form to me. You could remove the call from within the constructor, and call it from elsewhere once the object has been instantiated. I.e. the object that makes the new RandomTerrain();/your main class could subsequently call randomTerrain.loop();, and launch the loop.

Thanks, its now drawing the player, however I couldnt figure this out before, but for some reason the player moves really slow, and the physics are inverted, so it falls up instead of down, ideas?

and I'll have a look at calling loop from the main class after I get this working,
Thanks,
- Dan
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline nerb
« Reply #3 - Posted 2014-02-16 06:28:32 »

and the physics are inverted, so it falls up instead of down

I can't see anything glaringly obvious in your MainPlayer class that could be causing this. Can I suggest that you perhaps do some println's on your x, y, xspeed and yspeed variables in the logic() method, and compare the values to what you expect. If the values are correct, then it could be an issue with rendering; but explore this option later.

for some reason the player moves really slow

Slow as in 'smooth but slow'? Then the easiest answer is to increase your speed variables.

Or slow as in 'clunky'? This isn't so easy. Currently I can't see where or how you are controlling timing. It appears your game loop is running flat-out (i.e. not limited by Thread.sleep() or Display.sync()). Furthermore, you are using fixed updates, so that each time through the loop, the players speed and position is updated irrespective of how long the last loop took. This generally leads to choppy movement. In many cases, a game loop implements a timing mechanism, so that movement can be smoothed, due to the irregular time of updates. It also ensures that the scene appears to move at the same rate, independent of how many FPS/UPS you are running at. There are plenty of resources on this forum about game loops and timing, which a quick search will reveal. (There is a good tutorial floating around from memory).

PS. Here's the tutorial I was thinking of: http://www.java-gaming.org/topics/game-loops/24220/view.html
Offline GamerIDGoesHere

Junior Duke





« Reply #4 - Posted 2014-02-16 06:33:37 »

ok, thanks, got any ideas about collision?

Thanks,
- Dan
Offline trollwarrior1
« Reply #5 - Posted 2014-02-16 07:35:20 »

ok, thanks, got any ideas about collision?

Thanks,
- Dan

http://www.java-gaming.org/topics/2d-collision-detection/31488/view.html

There won't be another part of the tutorial though .. Too lazy to write it.
Offline GamerIDGoesHere

Junior Duke





« Reply #6 - Posted 2014-02-16 07:42:37 »


Great thanks mate,
- Dan
Offline GamerIDGoesHere

Junior Duke





« Reply #7 - Posted 2014-02-16 08:48:54 »

Hi trollwarrior1,

I had a go at using that code with the other code I have which you helped me write, and I came across issues when trying to add CanMove to my player class, because the pixel precision isnt working due to conflicts with the way RandomTerrain is drawing tiles, this is about as far as I got:

Tile class:
http://pastebin.java-gaming.org/afe515c3f8b

Player class (with canMove not working):
http://pastebin.java-gaming.org/fe51c6f3b89

and updated RandomTerrain class:
http://pastebin.java-gaming.org/e51cf7b3988

Thanks and sorry for my slow ability to understand,
- Dan
Offline trollwarrior1
« Reply #8 - Posted 2014-02-16 14:55:44 »

You're just writing code which is beyond your ability to do so.

in canMove method, you shouldn't be called canMove. You should be calling canMoveP
Offline GamerIDGoesHere

Junior Duke





« Reply #9 - Posted 2014-02-17 02:28:22 »

I understand most of it and am trying to learn the rest, and I noticed the canMove thing and changed it to canMoveP,

However in that tutorial you check for collision using this:
1  
2  
3  
4  
5  
   private boolean canMoveP(int x, int y) {
      // We devide to convert from pixel precision to tile precision.
      return RandomTerrain.getTile(x / RandomTerrain.TILE_SIZE, //Not sure what I should put here
            y / RandomTerrain.TILE_SIZE).canMove();
   }


Now this is the array that is being drawn in RandomTerrain, however if I put it in a getTile() function, then it still doesnt work:
1  
2  
3  
4  
5  
6  
7  
8  
9  
      for (int y = 0; y < height; y++) {
         for (int x = 0; x < width; x++) {

            if (tiles[x + y * width] == 2) {
               render(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE, 0.3f, 0.3f, 0.3f); // render rock tile
            }

         }
      }


So my question is this: How should I be checking for tile size using canMoveP?

Thanks and sorry I'm a bit slow, but I do catch up after a bit,
- Dan
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GamerIDGoesHere

Junior Duke





« Reply #10 - Posted 2014-02-18 01:09:22 »

Sorry to pester, in a bt of a rush, did you see my reply?

Thanks so much for your help,
- Dan
Offline GamerIDGoesHere

Junior Duke





« Reply #11 - Posted 2014-02-21 10:05:19 »

Hi,

Sorry to pester, but any ideas?

Thanks,
- Dan
Offline opiop65

JGO Kernel


Medals: 156
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #12 - Posted 2014-02-21 16:53:07 »

Dan, no offense but you need to start at the basics. Obviously this is above your head (as other people have pointed out). Right now it seems that you are just copying code from other people and trying to make it work which is the wrong way to program. Write your own code so you understand what its doing, and so you learn when you complete your task.

Offline GamerIDGoesHere

Junior Duke





« Reply #13 - Posted 2014-02-21 21:20:03 »

Dan, no offense but you need to start at the basics. Obviously this is above your head (as other people have pointed out). Right now it seems that you are just copying code from other people and trying to make it work which is the wrong way to program. Write your own code so you understand what its doing, and so you learn when you complete your task.

Ok then got any good places for me to start? (Prefer online things to books)

Thanks,
- Dan
Offline GamerIDGoesHere

Junior Duke





« Reply #14 - Posted 2014-03-02 09:32:07 »

Hey guys,

So I decided to have another proper go today, and I got really really close to making it work, no launch errors now, but canMoveP is null :/ and I get this as well ExceptionInInitializerError in RandomTerrain

Here is my modified RandomTerrain class:
http://pastebin.java-gaming.org/8f948155e83
the Player class:
http://pastebin.java-gaming.org/f94852e5382
and the Tile class:
http://pastebin.java-gaming.org/9485e335280

Sorry if I've missed something obvious,
Ideas/Suggestions would be nice Smiley
Thanks,
- Dan
Offline Gibbo3771
« Reply #15 - Posted 2014-03-02 10:20:01 »

Lol you are biting off a bit more than you can chew Dan, take your time and focus more on debugging.

People are glad to help, I get what it is like when you have a problem you just can't figure out buy having people tell you it won't help you improve. It just makes lazy lol.

Post the stacktrace.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline GamerIDGoesHere

Junior Duke





« Reply #16 - Posted 2014-03-02 10:21:45 »

Lol you are biting off a bit more than you can chew Dan, take your time and focus more on debugging.

People are glad to help, I get what it is like when you have a problem you just can't figure out buy having people tell you it won't help you improve. It just makes lazy lol.

Post the stacktrace.

Yeah I know, I'm one for taking on challenges that are a bit over my head  Grin

heres the stacktrace:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
Exception in thread "main" java.lang.ExceptionInInitializerError
   at ms.nac.spacejunk.gen.RandomTerrain.makeTiles(RandomTerrain.java:47)
   at ms.nac.spacejunk.gen.RandomTerrain.<init>(RandomTerrain.java:27)
   at ms.nac.spacejunk.main.Game.render(Game.java:148)
   at ms.nac.spacejunk.main.Game.<init>(Game.java:79)
   at ms.nac.spacejunk.main.Game.main(Game.java:156)
Caused by: java.lang.NullPointerException
   at ms.nac.spacejunk.player.MainPlayer.canMoveP(MainPlayer.java:94)
   at ms.nac.spacejunk.player.MainPlayer.canMove(MainPlayer.java:87)
   at ms.nac.spacejunk.player.MainPlayer.logic(MainPlayer.java:74)
   at ms.nac.spacejunk.player.MainPlayer.draw(MainPlayer.java:132)
   at ms.nac.spacejunk.gen.RandomTerrain.loop(RandomTerrain.java:71)
   at ms.nac.spacejunk.gen.RandomTerrain.<init>(RandomTerrain.java:29)
   at ms.nac.spacejunk.logic.Tile.<init>(Tile.java:15)
   at ms.nac.spacejunk.logic.Tile.<clinit>(Tile.java:8)
   ... 5 more
AL lib: (EE) alc_cleanup: 1 device not closed


Thanks,
- Dan
Offline trollwarrior1
« Reply #17 - Posted 2014-03-02 10:23:00 »

Why can't people post the line where the error occurred?
Offline Gibbo3771
« Reply #18 - Posted 2014-03-02 10:33:41 »

There is something wrong with your Tile generation, it's not my code and hard for me to debug.

1  
2  
3  
4  
5  
 private static boolean canMoveP(int x, int y) {
      // We divide to convert from pixel precision to tile precision.
      return RandomTerrain.render(x / RandomTerrain.TILE_SIZE, //Not sure what I should put here
            y / RandomTerrain.TILE_SIZE, RandomTerrain.TILE_SIZE, RandomTerrain.TILE_SIZE).canMove();
   }


When this is calling RandomTerrain.render(), something is nulling. The stacktrace just says null and it is not very precise as you are not handling any exceptions yourself.

Although it looks like the problem lies in the canMoveP it probably goes a little deeper, that methods is trying to call something that is null.

Try throwing in some != null checks and see if you can at least get it past that part, from there you can figure out what exactly is nulling.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline 65K
« Reply #19 - Posted 2014-03-02 10:39:43 »

Add an exception breakpoint for NullpointerException to your IDE, start the debugger and happily stop on the line of code which causes the problem.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (43 views)
2014-10-17 03:59:02

Norakomi (33 views)
2014-10-16 15:22:06

Norakomi (26 views)
2014-10-16 15:20:20

lcass (30 views)
2014-10-15 16:18:58

TehJavaDev (60 views)
2014-10-14 00:39:48

TehJavaDev (60 views)
2014-10-14 00:35:47

TehJavaDev (50 views)
2014-10-14 00:32:37

BurntPizza (66 views)
2014-10-11 23:24:42

BurntPizza (38 views)
2014-10-11 23:10:45

BurntPizza (80 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!