Game Programming and Development Tools

End of Angle X,Y – Mene-Mene

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
I'm working again some more on my Programming ship battle game, and I'm working on the laser system.

This is how the laser works:
Player is facing 0/Up
Player fires laser
Laser checks for collisions within its path between the ship and the end of the angle (in this case purely up to the edge of the top wall) and if there is any, it stops at the first one, and shows a colision along with the obsticle being damaged. If no obsticles are found, it shows a colision at the wall.


My problem is say the player is facing 1, how do I calculate the path?

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
I reserve the full right to change my views/theories at any time.

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
what are you using as your algorithm? it sounds like just need some old y=mx+b stuff
Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
The algorithm is my problem, and what I need. Unfortunetly the most advanced math I get in school is the Metric system, and graphing. So I've got to learn what I can off the net.

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
I reserve the full right to change my views/theories at any time.

dartsman

Member

Posts: 484
From: Queensland, Australia
Registered: 03-16-2006
your path would just be the reverse (negative) of how you do it for 0...

ie.

pathY = pathY * -1; // simple reverse the pathY (if - make +, if + make -)...

------------------
www.auran.com

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
Okay, how do I calculate if someone is within the path? It'd be at angle 22.5 BTW.

I just checked your formula, it appears to be, dist * Abs(angle) But that can't be right. because that would mean the end X,Y would be if starting from 400,300 would be -6,750. Would you mind going into a bit more depth?

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
I reserve the full right to change my views/theories at any time.

[This message has been edited by Mene-Mene (edited February 21, 2007).]

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
What kind of shapes are in the path? Are they rectangles? Polygons? Pixels? How are you doing your level map?

If they are rects or polygons you will have to track the line intersections. If pixels, I believe you will have to do a ray trace.

------------------
Sam Washburn

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
The obsticles are images. I'll redescribe my problem. I know how to calculate the Additions to X and y. Y = Y - (Sine of (Angle - 90) * Distance) and X = X - (Cosine of (Angle - 90) * Distance). My problem I'm realizing is my distance formula requires the X and Y coordinates.

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
I reserve the full right to change my views/theories at any time.

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
Are you planning on doing bounding box (or bounding circle), or pixel perfect collisions?

------------------
Sam Washburn

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
bounding box.

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
"Of course, prayer requires that you actually take the time to listen for His answer..." - I'msold4Christ I reserve the full right to change my views/theories at any time.

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
Your best bet is to compute the intersection of the laser's line with the lines of each of the bounding boxes.

Here is a pretty cool line intersection algorithm google produced. Its written in C so you will have to port it to BM. (Good Practice )

http://www.whisqu.se/per/docs/math28.htm

------------------
Sam Washburn

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
Finally got around to converting it, some of the things didn't quite crossover, and the variables are terribly labled, (not that I'm not as bad) all the names are the same as I didn't quite understand it. here's the BM code:

