Raycasting howto

Hello,

I've been postponing this, but I need to use the Raycasting functions to create an aim-line for shooting a ball off. OR I assume I need them, but I wanted to check in here first.

I get that RaycastOne will return data which I can use to draw this line, but I don't understand how to use the data to this end.

I've also read that the TestBed shows example(s) of its' use, but I simply can't find them (just like I can't find the actual functions in the source ❓ ).

So, what I'm looking for, I suppose, is good examples of the use of Raycasting (preferrably RaycastOne).

I appreciate your time.
-Bingoboy
could you take a moment and upload a rough picture of how you expect it to look?
*since your not sure raycasting is what you need*

i can post some reference code a bit later, not at this moment though.
Absolutely! Here we go:

Image

Picture it like a Billiards table, as seen from the top down. So no gravity. I want to apply some force to the ball in order to shoot it off.

A player should be able to click and hold on a ball, at which time an 'aimline' should appear. This line should start from the mouse position, continue through the center of the ball and terminate at whichever shape it hits first.

I've tried to show how it would look just before a player would shoot the ball towards the upper right corner.

I've got eventlisteners set up for most of this functionality, but I can't figure out how to determine the end point of the aimline.
you may have to do some interpretation, I'm chopping this out of something that was built to do a bit more.
hope this helps, good luck.

Code:
var points:Array = new Array();
var zeroVec2:b2Vec2 = new b2Vec2(0,0);
var rayLength:int = 100;
var Pi2:Number = 2*Math.PI

//drawOnMC is the MovieClip you want to draw these lines on.

for(var angle:Number = 0; angle<Pi2; angle += glowIncrement){
   var segment:b2Segment = new b2Segment();
   segment.p1.SetV(m_body.GetWorldPoint(zeroVec2));
   segment.p2.SetV(m_body.GetWorldVector(new b2Vec2(Math.sin(angle)*rayLength,Math.cos(angle)*rayLength)));
   segment.p2.Add(segment.p1);
   
   var shapes:Array=new Array();
   var n=1;
   
   var lambda=[1];
   var normal:b2Vec2=new b2Vec2();
   var shape:b2Shape = m_body.GetWorld().RaycastOne(segment,lambda,normal,false,null);
   lambda=lambda[0];
   points.push(new Point(Math.sin(angle)*rayLength*30*lambda, Math.cos(angle)*rayLength*30*lambda));
}
var point0:Point = points[0];
var allPoints:int = points.length;
var p:int
var pointp:Point

drawOnMC.graphics.lineStyle(1, 0x000000, 1);
drawOnMC.graphics.moveTo(point0.x, point0.y);

//Line to mouse
//Line back to 0,0
//then line to end of raycast

for(p = 1; p<allPoints; p++){
   pointp = points[p];
   drawOnMC.graphics.lineTo(0,0);
   drawOnMC.graphics.lineTo(pointp.x, pointp.y);
}
drawOnMC.graphics.lineTo(0,0);
drawOnMC.graphics.lineTo(point0.x, point0.y);
your other post points to the importance of knowing where to get raycast from
its not included in Box2D 2.01 i suppose.
that information would be relevant to this post too.

I believe I am using a patched in version from this source
http://personal.boristhebrave.com/proje ... d-raycasts
Nods. It's hard enough without having the function itself 😉

I think it's only included from 2.1.

In any case, your code took me a good part of the way. The function seems to work, but I still don't get how you extract the point from this lambda fraction.

Here's my code:
Code:
function drawAimLine ():void
{
   //drawOnMC is the MovieClip you want to draw these lines on.   
   drawOnMC.graphics.clear();
   var rayLength:int=500;   
   var segment:b2Segment = new b2Segment();
   segment.p1.SetV (aimBody.GetPosition());
   segment.p2.SetV (new b2Vec2(mouseX, mouseY));
   
   var lambda=[1];
   var normal:b2Vec2=new b2Vec2();
   var shape:b2Shape=world.RaycastOne(segment,lambda,normal,false,null);
   lambda=lambda[0];   
   var point0:Point=(new Point(Math.sin(0)*rayLength*30*lambda, Math.cos(0)*rayLength*30*lambda));
   
   drawOnMC.graphics.lineStyle (1, 0x000000, 1);
   //drawOnMC.graphics.moveTo (point0.x, point0.y);
   drawOnMC.graphics.moveTo(aimBody.GetPosition().x*physScale, aimBody.GetPosition().y*physScale);
   drawOnMC.graphics.lineTo (mouseX, mouseY);
   //drawOnMC.graphics.lineTo (point0.x, point0.y);
   trace (point0.x, ", ", point0.y);
   
}



