Raycast understanding [c++]

Discuss issues specific to miscellaneous ports of Box2D
chelneru
Posts: 5
Joined: Fri Nov 08, 2013 11:33 am

Raycast understanding [c++]

Postby chelneru » Thu Jan 02, 2014 2:44 am

Sorry for double topic (not much activity here, just let me know if I should rather continue my previous topic than posting a new one )

So the only one tutorial that I found about ray casting was the iforce2d's tutorial. I am having difficulties to undestand the tutorial.

Can you show me an example of code of a ray cast ? (preferably with helpful commentaries)

Now for my pool game , can I identify which body intersect with the ray cast ?

I succeded with time control to predict the positions of the balls (thanks irresistible force for the idea) and this is the last problem.

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Raycast understanding [c++]

Postby irresistible force » Thu Jan 02, 2014 11:21 am

There is an example in the Box2D testbed, and the iforce2d tutorial has code snippets to explain each point with helpful commentaries (imho), and there is a full download for all topics at this link: http://www.iforce2d.net/b2dtut/sourcecode
If you could be more specific about what part you're having difficulty understanding, it would be easier to help.

chelneru
Posts: 5
Joined: Fri Nov 08, 2013 11:33 am

Re: Raycast understanding [c++]

Postby chelneru » Fri Jan 03, 2014 12:18 am

Well for start here are my 2 problems :

I added in my code :

this, global :

Code: Select all


float currentRayAngle = 0;
   
  float rayLength = 25;
  b2Vec2 p1( 0, 20 );
  b2Vec2 p2 = p1 + rayLength * b2Vec2( sinf(currentRayAngle), cosf(currentRayAngle) );

(First I only want a horizontal line that detect bodies. so no clockwise movement)


Now the part to add in the Step() function I added right before "world.Step(timeStep, velocityIterations, positionIterations); ", it's ok to add there ?

I added this :

Code: Select all

      //set up input
      b2RayCastInput input;
      input.p1 = p1;
      input.p2 = p2;
      input.maxFraction = 1;
 
      //check every fixture of every body to find closest
      float closestFraction = 1; //start with end of line as p2
      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 ) )
                  continue;
              if ( output.fraction < closestFraction ) {
                  closestFraction = output.fraction;
                  intersectionNormal = output.normal;
              }           
          }
      }
 
      b2Vec2 intersectionPoint = p1 + closestFraction * (p2 - p1);
 
 


My problems :
1. Variable m_world is a b2world variable ? Because when I replace m_world with world (my variable ) I get 3 errors in the for loop :

Code: Select all

c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\source2.cc(272): error C2819: type 'b2World' does not have an overloaded member 'operator ->'
          c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\testb2\box2d\dynamics\b2world.h(42) : see declaration of 'b2World'
          did you intend to use '.' instead?
c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\source2.cc(272): error C2232: '->b2World::GetBodyList' : left operand has 'class' type, use '.'
c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\source2.cc(272): error C2512: 'b2Body' : no appropriate default constructor available
c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\source2.cc(272): error C2451: conditional expression of type 'b2Body' is illegal
          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\source2.cc(272): error C2819: type 'b2Body' does not have an overloaded member 'operator ->'
          c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\testb2\box2d\dynamics\b2body.h(127) : see declaration of 'b2Body'
          did you intend to use '.' instead?
c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\source2.cc(272): error C2232: '->b2Body::GetNext' : left operand has 'class' type, use '.'
c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\source2.cc(273): error C2819: type 'b2Body' does not have an overloaded member 'operator ->'
          c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\testb2\box2d\dynamics\b2body.h(127) : see declaration of 'b2Body'
          did you intend to use '.' instead?
c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\source2.cc(273): error C2232: '->b2Body::GetFixtureList' : left operand has 'class' type, use '.'

2. The second problem is this :

Code: Select all

c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\source2.cc(276): error C2660: 'b2Fixture::RayCast' : function does not take 2 arguments


I use the last version before the 2.3.0 , could be this an issue ?

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Raycast understanding [c++]

Postby irresistible force » Fri Jan 03, 2014 2:38 am

Yes, you can do the raycast check before the world Step.

Regarding the compiler errors, they do not seem to be related to the code you pasted, and the explanation is right there for you to see:

Code: Select all

type 'b2World' does not have an overloaded member 'operator ->'
did you intend to use '.' instead?

left operand has 'class' type, use '.'

'b2Fixture::RayCast' : function does not take 2 arguments

The errors saying about using '.' instead of '->' are most likely because you have declared things like 'b2Body' instead of 'b2Body*' to make them a pointer. This is basic C++ understanding not really to do with Box2D.

For the last one, take a look at the header file (b2Fixture.h) to see what parameters you should be passing. Keep in mind that some tutorials (including the one we're talking about here) were written for older versions of Box2D than 2.3.0. In this case, you'll find that there is a third parameter 'childIndex'. This can be zero for all shape types except b2ChainShape where it should be the index of the segment you want to test against.

chelneru
Posts: 5
Joined: Fri Nov 08, 2013 11:33 am

Re: Raycast understanding [c++]

Postby chelneru » Fri Jan 03, 2014 4:02 am

Thanks ! Yeah...pointers are my weakness.. :?

I got rid of errors , I declared the world with b2world* type but I get error with the gravity :

Code: Select all

c:\documents and settings\alinx\my documents\visual studio 2010\projects\testb2\source2.cc(36): error C2440: 'initializing' : cannot convert from 'b2Vec2' to 'b2World *'


This problem is because I changed the b2world to b2world* . In tutorials you have the same but no errors..

Do you know where the problem might be ? :|

Also do you know how to identify the body ? I mean to know wich object collide with the ray cast ?

Thanks again for all the help

irresistible force
Posts: 1991
Joined: Tue Jun 24, 2008 8:25 pm
Location: Tokyo
Contact:

Re: Raycast understanding [c++]

Postby irresistible force » Fri Jan 03, 2014 11:24 am

I guess you are just trying to assign the wrong type of variable... again not really a Box2D issue.

The raycast method used in the code you pasted says "//check every fixture of every body to find closest"... so you already have the fixture, so you already know the body. As the tutorial states at the beginning, "we can ... check if the line crosses a fixture".
Perhaps you want to fire a ray out into the world and find out what it hits, without having any specific fixture to check. In that case this is not the method you should be using. Try this topic instead: http://www.iforce2d.net/b2dtut/world-querying
Also, please look at the 'Raycast' example in the Box2D testbed.


Return to “Miscellaneous”



Who is online

Users browsing this forum: No registered users and 1 guest