forums

TNT Basic Forums > Programming Q&A
how can we put a collision with lines?
< Last Thread     Next Thread >
Author
Thread        Post A Reply

11-21-2003 16:47

Posted by:
Leorik The Creator

Location:
Montréal,Québec(canada)

Find more posts by Leorik The Creator

im using line to make wall on my game but im looking for a way to make them on a collision

ex : line 4,2 to 430,2

i use

playerx=10
playery=10
playerspeed=4

how can i do when player x and player y is on the same x and y than the line make the player not able to cross the line ?


thanks

11-21-2003 18:09

Posted by:
buddy

Location:
Champaign, IL

Click Here to Email buddy   Find more posts by buddy

I use a "Location Grid" (that's just what I call it.

This is basically a grid of whatever resolution you want to make it, that is a matrix of values. The value at any given spot in the grid indicates what is there (i.e. is it empty, is there an enemy there, is the player there, etc.)

Small example:

0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0

In this grid, perhaps the "1" represents a short wall or something.

To do this, you need:
(oops... first figure out the size of your grid. In my example, each grid cell represents one tile in my map. My map tiles are 32 pixels square, and the maps I am using in this example are 84 tiles wide by 64 pixels tall)

(a) a two-dimensional array to represent the grid in memory:
global int gLocGrid[64,84] 'NOTE that this is [rows,cols]

(b) a way to initialize the grid to initially be "empty". I personally use "-1" to indicate empty.
procedure InitLocationGrid()
int row,col
for row=0 to 63
for col=0 to 83
gLocGrid[row,col]=-1
next col
next row
end proc

(c) a way to SET the values in the grid
(I have two procedures... one to set just one cel, the other to set a range of cells all at once)

'set a range of grid values
procedure SetGridValues(int value,int x1,int y1,int x2,int y2)
'(1) resolve x and y to figure out which col / row in the grid we are setting
'(2) set the col / row to value
int startCol,startRow,endCol,endRow
int row,col,gridCell
'resolve column and row
startCol=Floor(x1/32)
startRow=Floor(y1/32)
endCol=Floor((x2-1)/32)
endRow=Floor((y2-1)/32)
'actually set the values
for row=startRow to endRow
for col=startCol to endCol
gLocGrid[row,col]=value
next col
next row
end proc

'set only one grid value
procedure SetGridValue(int value,int x1,int y1)
'(1) resolve x and y to figure out which col / row in the grid we are setting
'(2) set the col / row to value
int col,row,gridCell
'resolve column and row
col=Floor(x1/32)
row=Floor(y1/32)
'actually set the value
gLocGrid[row,col]=value
end proc

(d) a way to GET the value at a specific grid location
'return what object type is at a grid location
procedure GetGridValue(int x,int y)
int value=-1 '-1 is empty
int col,row,gridCell
col=Floor(x/32)
row=Floor(y/32)
value=gLocGrid[row,col]
end proc(value)

Then, when I am moving a player, I need to:
(1) figure out where his next POTENTIAL position will be.
(2) figure out what cell on the grid this potential postion would hit.
(3) figure out if that cell is empty or not.
(4) if the grid cell is empty, I can go ahead and set my player's ACTUAL position to the POTENTIAL position.
-- otherwise --
I go ahead and disallow the move.
==================
'NOTE that newX and newY are the POTENTIAL new postion for the player
GetGridValue(newX,newY) 'check to see what is at that position

whoIsThere=Result Int 'what is at that location on the map

'we want the player to be able to go into the following:
'a. empty spaces
'b. his own space
'c. enemy spaces
'If we don't allow him to go into his own space, he can get stuck...
'if we don't allow him to go into enemy spaces, then your collision
'detection for colliding with enemies might not get triggered

'in my example, I use these values in my grid:
'(-1) is empty
'(0) is the player
'(16 and up) are enemies

if whoIsThere<1 or whoIsThere>15
' allow the POTENTIAL position to become the REAL position
gXPosition[0]=newX
gYPosition[0]=newY
'this is just setting values for my viewport
gMainViewLocX=newViewX
gMainViewLocY=newViewY
' notify the old location that there is no player there
SetGridValue(-1,oldX,oldY)
' notify the new location that there is a player there
SetGridValue(0,newX,newY)
else
'cannot move because the player hit a boundary
'I use canMove later for some other decisions
canMove=0
end if

I hope this kind of makes sense to you.
The point of doing it this way is that if you use actual collision detection for EVERYTHING (like hitting walls etc.) things can get pretty slow.

11-21-2003 18:10

Posted by:
buddy

Location:
Champaign, IL

Click Here to Email buddy   Find more posts by buddy

CORRECTION...

84 tiles wide by 64 tiles tall)

I had put 64 pixels tall... BIG difference...

11-22-2003 21:30

Posted by:
Leorik The Creator

Location:
Montréal,Québec(canada)

Find more posts by Leorik The Creator

thanks

thanks but something that i have to say is lot of code for just that...

11-23-2003 01:21

Posted by:
buddy

