0

I am developing a game using Cocos2d, the problem i am facing is while getting CGRect of the wheel which i have placed at anchor points(0.5,0.5) and position at win-size width/2-height/2.I am detecting touch on wheel and i am facing problem in that using CGRect.intersects(CGRect a,CGRect b).

I am trying this to get wheel CGRect positions

CGRect wheelrect=CGRect.make(wheel.getPosition().x-(wheel.getContentSize().width), 
                                     wheel.getPosition().y-(wheel.getContentSize().height), 
                                     wheel.getContentSize().getWidth(), 
                                     wheel.getContentSize().getHeight());

I am getting CGRect of left half of the wheel, But i need to detect the touch on whole wheel.

I am using Intersect method of CGRect to follow the intersect points.

Need some help.Thanks in advance. This is how it is happening

ManishSB
  • 767
  • 9
  • 27

2 Answers2

2

is there a boundingBox method or property? If so you could use that.

Anyway your issue is that you subtract contentSize from position, but you need to multiply contentSize with anchorPoint (ie half the contentSize) before subtracting it.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • I searched for boundingBox method, didn't find that method nor property. I subtracted the contextSize but only half the sphere is getting touch detected, another half i am not getting intersect point. – ManishSB Jun 19 '14 at 08:39
  • I edited the code CGRect wheelrect=CGRect.make(wheel.getPosition().x-(wheel.getContentSize().width/2), wheel.getPosition().y-(wheel.getContentSize().height/2), wheel.getContentSize().getWidth(), wheel.getContentSize().getHeight()); – ManishSB Jun 19 '14 at 08:43
  • Is there any other library which would help me get the Rect of any shape and find the intersect in android. – ManishSB Jun 21 '14 at 06:30
2

and how about these functions :)

wheel.getBoundingBox().contains(x, y) wheel.getBoundingBox().containsPoint(aRect, aPoint)

or

wheel.getBoundingBox().containsRect(aRect, bRect)

and I prefer to use

wheel.getBoundingBox().size.width

than

wheel.getContentSize().width

because when your CCSprite is scaled, you could get wrong size.

Edit:

CGRect wheelrect = CGRect.make(
            wheel.getPosition().x - (wheel.getBoundingBox().size.width / 2f), wheel.getPosition().y - (wheel.getBoundingBox().size.height / 2f), 
            wheel.getBoundingBox().size.width, wheel.getBoundingBox().size.height); 

if(CGRect.intersects(wheelrect, wheel.getBoundingBox())){
    System.out.println("intersect");
}

This works for me. Maybe if you put more code here, we will find the problem.

StevoF
  • 477
  • 5
  • 17
  • I have edited and added your try, But too not getting the intersect point of that wheel and CGRect point.The red portion only has intersect point. – ManishSB Jun 19 '14 at 10:04
  • Hey @Steve - I tried your code but i am not able to get the intersect point, I even tried this wheel.getTexture().getHeight()/2 and for width too, Somewhere I am missing that spark.I will try some more code. – ManishSB Jun 20 '14 at 07:45