Ray casting intro

Hi,

Could anyone start me off with implementing ray casts? I have had a look at the manual and on various sites and forums, and while there is a little bit of information, it seems a bit too advanced or too far down the line for a beginner like me.

I hope ray casting is what I'm after anyway. I'm trying to project a beam from my sprite that will rotate with a users touch and the beam is meant to project outwards until it hits another body.

I have my sprite (called ballSprite) and its body, I can setup a revolution joint for the rotation bit, and I have my various other sprites and bodies. I just need to get the beam in-between!

I know it's rude to ask for help without any code or anything, so I apologise for that, but I just don't know where to start. I'd be very grateful if someone could point me in the right direction, so that I can attempt to write some code...

Thanks in advance...
Look for the section titled "4.6 Shape Ray Cast" in the box2d manual. It has a simple example of how you can cast a ray to check if it hits a shape, and also how you can determine the point where the shape was hit.
Maybe then you can ask something a bit more specific if you don't get how it's done. 😉
Ok, thanks for your help so far. 🙂

I've looked at the manual some more and set up the following in my timer method. I'm not sure that it's the best way to do it but it seems to give me some kind of result. The ballBody is the starting point for my raycast. At the moment otherBody is just a body that I can move around the screen. When a part of otherBody overlaps a part of a shape it outputs the location of the overlap and continues to until it is no longer overlapping. The results seem to be a bit hit and miss at the moment, as sometimes it looks like there is an obvious overlap but no result is produced. Anyway it's a start and I might be able to understand that problem better when I can get the next part working: I want there to be a visible "beam" between the p1 and p2 bodies and I want the end (p2) body not to be otherBody but instead I want the beam to extend outwards from ballBody until it hits a shape. The beam is to shoot off in one direction, which can be rotated by the user. A bit like a lighthouse search-light I guess.

Thinking that CCRibbon or CCMotionStreak might help me with a beam solution I managed to set that up, but it is kind of the opposite of what I want as it leaves a trail behind a moving body rather than projects one out from a static body (might come in handy in the future however! 😉 ). I noticed though that it trails a .png file in multiple segments. Would this be a possible way of projecting a beam, by projecting a .png image in repeated segments until it hits something? (I've no idea how I could implement that).

My other idea was to do a CCDrawLine between the start body and end body, which works but I have to know what the end body is.

Anyone have ideas for how I could proceed? Thanks.....



Code:
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {   
        if (b->GetUserData() != NULL) {
         for(b2Fixture *f = b->GetFixtureList(); f; f=f->GetNext()) {
            b2Shape *s = f->GetShape();
            b2Transform transform;
            transform.SetIdentity();
            
            b2RayCastInput input;
            
            input.p1.Set(ballBody->GetPosition().x, ballBody->GetPosition().y);
            input.p2.Set(otherBody->GetPosition().x, otherBody->GetPosition().y);
            
            input.maxFraction = 1.0f;
            
            b2RayCastOutput output;         
            
            bool hit = s->RayCast(&output, input, transform);
            
            if (hit)               
            {               
               b2Vec2 hitPoint = input.p1 + output.fraction * (input.p2 - input.p1);
               NSLog(@" HIT X: %.2f", hitPoint.x);
               NSLog(@" HIT Y: %.2f", hitPoint.y);         
            }            
         }
      }
   }
Any ideas anyone? 😕

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