how to get touch event on a body in Box2d(Java) - java

I have various bodies rotated at some angle with the help of Box2d in libGDX. What I want is to destroy the body when I click on it, but the problem is that I am not able to get the area definition of the body so, that I can check whether my touch point lies inside the body or not. I tried using actor and its hit() method but its working only if I don`t rotate it, As far as I know , once I rotated the body , its bounds are not rotated . So, how can we check Touch event in a Body.
Thanks in advance.

See the touchDown handler in the libgdx Box2DTest. They use World.QueryAABB (AABB is "Axis-Aligned Bounding Box") to query which objects intersect a small bounding box around the the touch point, and then use the query callback to verify the actual touch point intersects the object in question.

Related

How to change the point of rotation of bodies in BOX2d/LIBGDX?

I have got a player set up and I am trying to make an arm for my character. The shapes are all created using either PolygonShapes or CircleShapes and I have made it so it points towards the mouse but I want to change the the point of rotation from this:
to this:
(Please forgive my crude drawings)
Ok, so i used a revolute joint which allowed to me set an anchor elsewhere, however, how do i rotate it towards the mouse position as the applyTorque method uses torque instead of radians?

Getting location where a Box2d Body has been touched?

I'm making a libgdx game where the user can create a distance joint and a revolute joint on its own, so what I do whenever 2 bodies have been touched they are both added to an arrayList, then when a button is touched a joint will be created. The problem is that the joint are always at the center, so I was wondering if there is a way to get the location of the body where it was touched?, then set those locations as anchorPointA and anchorPointB.
The first idea I get is a Gesture listener, see for example the GestureDetector.GestureAdapter(). Then you implement the touchDown method where you can get the x, y touch positions. To see wheter a body is touched, you might use the Vector.dst() method, but don't forget to unproject if you need.
Another idea might be to add an InputListener to your actor (which is connected to your body), but I haven't tried it yet.

Simulating a click on GearVR

I am trying to simulate a click on GearVR inside a webview.
Since the content in webview can be any website, it is not under a controlled environment, I need the user to click on the headset, get the X and Y position in the webview and simulate a click.
I've tried the MotionEvent.obtain, but it needs the x and y positions first to simulate a click, and I don't have, since it is not a real touch.
Any ideas?
Any help would be great.
I've been looking around to see if I can get absolute coordinates from MotionEvent but i don't think that's possible [1].
However, you can get the X and Y position of what the user is currently looking at by performing a ray trace (depending on how you've implemented your application), and then correlate the hit point as a result of that trace to your web view to infer a "click". Hope this helps!
https://github.com/Samsung/GearVRf/issues/231
Yes to add to #DarkTemplar's response, cast a ray from your virtual position in the direction of your looking direction. Your webview is probably mapped to a quad in space I assume. Then you perform a ray-plane intersection, then check if the intersection point is in the quad. If it is, figure out the relative coordinate of the intersection within the quad and then simulate the click there.
You are encouraged to check out
https://github.com/samsung/gearvrf/wiki
more developers can help you there

Box2D (LibGdx) getWorldPoint is wrong after collission

I've just started with Box2D and have come across a strange problem.
I have a simple function to constrain object position to within a predefined area.
I do this by getting the body's world position, checking this against the predefined area's bounding box values, and applying a force to the body to keep it within.
if (bodyWorldPos.x >= worldWidth)
body.setLinearVelocity(...);
This works fine.
However, if the body collides with another body, this simple method stops working.
The body's world position, retrieved like this:
body.getWorldPoint(body.getPosition())
returns wrong values.
Is this a bug in Box2D for LibGDX or am I doing something wrong?
The function getWorldPoint converts a point from 'local coordinates' (relative to the body's 0,0 position) to global coordinates (relative to 0,0 in the world).
I think for this purpose you can just use getPosition() only.

rotate/spin a body when collide with another

I'm implementing a physics game powered by AndEngine with box2d.
suppose there is an object falling from above vertically.
the ball collide with another object, and change its direction
now, after the collision, the ball should spin/rotate in the air, right?
so, I wanted to know if I need to do the calculation by myself (and how?) using setAngularVelocity function
or box2d can do it automatically.
I hope I expressed myself correctly
thanks for the help
sock.socket
No... you dont need to do any calculations... when you create your physics connector for your body like this..
public PhysicsConnector(final IShape pShape, final Body pBody, final boolean pUdatePosition, final boolean pUpdateRotation) {
if you set the pUpdateRotation true... you can see your body rotating.. and if dont want to see..put it to false... but this will only disable the updating of the sprite on the screen. It still keeps the Body in the Physics.. so the body will still be rotating.. but the rotation is not visible...
For anyone else reading this, another reason your sprite might not be rotating is if the friction of the fixture/fixtures is set to 0. (think sliding on ice).

Categories