How can I detect touch on Sprite3D?

I want to know …
How can I detect touch down on Sprite3D?

Sprites like almost everything in Cocos2d is a node. You can install a Listener on a node to listen to touch events. Check out http://www.cocos2d-x.org/wiki/How_To_Subclass_Sprite_And_Add_Event_Listeners for more information.

I know that… But I try it in Sprite3D…
In Sprite3D, that is not working…


auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [](Touch* touch, Event* event) {
    auto target = static_cast<Sprite3D *>(event->getCurrentTarget());
    Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
    Size size = target->getContentSize();
    log("size .. %f : %f", size.width, size.height);
    Rect rect = Rect(0, 0, size.width, size.height);
    if (rect.containsPoint(locationInNode))
    {
        log("touch");
        return true;
    }
    return false;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sprite3d2);

Have you tried adding the Listener to the scene, and from that listener then compare the touch event coordinates to the Sprite3D (similar to what you are doing right now)?

you know I haven’t tried touch events on a Sprite3D yet. I should really try that.

Are you guys using Blender to make your object files?

I made obj file by using “3ds Max exporter”.

The problem is that getContentSize() and getBoundingBox() function don’t work in Sprite3D.

I think there should be a getContentSize() and getBoundingBox() for Sprite 3D. I’m going to ping the developers about this to have them added. I’ll also ask for a way to calculate this while they are adding.

Anyone made progress on this? Currently, as far as I know, it is not possible to detect touch events on Sprite3D nodes.

The developers are aware and there has been internal discussion about this. I’m not current on the status of implementing this.

1 Like

v3.2 sprite3D’s contentsize is zero. you can set a size to it.
We will at v3.3 use ray to detect a 3d modle.

Ok, I see.
And I will wait for v3.3.

@hhyytt, I did set the content size. Note however that currently content size only accepts a 2D size instead of 3D. Another problem is that when transforming the bounding box to world space, the Cocos2d-x transformation functions return an incorrect result. When I looked at the transformation code I noticed that in the transformation matrices, the z-axis isn’t being handled at all.

Normally, 3D use AABB for a model.
@mtartist, you make 3D test or 2.5D test? if a sprite3D move in 2D scene, 2d bounding box can make a not very accurate pick/detect.

@hhyytt, I’m working in 3D. Unfortunately Sprite3D does not have a method to return an AABB.

This is all being worked out be the developers. I’ll see if I can get an update.

1 Like

Hey guys, any update here? I’m working with Sprite3D in beta 3.3 and getBoundingBox still returns 0 :frowning:

Hi, I’m also using 3.3beta0 and trying to tap on Sprite3D objects.
I see a Ray class and a Sprite3D::getAABB method, but I’m not sure how to cast the ray from the camera using the correct 3d position and direction… anyone has any ideas?

I can try to get to this. With the developer conference on Tuesday I’m trying to finish the first version of the Programmer Guide.

That would be great.
Too bad I really needed this feature for a game jam this week. I’m sure there’s some kind of mathemagick to combine the Touch, the Camera and Ray to achieve this, but I won’t have the time to figure it out. Surely this will be resolved for the actual 3.3 release.
Guess I’ll switch to Unity for a little while =(

EDIT: I’ve just found the ‘unproject’ method in CCCamera.h but I’m not sure how to make it work or if it’s useful at all… anyone has any ideas?

Hi, what is the status on this?

I have been spending all day almost, just to be able to select my sprite3D… I am currently using v3.14.1, which is much newer than this post, yet it doesn’t work!
Is this still being worked on?

I don’t understand that the only info I find seems to say that it should work, but I can’t get a boundingbox nor an AABB, nor a raycast intersect or anything. Tried all the examples I could find, inluding cpptest, but nothing works.

Any solution?

ex: if I ask for the boundingbox of my sprite3D it always returns a width and height of -199998