Author Topic: Box 2D help - boundaries of a shape  (Read 3008 times)

Djjammin

  • Guest
Box 2D help - boundaries of a shape
« on: March 13, 2013, 01:42:46 pm »
Hello All,

need some help with box2d, i'm not the greatest programmer... visual basic background but i've got a good grasp on C++ now. One thing i can't get my head round which there is probably quite a simple solution for is; i want my app to recognise when you've touched a shape in a Box2D environment. So obviously you have to get the X&Y of where the user has touched, but how would i compare that with the X&Y of the WHOLE shape? Not just the 1 pixel in the middle of the shape, but everything within the boundaries.

not only that, it's a circle... so it doesnt seem as easy as if it were a mathematically simple shape like a square or triangle...

Regards,
Dj Jammin

glina126

  • Guest
Re: Box 2D help - boundaries of a shape
« Reply #1 on: March 13, 2013, 09:04:00 pm »
There are a few ways around... Hard, medium and easy. All will have drawbacks...

The "hard" method (if you dont know Box2d too well) would be creating an object in b2d where the person has touched once the screen has been touched. Then setting this object to be a sensor and filter through all the objects, or the desired objects to see if the one your looking for is within the sensor field of "view" And once the person lets go of the screen delete that object...
- this will provide great control, you will be happy to see less "tunneling" and etc..

The medium method would be to create an array of small squares. In other words divide the screen into lets say 48x32 square segments. Then you will end up with 1536 "pixels" to play with and doing so you can assign the "pixels" where the image is to be lets say true and when a touch is registered you can run a check to see if any of them correspond.
I hope I'm making it clear... I have done this before and I have the code if you would like to see it..
- problems? just one that I know of.. Tunneling. (look it up)

The easy method which you seem to know is to create a square for where a circle is and thats it..
- drawbacks? you can list them hah

Anyway, if you want the code for the medium method then Ill dig through my old stuff and I will be more than happy to share it. Otherwise check out these tutorials on sensors in b2d http://www.iforce2d.net/b2dtut/sensors

Damian

Djjammin

  • Guest
Re: Box 2D help - boundaries of a shape
« Reply #2 on: March 14, 2013, 04:21:18 am »
I've just realized something... the image is loaded into a view right? So i could probably just use the "On touch" routine in a view... So simple... how could this not have crossed my mind before i dont know...

I see no reason why it shouldn't work?

glina126

  • Guest
Re: Box 2D help - boundaries of a shape
« Reply #3 on: March 14, 2013, 06:17:27 am »
I've just realized something... the image is loaded into a view right? So i could probably just use the "On touch" routine in a view... So simple... how could this not have crossed my mind before i dont know...

I see no reason why it shouldn't work?

Haha that is true, but then you are constraining yourself to a big square. I'm actually using that in one of my games that I'm about to release but I'm not found of the method for circles ;)
Remember that every image is saved as a square even though there can be transparency.. That transparency will be touchable!!

Djjammin

  • Guest
Re: Box 2D help - boundaries of a shape
« Reply #4 on: March 14, 2013, 09:09:18 am »
Ah yeah you do have a point... for this game though it should still be okay... and great for people with fat thumbs :P

glina126

  • Guest
Re: Box 2D help - boundaries of a shape
« Reply #5 on: March 14, 2013, 06:22:27 pm »
Ah yeah you do have a point... for this game though it should still be okay... and great for people with fat thumbs :P

hah yea same for my game. I'm doing this method but only because I'm not using b2d as there is no need for my app

Djjammin

  • Guest
Re: Box 2D help - boundaries of a shape
« Reply #6 on: March 18, 2013, 02:45:07 am »
Yeah, my way isn't working out very well... really hit and miss and i can't work out why

glina126

  • Guest
Re: Box 2D help - boundaries of a shape
« Reply #7 on: March 18, 2013, 07:18:06 am »
Yeah, my way isn't working out very well... really hit and miss and i can't work out why

if the images are moving then this is "normal" I guess its because of the fps difference at which the timer and touch are running.. I dont know though..

Djjammin

  • Guest
Re: Box 2D help - boundaries of a shape
« Reply #8 on: March 20, 2013, 07:12:35 am »
What you're saying does sound right... how many ms does the timer run at as default?

glina126

  • Guest
Re: Box 2D help - boundaries of a shape
« Reply #9 on: March 20, 2013, 01:42:55 pm »
What you're saying does sound right... how many ms does the timer run at as default?

well timer ticks 30 frames in a second.. The native fps is 60.. The difference is I guess why some touches will "miss" but im not sure to be honest.. For this matter, box2d would be much better because of its internal magic it should register the hit even if the timer "misfires"
again.. tunneling is the biggest problem..

Djjammin

  • Guest
Re: Box 2D help - boundaries of a shape
« Reply #10 on: March 27, 2013, 08:05:36 am »
I Cracked it now... i wasn't moving the Touch area with the actual shape in the timer subroutine i was doing something retarded... Thanks for your help though!