I'm making an RPG, need help with collisions.

BlitzMax Forums/BlitzMax Beginners Area/I'm making an RPG, need help with collisions.

zcbeaton(Posted 2008) [#1]
Hey, I'm pretty new to BlitzMAX here. I started working on a simple "game" recently, a simple yellow rectangle that could be controlled using the arrow keys.

I've been learning BlitzMAX a bit more, and now I have it using sprites instead of a rectangle, and I added a tree.

I would like it so my character can walk around, but not THROUGH the tree. In pseudo-code I was expecting something like this:

If KeyDown(KEY_RIGHT)
If !(TheTreeIsAt(x+1)) x:+1
EndIf

I'll post my BMX file if needed.


Otus(Posted 2008) [#2]
You'll probably want to take a look at ImagesCollide.


xMicky(Posted 2008) [#3]
still one of my favorites for collission checking topics:

Looking for rotated, scaled box collisions


zcbeaton(Posted 2008) [#4]
ImagesCollide looked like I wanted, but I want it so that rather than stopping you from moving further into the tree when you collide with it, but rather preventing you from walking any closer to a tree if that would cause a collision.

In other words, rather than smacking into a wall and unable to go any further, I'd like to stop before the wall because otherwise I will smack into it.


Czar Flavius(Posted 2008) [#5]
Is this a grid-based game?


plash(Posted 2008) [#6]
Is this a grid-based game?
Or in other words, a tile-based game.

If it is tile-based you need to add a 'walkable' variable to each tile, and a bounding area for each tile.

If the player tries to move onto a tile that is not walkable move him back to the last tile before flipping the changes to the screen.


Czar Flavius(Posted 2008) [#7]
Or in other words, a tile-based game.
It was on the tip of my tongue, but the word just alluded me :)


CS_TBL(Posted 2008) [#8]
don't do this:
If KeyDown(KEY_RIGHT)
If !(TheTreeIsAt(x+1)) x:+1
EndIf


do this:
If KeyDown(KEY_RIGHT)
  MoveRight
EndIf

..

Method MoveRight() ' or a 'Function' if you're not using types
  ..
EndMethod

One well known programming style is to separate (G)UI from implementation. The advantage here is that some script can also move the player right by calling that Method or Function.

Btw, if this is your first game, first experience, first code, first whatever: know that probably all ppl here have zillions of RPG's/tilemapscrollers projects in their blitz directories. An RPG is not difficult because of its code (a raycasting engine requires more math skills than a classic/retro Zelda), but because of its vastness.


zcbeaton(Posted 2008) [#9]
Thanks for all the help, and I apologise for this late reply. The game is not based on a grid, it is a free roaming game. I think I now see how I can make this work, so no problem there.

I'll use a method for moving right and design it so that if moving right caused the player to collide with the tree, it will simply move him back before refreshing the screen, and therefore he will not pass through the tree.

I'll try to code it later today.


Link(Posted 2008) [#10]
id have to say something like imagerectcollide.
try the example
http://blitzbasic.com/b3ddocs/command.php?name=ImageRectCollide&ref=goto