Point Query, Ray Cast and Rect Query

The link below linked to a Physics tutorial but the section at bottom leaves little or no help understand how point query or rect query work

Physics Tutorial Page

The main issue I have is this line
scene->getPhysicsWorld()->queryRect(func, Rect(0,0,200,200), nullptr);

I cannot make scene global or accessible to any other part of the game.

I did try initializing the scene variable in the header, instead of a auto in the create Scene function file but that doesn’t work

Is there any game examples of this working ?

How can my scene be made global or accessible to other functions such as update function ?

Thanks

You can get current running scene by,
Scene *curScene = Director::getInstance()->getRunningScene();

So this will get the scene I’m currently running and copy it into that declared scene so as i can use it for queries ?

Thanks

You dont have to store into global variable just use directly as i written wherever you required in game.

Have you ever used the queries ?

All that the Tutorial gives you (Shared above), is two parts

Part one
PhysicsQueryRectCallbackFunc func = [](PhysicsWorld& world, PhysicsShape& shape,
void* userData)->bool
{
//Return true from the callback to continue rect queries
return true;
}

Part Two
scene->getPhysicsWorld()->queryRect(func, Rect(0,0,200,200), nullptr);

How do I use these. I can’t figure it out. That part one fucntion I added to INIT , but then it cannot be called in Update Function.

I simple want to run a handful of checks in my update function each times its called, check if a object is inside a rectangle area and then act upon that? How might that be done with them two parts ? On WIndows!

Thanks Very Much Smit Patel

No.
Mostly i manage through onContactBegin & onContactSeparate

You can also take help of intersectRect method.
If you know both rect then
bool isCollide = rect1.intersectsRect(rect2);

Mostly i manage through onContactBegin & onContactSeparate

Does this need to be physic bodies . How can I compare a object to a rect or custom area?
As example, if my vehicle drives over a sprite can we check if it contacted it without using physics contact ?

Thanks :slight_smile:

I’d stop using the Wiki for things. It could be helpful, but it is mostly out of date.

Check this out: http://cocos2d-x.org/docs/programmers-guide/physics/index.html

Hi again. Them query tutorials at bottom simple do not a thing for me. I cannot find any query examples anywhere on Earth :frowning:

No idea what so ever. I know what that special nameless function is func. I can only imagine that going in init. Could you put it in update ? Can I query objects in places every update and act on it. What code would I put in Update maybe. Just Point or rect would be helpful

Im gonna try out smitpatel88 solutions shared above. Using intersectRect , see can i replicate these query functions

I just found this tutorial. Ill see if I can use this. Seems should be work?

Collision of Object Tutorial Link

What are you trying to do exactly?

OK Let’s me try and explain. We have a game built somewhat. Its top down racing game using physics. We have huge 6000 pixel X 3000 pixel sprite race track. We use Photoshop to find the pixel points and then create two physics body to match the two track sprites, a inner sprite and a outer sprite. We use edge physics body which takes in a array of points. So we have a track that a vehicle drives around and walls to bounce of and keep car in track

We want AI cars. We were thinking if we could ray cast the inner and outer edge and get them two points each update , that we could then with them two points which are left and right of the AI vehicle, get a velocity point straight ahead.

We simple(Impossible) need AI cars to drive around the track ?

We could use queries too, but no examples or idea how to use them. The tutorial doesn’t help at all really

Thanks For Reading

Use waypoints? I’ve never implemented AI for racing cars with physics but if you set waypoints on the track, store them in a vector, and have your AI cars move to each waypoint in the vector?

2 Likes

Need you again… I was wondering if you could give some more advice. So lets say our track is a square. That means we have 4 corners to turn… I will use way points. I was thinking to stick to physics functions. So game starts and AI vehicle, hover car is at start. His first point is ahead by the turn. I was thinking to do it like this as it may look better.

The vehicles velocity is always a point ahead of it facing direction, which would be its hypotheses as we use trigonometry to find it each update. I already have this code working with player vehicle. So seems the AI vehicle is always going the way it is facing, we simple need to face it to way points. I need to query that it has reached a way point though, or a rectangle surrounding the point. I was thinking having another Boolean array. Only one point is true when that’s the point we are trying to reach. Once reached, it becomes false and next point becomes true??

What you think
?

:slight_smile:

I cant make this work

bool isCollide = playerSprite.intersectsRect(rectNode);

Can we check a sprite to a rect ?

Not having any luck with queries or intersect :frowning:

Can you share you onContactSeparate fucntion please ?:slight_smile:

Is there a query example. I cannot find any examples in my cocos2d folder. Of any type or sort

Another failed example for me. Ill share
HEADER

	cocos2d::DrawNode* rectNode1;
	cocos2d::DrawNode* rectNode2;

INIT

rectNode1 = DrawNode::create();
Vec2 rectangle[4];
rectangle[0] = Vec2(-130 / TIscale, 50 / TIscale);
rectangle[1] = Vec2(130 / TIscale, 50 / TIscale);
rectangle[2] = Vec2(130 / TIscale, -50 / TIscale);
rectangle[3] = Vec2(-130 / TIscale, -50 / TIscale);

Color4F white(1, 1, 1, 0.1);
rectNode1->drawPolygon(rectangle, 4, white, 1, white);
rectNode1->setPosition(Vec2(-2475 / TIscale, 1175 / TIscale));
rectNode1->setRotation(45);
this->addChild(rectNode1);

rectNode2 = DrawNode::create();
Vec2 rectangle[4];
rectangle[0] = Vec2(-40 / TIscale, -15 / TIscale);
rectangle[1] = Vec2(40 / TIscale, -15 / TIscale);
rectangle[2] = Vec2(40 / TIscale, 15 / TIscale);
rectangle[3] = Vec2(-40 / TIscale, 15 / TIscale);

Color4F white(1, 1, 1, 0.1);
rectNode2->drawPolygon(rectangle, 4, white, 1, white);
this->addChild(rectNode2);

UPDATE FUNCTION

rectNode2->setPosition(playerSprite->getPosition().x, playerSprite->getPosition().y);
bool isCollide = rectNode2.intersectsRect(rectNode1);

Gives error at rectNode2 in update, says “expression must have a class type”

:frowning:

Really feels like that tutorial tells you nothing. I still no idea how to use rect query …

auto func = [](PhysicsWorld& world, PhysicsShape& shape, void* userData)->bool
{
    //Return true from the callback to continue rect queries
    return true;
}
`Scene *curScene = Director::getInstance()->getRunningScene();`
curScene ->getPhysicsWorld()->queryRect(func, Rect(0,0,200,200), nullptr);

Do these both have to go in init ?

What is been queried, is it querying the entire physics world and object to a certain rectangle, it does seem ?

Rect queries are a way to check if and what physic shapes are in a specific region (Hope that sentence made sence :smile:). Basically the queryRect function is doing the following:

1.) Iterate over all available physic shapes and check them against your bounding box
2.) If not intersecting, go back to 1
3.) If intersecting, the given function (lambda in your case) is executed
4.) If function returns true, go back to step 1
5.) If function returns false, exit iteration and return to user

Depends on what you are trying to achieve. If I understand you correctly, you want to check if a car in your game is near a waypoint. In this case the queryRect call belongs in the update routine. You then put some logic to check if the intersecting shape belongs to the car in question, into the lambda. If it does, the car is in range of the waypoint. Now you just have to return false and store the success into some variable.

Hope that helps clearing things up.

1 Like