Function Intersect_Lines(x0#,y0#,x1#,y1#,x2#,y2#,x3#,y3#) ' In the example it has 2 pointers, xi, and yi BM doesnt' have pointers so not sure how to work this.
Local a1#,b1#,c1#
Local a2#,b2#,c2#
Local det_inv#
'Not sure how to do matrix, it has m1, and m2

if (x1 - x0) <> 0
m1 = (y1-y0)/(x1-x0) 'Won't work as it requires a matrix
else
m1 = ie+10 'won't work as it requires a matrix, and not sure if the equation works.

' Compute constants

a1 = m1
a2 = m2

b1 = -1
b2 = -1

c1 = (y0-m1) * x0
c2 = (y2-m2) * x2

' Compute the inverse of the determinate

det_inv = 1/(a1*b2 - a2 * b1)

' use Kramers rule to compute xi and yi
' this won't work as there's no pointers in BM, and I'm not sure what it wants.

xi = ((b1 * c2) - (b2 * c1) * det_inv)
yi = ((a2 * c1) - (a1 * c2) * det_inv)

End Function 'End Intersect_lines


The things that didn't crossover well were the matrixes, the setting of an infinity variable, and pointers. Another problem is its all good and well in all, but it doesn't return any result, how am I too know what to input, and what means I had a collision?

Here's the original:


void Intersect_Lines(float x0,float y0,float x1,float y1,
float x2,float y2,float x3,float y3,
float *xi,float *yi)
{
// this function computes the intersection of the sent lines
// and returns the intersection point, note that the function assumes
// the lines intersect. the function can handle vertical as well
// as horizontal lines. note the function isn't very clever, it simply
//applies the math, but we don't need speed since this is a
//pre-processing step

float a1,b1,c1, // constants of linear equations
a2,b2,c2,
det_inv, // the inverse of the determinant of the coefficient
matrix
m1,m2; // the slopes of each line

// compute slopes, note the cludge for infinity, however, this will
// be close enough

if ((x1-x0)!=0)
m1 = (y1-y0)/(x1-x0);
else
m1 = (float)1e+10; // close enough to infinity

if ((x3-x2)!=0)
m2 = (y3-y2)/(x3-x2);
else
m2 = (float)1e+10; // close enough to infinity

// compute constants

a1 = m1;
a2 = m2;

b1 = -1;
b2 = -1;

c1 = (y0-m1*x0);
c2 = (y2-m2*x2);

// compute the inverse of the determinate

det_inv = 1/(a1*b2 - a2*b1);

// use Kramers rule to compute xi and yi

*xi=((b1*c2 - b2*c1)*det_inv);
*yi=((a2*c1 - a1*c2)*det_inv);

} // end Intersect_Lines

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
"Of course, prayer requires that you actually take the time to listen for His answer..." - I'msold4Christ
"I would much rather say that every time you make a choice you are turning the central part of you, the part of you that chooses, into something a little different from what it was before." -C.S. Lewis, Mere Christianity

I reserve the full right to change my views/theories at any time.

[This message has been edited by Mene-Mene (edited March 01, 2007).]

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
My big problem also is where is my X and Y? How am I to find out what the X and Y are?

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
"Of course, prayer requires that you actually take the time to listen for His answer..." - I'msold4Christ
"I would much rather say that every time you make a choice you are turning the central part of you, the part of you that chooses, into something a little different from what it was before." -C.S. Lewis, Mere Christianity

I reserve the full right to change my views/theories at any time.

Lazarus

Member

Posts: 1668
From: USA
Registered: 06-06-2006
The X and Y of what, Mene?
samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
First, good job on porting it. As for the matrix, that is a flaw in their posting system. the word matrix is a part of the previous line's comment.

// the inverse of the determinant of the coefficient matrix

so, m1 and m2 are simply floats, but good job recognizing that as a potential type. FYI, what tipped me off to it was that det_inv is followed by a comma not a semi. Anyhoo.

It takes two points(x,y) to plot a line. x0,y0 is a point of one of the lines x1,y1 is the other point of that line. (x2,y2),(x3,y3) are the two points for the other line. BTW, these are infinite lines, not line segments with end points.

I don't know if you noticed, but the function doesn't return true if the lines intersect and false if they don't. It returns *where* they intersect. Which could be an advantage for you since you probably want an explosion at the point where they intersect.

The pointers they are using are actually the function's result. In other words xi and yi are the point on the screen where the lines intersect. The coder is using pointers to pass back two values by changing the variables pointed to by *xi,*yi.

That's all I've got time for right now.

God Bless!

------------------
Sam Washburn

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
Ugh, actually as I was looking through this code some more, its really not what you are looking for. You need a line segment intersection snippet. Here, try this for a starter.


' Currently this function doesn't return anything. I'll leave that up to you
' x1#,y1#,x2#,y2# coordinates of first line
' a1#,b1#,a2#,b2# coordinates of second line
function Intersect_Line_Segments(x1#,y1#,x2#,y2#,a1#,b1#,a2#,b2#)

local dx#,dy#,da#,db#,o1#,o2#,s#,t#,xi#,yi#

dx# = x2# - x1#
dy# = y2# - y1#
da# = a2# - a1#
db# = b2# - b1#

o1# = (da# * dy# - db# * dx#)
o2# = (db# * dx# - da# * dy#)

if o1# = 0 then
' The segments are parallel.
' They do not intersect
else
s# = (dx# * (b1# - y1#) + dy# * (x1# - a1#)) / o1#
t# = (da# * (y1# - b1#) + db# * (a1# - x1#)) / o2#

if s# >= 0# and s# <= 1# and t# >= 0# and t# <= 1# then
' The segments intersect

' and here is how you find the point of intersection (xi#,yi#):
xi# = x1# + t# * dx#
yi# = y1# + t# * dy#
else
' the segments are not parallel, but do not intersect
end if
end if

end function

------------------
Sam Washburn

[This message has been edited by samw3 (edited March 02, 2007).]

Mene-Mene

Member

Posts: 1398
From: Fort Wayne, IN, USA
Registered: 10-23-2006
I'm being a bit pesky, and thanks for the code, but it isn't quite what I need. I've got an infinate line of which I only know the angle, and the starting points. It starts at say 0,300 and has an Angle of 90. I know that it will end up at the edge of the screen: 800,300. But if it has 89 I won't know. Sine and Cosine won't work because I don't know the distance. and they were built for a circle.

------------------
MM out-
Thought travels much faster than sound, it is better to think something twice, and say it once, than to think something once, and have to say it twice.
"Frogs and Fauns! The tournament!" - Professor Winneynoodle/HanClinto
"Of course, prayer requires that you actually take the time to listen for His answer..." - I'msold4Christ
"I would much rather say that every time you make a choice you are turning the central part of you, the part of you that chooses, into something a little different from what it was before." -C.S. Lewis, Mere Christianity

I reserve the full right to change my views/theories at any time.

samw3

Member

Posts: 542
From: Toccoa, GA, USA
Registered: 08-15-2006
Well, let me make sure I got it right.

Now, assuming an 800x600 screen with 0,0 in the top left corner.

The line segments you want to test for laser shot 1 are:
x1,y1 Player's position
x2,y2 calcuate with sin/cos using distance 2x screen width
a1,b1 Top right coordinate of wall
a2,b2 bottom right coordinate of wall

The line segments you want to test for laser shot 2 are:
x1,y1 Player's position
x2,y2 calcuate with sin/cos using distance 2x screen width
a1,b1 0,0
a2,b2 0,600

So to test if any laser shot hits anything, calculate your laser segment (x1,y1,x2,y2) and test it against all the lines in your level (walls, screen borders). In the example above there would be 8 tests, 4 for the screen boundaries, 4 for the wall (each side)

Is this what you are wanting to do?

P.S. If you are using a tile based map you could test the rectangle(four lines) of each tile that is solid.

------------------
Sam Washburn