Jump to content

Raycasting I guess

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
Maurice_Z

Maurice_Z

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
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.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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).
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Maurice_Z

Maurice_Z

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
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:
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
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
How many objects are you going to be looking at? Less than 100 would not be an issue.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
Hmm... Have you read this tutorial?
Ray Casting Tutorial

~Aristotle said:

It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep Posted Image

#6
Maurice_Z

Maurice_Z

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts

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
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
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.

~Aristotle said:

It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep Posted Image

#8
Maurice_Z

Maurice_Z

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
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:

sx=Distance(StartCoords[],HorIntCoords[])

sy=Distance(StartCoords[],VerIntCoords[])

if sx<sy 

          RayCollisionCoords[]=HorIntCoords[]

Else

          RayCollisionCoords[]=VerIntCoords[]

EndIf

That'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
gszauer

gszauer

    Programmer

  • Members
  • PipPipPipPip
  • 113 posts
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?

~Aristotle said:

It is the mark of an educated mind to entertain a tought without accepting it
If my post was helpful, please help me build some rep Posted Image

#10
Maurice_Z

Maurice_Z

    Learning Programmer

  • Members
  • PipPipPip
  • 41 posts
I dunno, just out of curiosity I guess :).