Dark Bit Factory & Gravity

PROGRAMMING => Other languages => Yabasic => Topic started by: Clanky on November 20, 2007

Title: Collision with Tangents?
Post by: Clanky on November 20, 2007
I was just wondering if anyone could knowledge me about tangent collisions.
Like, for instance, a collision between a point and a side of a triangle.
I need it for my entry for the 2007 Christmas Compo...
Not trying to give too much away, but a collision between the sides of trees (each with different hypontinu (spell check) values) and a point (chosen by the d-pad... if you get me!?).
The calculation will return true if it hits inside the triangle (in yabasic any specific value, say... 1 or the_is_so_true), or false if it lays outside the triangle.

Thanks! (easy language so I can implement ;D lol)
Title: Re: Collision with Tangents?
Post by: Jim on November 20, 2007
Do you know how to check if the centre of the circle is inside the triangle?

Jim
Title: Re: Collision with Tangents?
Post by: rain_storm on November 22, 2007
take the crossproduct of the point against each edge and if all edges return negative you have a collision

px,py ... your point
x1,y1,x2,y2,x3,y3 ... your triangle

Code: [Select]
  crosspoduct = (x1-px)*(y2-py) - (x2-px)*(y1-py)
  if (sig(crossproduct) < 0) then
    crosspoduct = (x2-px)*(y3-py) - (x3-px)*(y2-py)
    if (sig(crossproduct) < 0) then
      crosspoduct = (x3-px)*(y1-py) - (x1-px)*(y3-py)
      if (sig(crossproduct) < 0) then
        rem collision detected
      endif
    endif
  endif

check out my thread on circle to line collisions I have posted a lot of subroutines there that may interest you
Title: Re: Collision with Tangents?
Post by: Jim on November 22, 2007
I bet your tree looks like this
/\
in which case, collision with the bottom is easy
Code: [Select]
if circle.y+radius < bottomoftree.y then
'clunk
fi
Then the other sides have a gradient (rise over run, remember from school?)
x along, y up (x,y).
Then the 'normal' to the side is (-y,x)
You need to normalise that
L = 1/sqr(x*x+y*y)
X = -y*L
Y =  x*L

You need to check if
length(radius*X+treex-circlex, radius*Y+treey-circley) < 0

Jim
Title: Re: Collision with Tangents?
Post by: Clanky on November 23, 2007
ahh - Gradient lol.
Yep remember that from year 9  ;)
haha.
I just didn't know how to... normalise and make an equation to make a colision detection out of it :P
Thanks guys!
I'll definately check out the collision post by you rain! Always an interesting read your posts :P
Title: Re: Collision with Tangents?
Post by: Jim on November 23, 2007
Both methods should work.  Rain's is a 2d crossproduct - very, very useful thing.  It tells you the area of a 2d triangle.

Jim
Title: Re: Collision with Tangents?
Post by: Clanky on November 25, 2007
Ohk!
Tried your code out rain... and it is peculiar!
Is the naming of x1, y1, etc. important?
I named as:
/\
Code: [Select]
going clockwise, up, then down...
   2
1     3

I also did (since there are multiple trees):
Code: [Select]
for i = 1 to trees
crosspoduct = ((x(1, i) - px) * (y(2, i) - py)) - ((x(2, i) - px) * (y(1, i) - py))
if sig(crossproduct) < 0 then
crosspoduct = ((x(2, i) - px) * (y(3, i) - py)) - ((x(3, i) - px) * (y(2, i) - py))
if sig(crossproduct) < 0 then
crosspoduct = ((x(3, i) - px) * (y(1, i) - py)) - ((x(1, i) - px) * (y(3, i) - py))
if sig(crossproduct) < 0 then
        rem collision detected
fi
fi
fi
next
As I believe you cant achieve all tree collisions without x(n, i) etc.
Have I done this wrong?

I said (when a collisions was detected) to fill the screen with 0,0, 640,512 rectangle... it works... just not in the areas of the triangles.

Thanks!
Title: Re: Collision with Tangents?
Post by: Jim on November 25, 2007
check your spelling
Title: Re: Collision with Tangents?
Post by: Clanky on November 26, 2007
Oh. Yeah sorry.
Thats not off my code (hopefully) I just wrote it up... as my program is on my non-internet lap top.
Sorry.

I will, however, check the spelling on my lap top :P
Thanks Jim!
Title: Re: Collision with Tangents?
Post by: rain_storm on November 26, 2007
@CLANKY that looks alright to me just make sure that the vertices are in clockwise order.
@Jim I gotta try that meathod out it looks like a sweet solution