Location:
Champaign, IL

Click Here to Email buddy   Find more posts by buddy

When you figure out a shorter way that is efficient for the game, please post it, because that would be great.

Thanks.

11-23-2003 16:48

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

You can use get pixel color to test for the color on the screen.

11-23-2003 20:17

Posted by:
-wyvern

Location:
Bedroom

Find more posts by -wyvern

I do that in my RPG, although it's a big inconvienience... Basically, because for some reason finding a pixel looks on canvas zero, not the current canvas. I tried using viewports, but that doesn't work at all because VIEWPORTS DON'T WORK IN HARDWARE MODE... AAAARGH! - Anyway, the thing to do is create a level image on a very large sprite, then have that sprite move in the opposite directin to the player sprite, which is static. Then, at the start of each area, you paste another, differently coloured image into the background. This one is going to be colour coded, or line drawn or whatever. You then check to see whether the point the player is supposedly on is a certain colour (with me it's red 247, 'cause photoshop imported it wrong, but that's another story) and treat that as a wall. If the walls are thin, or the player is moving at a ridiculous speed, you'll have to check all the points in a line from the old position to the new to see whether they are coloured or the player will end up flying through solid walls when their speend reaches a sufficiently alarming magnitude. The upshot of all this is that the player sees a sprite and a background (basically a big sprite) moving around, while behind the scenes the player's real x and y positions are being monitored on a map of the level, colour coded this time. No viewports, no scrolling, and if you are anything like me much frustration, but it does the job for those poor souls who's computers choke on hardware mode - I swear I've mentioned it in the bugs panel.

Wow, thanks for reading that! (I hope it made more sense to you than it does to me, but if you are really stuck, I'll post an example featuring a slice of engine from my rpg to show you how it's done.)

-wyvern

11-24-2003 15:56

Posted by:
buddy

Location:
Champaign, IL

Click Here to Email buddy   Find more posts by buddy

Good idea

That is a good idea. That's the way they proposed to do it in RealBasic, too, like in your "map" that you check for walls and stuff, having one pixel for each tile.

LOL... Yeah, I ran into the "going too fast and zipped through a wall" problem with my number matrix version, too...

"It's a feature, not a bug..."
;-)

11-24-2003 22:49

Posted by:
eekaydee

Location:
CA, USA

Click Here to Email eekaydee   Find more posts by eekaydee

:O

Wow, that's a lot of work! You must really want to have hardware mode to go through that kind of trouble!

11-25-2003 00:17

Posted by:
-wyvern

Location:
Bedroom

Find more posts by -wyvern

I certainly do.

11-26-2003 22:16

Posted by:
DanLurie

Location:
Earth>USA>New Jersey>Clifton>My Chair

Click Here to Email DanLurie   Find more posts by DanLurie

Um. Use Algebra.

11-27-2003 22:18

Posted by:
-wyvern

Location:
Bedroom

Find more posts by -wyvern

Well, that's very useful. Any kind of algebra in particular? Not to sound harsh or anything, but we need an example of how you could create a database and check all the lines for collisions using algebra - which I think could just possibly be extremely complecated (although I can't talk, I'm mister "write 400 lines of code to cover collision detection using a giant map sprite")

11-28-2003 02:24

Posted by:
DanLurie

Location:
Earth>USA>New Jersey>Clifton>My Chair

Click Here to Email DanLurie   Find more posts by DanLurie

Before I post an answer, I should ask: are your walls always perfectly vertical or horizontal?

11-29-2003 02:11

Posted by:
Leorik The Creator

Location:
Montréal,Québec(canada)

Find more posts by Leorik The Creator

yes

my wall are horizontal or vertical only

11-30-2003 16:46

Posted by:
DanLurie

Location:
Earth>USA>New Jersey>Clifton>My Chair

Click Here to Email DanLurie   Find more posts by DanLurie

Would it also be true that the character can only move vertically or horizontally, i.e., only 4 units up/down or 4 units right/left?

12-17-2003 19:34

Posted by:
Mispeled

Location:
Cincinati, Ohio

Click Here to Email Mispeled   Find more posts by Mispeled

Um, couldn't you just do:
If playerY=(Ylocation of wall) and if playerX=(Xlocation of wall)
playerY=playerY-1
playerX=playerX-1
end if

01-07-2004 20:54

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

a better way to do it is somthing like this.
i'll just show one of the walls

int x
int deltaX
int wall1

while condition
.
.
.
if x >= wall1
x = x - deltaX
end if

this way you can adjust the speed of your object without breaking your code.

01-07-2004 20:57

Posted by:
puffin

Click Here to Email puffin   Find more posts by puffin

oh, and if you have a lot of walls, like a bomberman game, it would be easier to force the player to move on paths, but i don't know what kind of game your trying to make.

All times are GMT        Post A Reply

Forum Jump:
< Last Thread     Next Thread >

< Contact Us - TNT Basic >

Powered by: vBulletin Lite Version 1.0.1 Lite
Copyright © Jelsoft Enterprises Limited 2000.