I hope this isn't a crazy question

Blitz3D Forums/Blitz3D Beginners Area/I hope this isn't a crazy question

En929(Posted 2011) [#1]
I'm using 2D BlitzBasic. I'm think there is an alternative way to do collisions. For collisions, I usually use the "If ImagesCollide" or "If ImageRectCollide" commands, but I think that there's another way to do collisions that involves some type of mathematical formula (I saw it in the "Programming for Teens" book but I don't remember it). But, I would like practice at using one like that one so that if I decide to learn another programming language I'll know how to do it in that language too. Thus, can someone tell me what it is? Thanks.

P.S. I think the collision method that I'm trying to learn uses the "And" command but I'm not sure. But, I'll be glad to learn those alternative ways to do collision.

Last edited 2011


Matty(Posted 2011) [#2]
Think a little about the logic of testing for a collision. You don't need a computer or code for this - just write it on a piece of paper - the basic lgoic behind it.

First you have an object. When the object moves you need to check for collisions. When it is stationary there is no need to check for collisions.

Which objects do you need to check for collisions then?
Well - check if the distance between the moving object and each other object in the environment is less than a certain amount - if so you have a collision.

Simply put:

When an object moves check for collision.

To check for collision - loop through all other objects and check how close they are. If closer than defined amount then a collision has occurred - you will need to determine then how you want the moving object to respond to this collision.



Regarding your comment about the "And" command - don't think of programming questions so much in terms of 'commands' or 'syntax' but in terms of the logic required to solve the problem.


Charrua(Posted 2011) [#3]
hi, checking distances asumes that each entity has a radius and it's a simple way of doing collisions.

another is to use Widht and Height, but nor the radius nor the box will work if your entities are Stars or Triangles.

probably you are remembering some trace of code in wich LowerEntity1X and LowerEntity2X where tested, the code that should implement ImageRectColide

following is a 3D function to test if 2 boxes overlaps, with little modification could be used to test 2D Rectagles overlap

Function BoxesOverlap%(x0#, y0#, z0#, w0#, h0#, d0#, x2#, y2#, z2#, w2#, h2#, d2#)
	If (x0 > (x2 + w2)) Or ((x0 + w0) < x2) Then Return False
	If (y0 > (y2 + h2)) Or ((y0 + h0) < y2) Then Return False
	If (z0 > (z2 + d2)) Or ((z0 + d0) < z2) Then Return False
	Return True
End Function



the parameters are x,y,z (center of an entity) and Width, Height, Depth
for each entity.

Juan


En929(Posted 2011) [#4]
Hi Charrua, I tried using your collision code, but I failed miserably (I'm a beginning programmer and am trying to learn). Can you write your formula into the program I wrote in this link here:

http://www.blitzmax.com/Community/posts.php?topic=95506#1101019


It's the game code at the bottom involving the PLAYER and DOOR characters. Moore also gave me a code too, but I failed in implementing that one as well. Hopefully Moore or someone could show me how to use that one too.

Thanks