|
||||||
| Programming Theory Discuss programming theory, algorithm efficiency, logic, and other any other category where math and computer science overlap. |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Display Modes |
|
|||
|
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. |
| Sponsored Links |
|
|
|
|||||
|
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).
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||
|
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: Code:
level[0] = "xxxxxxxxxxxx" level[1] = "x x" level[2] = "x x" level[3] = "x x" And the thing is for shots/bullets which travel all the way to the wall or enemy in one step. Last edited by Maurice_Z; 11-23-2007 at 05:00 PM. |
|
|||||
|
How many objects are you going to be looking at? Less than 100 would not be an issue.
__________________
CodeCall Blog | CodeCall Wiki | Shareware | Linux Forum Programming is a branch of mathematics. |
|
|||||
|
Hmm... Have you read this tutorial?
Ray Casting Tutorial
__________________
Quote:
|
| Sponsored Links |
|
|
|
|||
|
Quote:
.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 . |
|
|||
|
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: Code:
sx=Distance(StartCoords[],HorIntCoords[])
sy=Distance(StartCoords[],VerIntCoords[])
if sx<sy
RayCollisionCoords[]=HorIntCoords[]
Else
RayCollisionCoords[]=VerIntCoords[]
EndIf
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 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?
__________________
Quote:
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
| WingedPanther | ........ | 2753.6 |
| Xav | ........ | 2704 |
| Brandon W | ........ | 1702.32 |
| John | ........ | 1207.73 |
| marwex89 | ........ | 1175.24 |
| morefood2001 | ........ | 966.05 |
| dcs | ........ | 655.75 |
| Steve.L | ........ | 475.59 |
| orjan | ........ | 418.58 |
| Aereshaa | ........ | 383.54 |