Well, this problem has been making me want to kill keyboard with my head for few hours already, and I feel that if I won't put it on some other shoulders I'll get insane in an instant.
So I have 2D Array containing which squares are occupied by walls and which are not. Every square is 25x25
I have starting X and Y (sX, sY) and I have Mouse X and Y(mX, mY).
So I want to shoot a ray from (sX,sY) going through (mX,mY) (Or trying to go through) which will stop on first encountered Wall returning X and Y position of Intersection, all in one Loop. You can say Bullet instead of Ray if it makes more sense.
I tried using the Grid Traversal which can be easily found through google, but it was so incomprehensible... I'm addicted to examples.
So I need some good way to do what I explained above, but I can't come up with anything (Excepting testing pixel by pixel but I won't even consider it).
And please, if you can, give um, Good explanation, because sometimes I just can't understand something just to realize how simple it was days or weeks later.
Thanks in advance.
Raycasting I guess
Started by Maurice_Z, Nov 23 2007 12:54 PM
9 replies to this topic
#1
Posted 23 November 2007 - 12:54 PM
|
|
|
#2
Posted 23 November 2007 - 01:37 PM
What you are talking about doing is attempting to find the first object encountered along a vector. I'm going to guess that you are working on correctly displaying a map of some sort (like the original Wolfenstein 3d, etc).
(sX,sY) and (mX,mY) define a ray (vector, half-line, etc). It will have slope
m=(mY-sY)/(mX-sX),
which means it lies on the line
y-sY = m(x-sX).
To search for a collision, find the intersections between this line and the lines defined by the walls of the squares. The "actual" collision will satisfy two properties:
1) it will be on the same side of (sX,sY) as (mX,mY) and
2) of those, it will be the one that is closest to (sX,sY).
(sX,sY) and (mX,mY) define a ray (vector, half-line, etc). It will have slope
m=(mY-sY)/(mX-sX),
which means it lies on the line
y-sY = m(x-sX).
To search for a collision, find the intersections between this line and the lines defined by the walls of the squares. The "actual" collision will satisfy two properties:
1) it will be on the same side of (sX,sY) as (mX,mY) and
2) of those, it will be the one that is closest to (sX,sY).
#3
Posted 23 November 2007 - 01:55 PM
Hm but it is rather slow way, since it has to do collision detection with quite a lot objects.
And the second thing is that the walls aren't objects. The whole level array looks actually like this:
And the thing is for shots/bullets which travel all the way to the wall or enemy in one step.
And the second thing is that the walls aren't objects. The whole level array looks actually like this:
level[0] = "xxxxxxxxxxxx" level[1] = "x x" level[2] = "x x" level[3] = "x x"Of course I could write code capable of going through such array, but it would be even slower. I guess.
And the thing is for shots/bullets which travel all the way to the wall or enemy in one step.
#4
Posted 26 November 2007 - 09:16 AM
How many objects are you going to be looking at? Less than 100 would not be an issue.
#5
Posted 26 November 2007 - 12:37 PM
~Aristotle said:
It is the mark of an educated mind to entertain a tought without accepting it
#6
Posted 26 November 2007 - 12:44 PM
WingedPanther said:
How many objects are you going to be looking at? Less than 100 would not be an issue.
The number of objects is not the matter, because I won't have much use of slower script when I'll go for more CPU eating projects, so in the end I have to learn the good method sooner or later, and better sooner, because it can make me think of some other good ideas tgo other scripts :).
gszauer - I'll look through it, I never seen it before, but as far as I see it might be enough. Tomorrow though, got to get some sleep in a moment, since it is evening already here :).
#7
Posted 26 November 2007 - 01:24 PM
It should be enough, the site gives a walktrough of an entire raycasting engine. The Java Source code is also available.
Also, if you want i can recommend some good casting books.
Also, if you want i can recommend some good casting books.
~Aristotle said:
It is the mark of an educated mind to entertain a tought without accepting it
#8
Posted 27 November 2007 - 11:15 AM
Ok, I guess I understood it in about 95%, but there is still one thing bugging me:
I concentrated on "Seventh Page" because that's all I need (I guess). I understood it, excepting for this: First I find the horizontal grid intersections and when I find the collision point I go to vertical intersection. Then I compare the distances between these points and the start point. The smaller distance lets me know which point is the good one.
To make it simpler:
The question is - Is there any way to make it so both scripts are executed simultaneously... I mean, that when you look at this link, the points A,C and D are determined through testing Horizontal intersections with grid, and points B and E through Vertical. Can I make it so there is more "Complicated" way of making the code so it looks at both Horizontal and Vertical intersections at once?
Sigh, explaining it is a real pain in the butt ^^;
I concentrated on "Seventh Page" because that's all I need (I guess). I understood it, excepting for this: First I find the horizontal grid intersections and when I find the collision point I go to vertical intersection. Then I compare the distances between these points and the start point. The smaller distance lets me know which point is the good one.
To make it simpler:
sx=Distance(StartCoords[],HorIntCoords[]) sy=Distance(StartCoords[],VerIntCoords[]) if sx<sy RayCollisionCoords[]=HorIntCoords[] Else RayCollisionCoords[]=VerIntCoords[] EndIfThat's how the overall "sketch" of what I mean looks.
The question is - Is there any way to make it so both scripts are executed simultaneously... I mean, that when you look at this link, the points A,C and D are determined through testing Horizontal intersections with grid, and points B and E through Vertical. Can I make it so there is more "Complicated" way of making the code so it looks at both Horizontal and Vertical intersections at once?
Sigh, explaining it is a real pain in the butt ^^;
#9
Posted 27 November 2007 - 12:03 PM
I sort of get what you mean, but why do you want to do that?
I mean, i followed the Permadi tutorial, and ended up with a working ray-caster. Why do you want to "over-complicate" it?
I mean, i followed the Permadi tutorial, and ended up with a working ray-caster. Why do you want to "over-complicate" it?
~Aristotle said:
It is the mark of an educated mind to entertain a tought without accepting it
#10
Posted 27 November 2007 - 12:18 PM
I dunno, just out of curiosity I guess :).


Sign In
Create Account


Back to top









