Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / 2D dot line collision

Author
Message
Wiley85
17
Years of Service
User Offline
Joined: 9th Jan 2009
Location:
Posted: 7th Feb 2009 16:39
Hello everybloody! I've a new problem concerning basic 2d drawing collisions. Specifically, I've a program that launches a dot line on a projectile path dependant on user input. I want the program to be able to detect when the dot line has collided with any of the lines I've use to create a basic terrain. ANy suggestions?
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 7th Feb 2009 18:21
So you're drawing the projectile's path using a dotted line?

You can loop through all the line segments which make up your terrain and check to see if a dot is below that line or not.

There is an equation for detecting that, but I don't know it off hand. I have it written down at home, but won't be there until tomorrow.

Your signature has been erased by a mod because it's larger than 600x120
Ashingda 27
18
Years of Service
User Offline
Joined: 15th Feb 2008
Location:
Posted: 7th Feb 2009 19:52
Have you tried using the point(x,y) function? It returns a color on the screen.
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 8th Feb 2009 07:17
y=mx+b (or for simplicity y-y1=m(x-x1))

so:

ya=m1(x-x1)+y1
yb=m2(x-x2)+y2

but ya and yb will be the same where they intersect so

m1(x-x1)+y1=m2(x-x2)+y2

m1x-m1x1+y1=m2x-m2x2+y2
m1x-m2x=m1x1-m2x2-y1+y2

x=(m1x1-m2x2-y1+y2)/(m1-m2)
then

y=m1(x-x1)+y1


Key:
m1=slope of line 1
m2=slope of line 2
(x1,y1)=a point on line 1
(x2,y2)=a point on line 2

(x,y)=the point of intersection.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Wiley85
17
Years of Service
User Offline
Joined: 9th Jan 2009
Location:
Posted: 9th Feb 2009 03:18
Ok, it's a mess, but here's what I've come up with so far. I couldn't figure out how to implement any of those formulas though. Check it out!

BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 9th Feb 2009 04:17
Here is a physics formula that you can use to calculate a projectile's path:

S=.5*GRAVITY*TIME^2+VELOCITY INITIAL*sin(ANGLE)*TIME+S ORIGINAL

Or, in a more friendly form:

Y=.5*G*(X-X Initial)^2+Vi*sin(ANGLE)*(X-X Initial)+Y Initial

Where X Initial and Y Initial are the starting points (the player locations).

LONG NOTE: The Velocity is multiplied by the Sine of the angle because of this:

The velocity is the speed along the line of action (the angled line). However, you need the x and y components of that velocity to do anything with it (since all other measurements are taken in x and y parts). So, using a bit of trig (if you don't know anything about trig, you can read on if you want, or you can just accept the fact), we can see that the y component is the hypotenuse of a triangle multiplied by the sine of the angle (remember, Sin(angle)=opposite/hypotenuse therefore hypotenuse*sin(angle)=opposite). Here is a diagram to help illustrate.



So, the opposite side from the angle of 30 degrees is the red side. Therefore, according to the formula, Red=black*sin(30), or Red=10*sin(30). Using a calculator we can find that sin(30)=.5, thereofre RED=10*.5 thus RED=5.
[/LONG NOTE]

Get it?

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Wiley85
17
Years of Service
User Offline
Joined: 9th Jan 2009
Location:
Posted: 9th Feb 2009 15:17
I do. That sounds much less messy than what I've done. I have taken a trig class and that's how I came up with my formulas. It looks like your formula would just find the y position, so for x I just use the same formula with cosine instead and without gravity correct?
BN2 Productions
22
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 10th Feb 2009 08:25
With x you just treat it like time, so that you can increase it by a fixed amount.

Alternatively, you can use

S=V*cos(angle)*Time+S INIT

But that would require you to time the shots. It is easier just to use the difference in x positions as the time and increment the x position by a fixed amount.

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose

Login to post a reply

Server time is: 2026-07-11 18:05:14
Your offset time is: 2026-07-11 18:05:14