LWJGL character collision

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.

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

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.

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

ok, thanks, got any ideas about collision?

Thanks,

  • Dan

There won’t be another part of the tutorial though … Too lazy to write it.

Great thanks mate,

  • Dan

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

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

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:

	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:

		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

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

Thanks so much for your help,

  • Dan

Hi,

Sorry to pester, but any ideas?

Thanks,

  • Dan

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

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 :confused: 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 :slight_smile:
Thanks,

  • Dan

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 ;D

heres the stacktrace:

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

Why can’t people post the line where the error occurred?

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



 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.

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