123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|671|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Blitz -> Array-based collisions

Tue, 07 Oct 2008, 19:20
Dither
Hello! First post here.

I'm using plain old Blitz Basic 2d and I'm pretty new to programming (just started a few weeks ago).

So far, I've figured out how to load and display a tile map (loading data statements into an array), and scroll it. Then I tried tackling collisions. For simplicity, I used a single-screen map.

I created a new instance of a Tile type for each non-empty tile in the array. For collisions, I was running through every tile on the screen and checking it for a collision with the player (a simple red square). If a collision happened, I just set the player's x or y coordinate flush with the tile (depending on which way the player was moving).

This works fine as it is but I'm guessing it's not very efficient because of all the collision checks happening each loop. I've heard about array-based collision checking, which would allow for checking only those tile positions adjacent to the player, but I still don't quite understand how to implement it.

My goal with this is to make a platformer. Any help you can offer is greatly appreciated. Thanks!
Tue, 07 Oct 2008, 21:28
Dither
Thanks, Tikihead! I am copying and saving what you've written for future reference. Much appreciated!

For anyone interested, I did get the array-based collision handling to work, with some help from a friend and some serious brain-busting. I'll post the source code and images if anyone would benefit by it.

For now, off to sleep.
Fri, 10 Oct 2008, 12:43
mindstorm8191
Well, I might have something else that might help you. What I understand from your post, for each tile you have a tile type instance, but to find each tile you have to scan all tiles. There is a highly under-documented pair of commands in Blitz, called Object and Handle. The Handle function returns an integer, when you give it any type instance, and object will give you a type instance when you give it an integer. Like this:

value = handle(myinst.type)
myinst.type = object.type(value)

Now, this isn't so useful by itself, but can be useful in arrays, like you have. You can put a type instance's handle in each array slot, and manage your type instance data from there. Like so:

myinst.type = object.type(myarray(x,y))

It might not help you out too much, but it might. Blitz also lets you make arrays of types, but that's a bit more complicated in my head!

-=-=-
Vesuvius web game
Sun, 12 Oct 2008, 21:37
Dither
Thanks, mindstorm! I'm not sure if I can make use of that right now (have to give it some thought) but it's good to know.

In my original way of doing collisions, I had created an instance of the type for all walls (not the blank tiles). So I had to check all the actual walls for collisions.

The way I'm doing it now, I'm only checking 2 array positions each time the player moves. This is using an array of type. The type itself has fields for x, y and collision (is it a wall or not?).

Assuming the player's bounding box is the same size as a tile, it might collide with two tiles at any given move. So, for instance, when the player moves right, I have to check the array position overlapping the TOP RIGHT and BOTTOM RIGHT corners of the bounding box. If there's a collision, I set the player's X coordinate in relation to the tile's X coordinate, so the right side of the player's bounding box will be flush with the left side of the wall.

I'm not sure if this is the best way of doing it, but it works!