-1

I am rotating a 3d model with this code:

_mesh = [[NGLMesh alloc] initWithFile:@"01.obj" settings:settings delegate:self];

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    [super touchesMoved:touches withEvent:event];

    UITouch *touch;
    CGPoint pointA,pointB;

    if ([touches count]== 1) {

        touch = [[touches allObjects]objectAtIndex:0];
        pointA = [touch locationInView:self.view];
        pointB = [touch previousLocationInView:self.view];
        //      _mesh.rotateY -= (pointA.x - pointB.x) * 0.5f;
        //      _mesh.rotateX -= (pointA.y - pointB.y) * 0.5f;

        _mesh.rotateY += (pointA.x - pointB.x) * 0.5f;
    _mesh.rotateX += (pointA.y - pointB.y) * 0.5f;
    }
}

The code rotates like it's supposed to, but it rotates no matter where in the view I tap. I'd like it to only rotate while touching inside the imported model. How would I do this?

sarahhodne
  • 9,796
  • 3
  • 39
  • 44
Sandeep
  • 221
  • 1
  • 9

2 Answers2

1

For this,You have to manually get the 3d model object frame (corresponding to the screen) and comparer with the current touch location using CGRectContainsPoint in touchesMoved delegate.

Write the code _mesh.rotate only if the result CGRectContainsPoint returns YES.

Note: NinevehGL have their own forum to discuss thing related to the framework. Please post the queries there to get good and quick responses.

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
1

Following code might help you to some extent,

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{  
[super touchesMoved:touches withEvent:event];

UITouch *touch;
CGPoint pointA,pointB;
if ([touches count]== 1)
{
    touch = [[touches allObjects]objectAtIndex:0];
    pointA = [touch locationInView:self.view];
    pointB = [touch previousLocationInView:self.view];

    NGLTouching touchingStruct = [_camera touchingUnderPoint: pointA];

    if (touchingStruct.mesh)   
    {
        _mesh.rotateY += (pointA.x - pointB.x) * 0.5f;
        _mesh.rotateX += (pointA.y - pointB.y) * 0.5f;
    }
}

But still it wont change the bounding box of the mesh. You can refer this blog post.

Prasad Devadiga
  • 2,573
  • 20
  • 43