As you can tell, I didn't know what to do with your 'angle' variable. It seems like it's part of your specific implementation, so I took it out and just put 0 in there. It would seem this isn't the right thing to do, but I have -no clue- what to do about it.

Typical output from this function is:
Code:
0 ,  112.22663431512362
0 ,  114.19415710122973
0 ,  115.92806617632277
0 ,  112.543808683098
0 ,  110.8788925851313
0 ,  109.0873040737754
0 ,  105.65033892746786
0 ,  102.74414654523271
0 ,  96.39582517635868


Can you help me figure out what to replace that 'angle' variable with, instead of 0?
sorry this code was built for raycasting light, so it was built to increment angle by xDegrees at a time in the for loop.
angle is in degrees I'm fairly sure.

to find the angle you want, you might try
Code:
var thisBall:MovieClip; //is the ball your trying to shoot.
//get a relative vector of the ball to the mouse
var unitVector:Point = new Point(thisBall.mouseX, thisBall.mouseY)
//find its length
var vLength:Number = Point.distance(new Point(0,0), unitVector);
//devide vector by its length to make it a unit vector.
unitVector.x /= vLength;
unitVector.y /= vLength;
//reverse direction
unitVector.x *= -1;
unitVector.y *= -1;
//times length;
unitVector.x *= rayLength;
unitVector.y *= rayLength;
//the unit vector should now be the x and y where you are aiming.  //   *30 meters per inch to convert to box2d's position.

//convert points to angles and perform raycast.
var angle:Number = Math.atan2(unitVector.y, unitVector.x)
points.push(new Point(Math.sin(angle)*rayLength*30*lambda, Math.cos(angle)*rayLength*30*lambda));


//if angle isn't working out try one of these
//its too early for me to remember which type it starts in and which you need...
function toDegrees(rad:Number):Number{
    return rad*180/Math.PI;
}
function toRadians(deg:Number):Number{
    return deg*Math.PI/180;
}



good luck.
The forthcoming version 2.1 will have raycasts, which it appears you are looking at. I've recently checked in a raycast testbed, I hope that will explain things.

Note that Raycast is the important call. RaycastOne and RaycastAll just build off that - they are practically toy examples of what can be done with Raycast.
@Sketch:

Thanks for all your time. I have to admit that though I feel I should be able to put the pieces together, I can't (I think you did forget, or at least leave out, some important parts in your last example. The raycast itself, for example). I sorta expected that I wouldn't figure it out before I had to deliver the project at school, so I went ahead an did something a little simpler.
You can dl the results of my (and your) hard work from here: http://www.filefactory.com/file/a118214/n/JuggleBall.rar

Unfortunately I never realized that those .png files didn't ship with the .swf file, so I didn't have time to figure out how to include them in the executable (hint hint! 😕 ).

I did that in 5 weeks, with no prior knowledge of either AS3 or Box2D, and if nothing else, it's a testament to the power these yield, as well as the ease with which they're used. Really impressive to me.

Anyways, I have to focus on .NET in the coming time, but I'll hang around to test your patience in the future as well 😉

Again, many thanks for all your help.

@Boris: Thanks for the tip. I guess I'll have to dig up that old tortoise again.
I also wanted to know whether there's anything a noob like myself can do to help in general? I'm quite good at English, but I don't have enough programming experience to be of much help with actual coding.
Quote:
@Boris: Thanks for the tip. I guess I'll have to dig up that old tortoise again.
I also wanted to know whether there's anything a noob like myself can do to help in general? I'm quite good at English, but I don't have enough programming experience to be of much help with actual coding.

Hmm. The wiki can always do with more work. It's pretty much useless as a resource at the moment, as it doesn't mention 2.1 concepts, and doesn't even make clear what version each page is for. But it's a huge sink of work.

Original thread: https://box2d.org/forum/viewtopic.php?t=4131