Author Topic: Collision with Tangents?  (Read 5481 times)

0 Members and 1 Guest are viewing this topic.

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
Collision with Tangents?
« 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)
« Last Edit: November 20, 2007 by Clanky »
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Collision with Tangents?
« Reply #1 on: November 20, 2007 »
Do you know how to check if the centre of the circle is inside the triangle?

Jim
Challenge Trophies Won:

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Collision with Tangents?
« Reply #2 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
« Last Edit: November 22, 2007 by rain_storm »

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Collision with Tangents?
« Reply #3 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
Challenge Trophies Won:

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
Re: Collision with Tangents?
« Reply #4 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
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Collision with Tangents?
« Reply #5 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
Challenge Trophies Won:

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
Re: Collision with Tangents?
« Reply #6 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!
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Collision with Tangents?
« Reply #7 on: November 25, 2007 »
check your spelling
Challenge Trophies Won:

Offline Clanky

  • Laser Guided Memories
  • Amiga 1200
  • ****
  • Posts: 340
  • Karma: 16
  • kiss that sound that pounds your senses
    • View Profile
Re: Collision with Tangents?
« Reply #8 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!
He tilts, and his eyes are focused on the ground far below.. Wind? Angels? Men..

Offline rain_storm

  • Here comes the Rain
  • DBF Aficionado
  • ******
  • Posts: 3088
  • Karma: 182
  • Rain never hurt nobody
    • View Profile
    • org_100h
Re: Collision with Tangents?
« Reply #9 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

Challenge Trophies Won: