Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8995959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:41:21+00:00 2026-06-15T23:41:21+00:00

I’ve tried a few different methods gleaned from the internet about how to set

  • 0

I’ve tried a few different methods gleaned from the internet about how to set up raycasts, but no matter what I do I just can’t get the thing to register any kind of connection. What’s going on here? The world is set up correctly (as in, things appear and collide and whatnot), but the raycast never seems to detect anything and I try three different methods (two slightly different). For completeness, I am going to include my testcase and the affiliated classes (some of you will probably recognize setups from Ray Wenderlich):

ccLayer header

//B2dtest.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "Box2D.h"
#import "GLES-Render.h"
#import "MyContactListener.h"
#define PTM_RATIO 16
@interface B2dTest : CCLayer {
b2World* world;                 // strong ref
GLESDebugDraw *m_debugDraw;
MyContactListener *_contactListener;
b2Body* groundBody;
b2Body* wallBody;
DataModel *m;
float rayAngle;
b2Vec2 p1;
b2Vec2 p2;
b2Vec2 intersectionPoint;
}
+(CCScene *) scene;
-(void)initPhysics;
@end

ccLayer class (offending code most likely in update)

//B2dtest.mm
#import "B2dTest.h"
#import "RaysCastCallback.h"
#import "OtherCast.h"
enum {kTagParentNode = 1,};

@implementation B2dTest
+(CCScene *) scene
{
CCScene *scene = [CCScene node];
B2dTest *layer = [B2dTest node];
[scene addChild: layer];
return scene;
}
-(id) init
{
if( (self=[super init])) {
    [self initPhysics];
    [self addBarrier];
    [self scheduleUpdate];
}
return self;
}

-(void) initPhysics
{
CGSize s = [[CCDirector sharedDirector] winSize];
b2Vec2 gravity;
gravity.Set(0.0f, -21.0f);  //was ten
world = new b2World(gravity);
_contactListener = new MyContactListener();
world->SetContactListener(_contactListener);
world->SetAllowSleeping(true);
world->SetContinuousPhysics(true);  
m_debugDraw = new GLESDebugDraw( PTM_RATIO );
world->SetDebugDraw(m_debugDraw);
uint32 flags = 0;
flags += b2Draw::e_shapeBit;
m_debugDraw->SetFlags(flags);       
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0); // bottom-left corner
groundBody = world->CreateBody(&groundBodyDef);
b2EdgeShape groundBox;  
groundBox.Set(b2Vec2(0,s.height/PTM_RATIO/3), b2Vec2(s.width/PTM_RATIO,s.height/PTM_RATIO/3));
b2FixtureDef fixtureDef;
fixtureDef.filter.categoryBits = 0x0004;
fixtureDef.filter.maskBits = 0x0002;
fixtureDef.shape = &groundBox;
fixtureDef.density = 0;
fixtureDef.restitution = 0.0f;
groundBody->CreateFixture(&fixtureDef);
}

-(void)addBarrier{
b2BodyDef bodyDef;
bodyDef.type = b2_staticBody;
wallBody = world->CreateBody(&bodyDef);
for (int i = 0; i < 2;i++){
    b2PolygonShape platform;
    platform.SetAsBox(2, .25, b2Vec2(12+ (i * 5), 9 + i),0);
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &platform;
    wallBody->CreateFixture(&fixtureDef);
}
}

-(void) draw{
ccDrawLine(ccp(p1.x,p1.y), 
           ccp(p2.x,p2.y));
[super draw];
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
world->DrawDebugData(); 
kmGLPopMatrix();
}


-(void) update: (ccTime) dt{
int32 velocityIterations = 4;
int32 positionIterations = 1;
world->Step(dt, velocityIterations, positionIterations);    
rayAngle = rayAngle + (2 * (M_PI / 180));
float rayLength = 200;
p1 = b2Vec2( 240, 240 ); //center of scene
p2 = p1 + rayLength * b2Vec2( sinf(rayAngle), cosf(rayAngle) );

RaysCastCallback callback;  //FIRST RAYCAST METHOD
world->RayCast(&callback, p1, p2);
if (callback.m_fixture){
    CCLOG(@"OH YESAAH!");
    p2 = b2Vec2(callback.m_point.x * PTM_RATIO, callback.m_point.y * PTM_RATIO);   
}

OtherCast allback; //SECONDRAYCASTMETHOD
world->RayCast(&allback, p1, p2);
if (allback.m_hit){
    CCLOG(@"OH YESAAH! AD");
    //   p2 = b2Vec2(callback.m_point.x * PTM_RATIO, callback.m_point.y * PTM_RATIO);   
}

b2RayCastInput input; //THIRDRAYCASTMETHOD
input.p1 = p1;
input.p2 = p2;
input.maxFraction = 1;
float closestFraction = 1; 
b2Vec2 intersectionNormal(0,0);
for (b2Body* b = world->GetBodyList(); b; b = b->GetNext()) {
    for (b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext()) {
        b2RayCastOutput output;
        if ( ! f->RayCast( &output, input, 0 ) )
            continue;
        if ( output.fraction < closestFraction ) {
            CCLOG(@"woo");
            closestFraction = output.fraction;
            intersectionNormal = output.normal;
        }            
    }
}
}
@end

RaysCastCallback.h

#ifndef Shoot_N_Run_RaysCastCallback_h
#define Shoot_N_Run_RaysCastCallback_h
#endif
#import "Box2D.h"

class RaysCastCallback : public b2RayCastCallback{
public:
RaysCastCallback() : m_fixture(NULL) {
}

float32 ReportFixture(b2Fixture* fixture, const b2Vec2& point, const b2Vec2& normal, float32 fraction) {        
    m_fixture = fixture;        
    m_point = point;        
    m_normal = normal;        
    m_fraction = fraction;        
    return fraction;     
}    

b2Fixture* m_fixture;    
b2Vec2 m_point;    
b2Vec2 m_normal;    
float32 m_fraction;
};

OtherCast.h

#import "Box2D.h"
class OtherCast : public b2RayCastCallback{
public:
OtherCast()
{
    m_hit = false;
}
float32 ReportFixture(  b2Fixture* fixture, const b2Vec2& point,
                      const b2Vec2& normal, float32 fraction)
{
    b2Body* body = fixture->GetBody();
    void* userData = body->GetUserData();
    if (userData)
    {
        int32 index = *(int32*)userData;
        if (index == 0)
        {
            // filter
            return -1.0f;
        }
    }

    m_hit = true;
    m_point = point;
    m_normal = normal;
    return fraction;
}

bool m_hit;
b2Vec2 m_point;
b2Vec2 m_normal;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:41:22+00:00Added an answer on June 15, 2026 at 11:41 pm

    Check if ReportFixture methods are called. If they are, then the ray simply didn’t hit anything. This might be due to scale of values, perhaps the ray is too short or perhaps the objects are too far away (PTM_RATIO not applied?). But also the shapes of the bodies could be too tiny, or perhaps they don’t even have a shape. Perhaps the ray is aiming in the opposite direction or at a 90° angle, it’s easy to forget or incorrectly convert degrees to radians and vice versa. None of these issues are uncommon.

    It helps to set up a test case where you manually put two objects at known positions, then cast a ray so that the ray is guaranteed to intersect. Take out pen & paper and trace the positions and the ray, see if the values of variables during a debug run match. This should help to locate any issues due to incorrect vectors, angles and what not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
I've tracked down a weird MySQL problem to the two different ways I was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.