It is possible to detect when a body is under light? ( Collision with the light and a body )
How I check this with libgdx?
Sorry for my english...
It could be easy to implements with raycasting :
With a point light, you have to perform a raycast from your body object to the light object, if the first returned body is the light then your object is under light.
With a directional (infinite) light you have to raycast from your object body toward light direction opposite, if raycast not returning bodies then your object is under light.
With Cone lights, it's the same as point lights but you also need to check angle with that light.
Notes :
If your object body is big, you may perform multiple raycasts (from different positions).
If you have multiple lights, you need to perform multiple raycasts as well.
Related
I'm making a 2D (side view) car game with LibGDX using Box2D. The car has a front wheel and a rear wheel and it's "AWD". I would like to render a dust effect to each point where a wheel touches the ground when the player is "burning rubber". To do this, my plan was to
Implement ContactListener, and in beginContact(Contact contact) method find the Contacts where one of the fixtures is ground and the other one a wheel of the car.
Save these Contacts to a (wheel-specific) array, which is supposed to contain the active Contacts of the wheel (the wheel can touch the ground in multiple points, hence an array is required).
During each render call, for each Contact in each wheel's array: Get the contact position, and calculate velocity difference between the wheel body and the ground body at that position. If the length of the velocity difference vector is greater than a certain threshold, then draw the dust effect (using the velocity difference vector to define the speed and angle of the dust particles).
When endContact(Contact contact) of my ContactListener is called, remove contact from the array.
The problem is that LibGDX appears to use the SAME Contact instance for every Box2D contact, rewriting the existing Contact even if it's still active! This means, for example, that if the rear wheel touches the ground first and the front wheel after that, the Contact instance I saved to the rear wheel's contact array gets overwritten with front wheel's contact properties. Another problem is that when I receive endContact, I don't know which Contact really ended.
Have I understood something wrong, or is this how LibGDX is supposed to behave? Is it possible to somehow adjust this behavior, or is there an easy workaround?
(One option that comes to my mind is to keep listening to beginContact and endContact calls and just keep track of the ground bodies/fixtures that are touching each wheel, but then I would have to calculate the contact points manually each frame, which sounds somewhat difficult, expensive, and redundant - because Box2D anyway calculates it at each world step).
Maybe you can create a MyContactListener implementing the ContactListener class.
The MyContactListener class contains a MyContactInventory object which is a Singleton object containing your Contact Array and a couple of convenient method:
addContact
removeContact
The beginContact and endContact add/remove Contacts to your Array using the convenient methods above.
Then you call world.setContactListener( new MyContactListener());
Hope it helps!
I need to add a translucent guide layer above my UI, details are as follows:
there is one (or more) area (which I suggest the user to click) of the layer, and it's totally transparent and transmit touches to the UI below.
At the same time, the rest part of the layer, which is translucent, swallows touches.
I use a LayerColor clipped by ClippingNode to implement the guide layer, and I have a EventListenerTouchOneByOne (setSwallowTouches(true)) to detect touch, and then, in
bool touchBegan(cocos2d::Touch *touch, cocos2d::Event *event) {
// return (whether touch point belongs to translucent area).
}
So is there any way to judge whether a point belongs to the clipped area? Thanks.
p.s. Since the shape of clipped area is irregular, the way by checking whether the boundingBox of the stencil contains the touch point may not be acceptable.
p.p.s I've tried the way of judging pixel value of my LayerColor following methods such as Getting RGBA value of a pixel in a CCSprite, but failed to get values, someone says that the methods applies no more for cocos2d-x 3.x? Besides, I wonder will the pixel value of the LayerColor really change after being clipped?
Thanks again :D
I have two bodies. One circle with a ball inside and one bird with a polygon. I am trying to detect collision between the sprites within the bodies and not the bodies themselves as in the code snippet below.
#Override
public void beginContact(Contact contact) {
Body a = contact.getFixtureA().getBody();
Body b = contact.getFixtureB().getBody();
if(contact.isTouching()){
System.out.println(contact.isTouching());
if (a.getUserData() == Constants.Enemy || b.getUserData() == Constants.Enemy) {
System.out.println("yes");
}
}
}
the method above prints out "yes" when the bodies are in a stage as on the picture below which is not right because the sprites have not touched with each other. Any ideas?
there is probably another way of doing, but if I wanted more accurately, would use this tool -> http://www.aurelienribon.com/blog/projects/physics-body-editor/
If you experience any initial error you can look at these questions might be errors with charger ->
Physics Body Editor error
or this BodyEditorLoader - noSuchMethod in this response, public use the charger that works well for me in libgdx (1.5.x)
I hope to help
Update:
you said: "thanks for this but I am not sure whether this will help me in my case. "
Box2d initially assuming that knows nothing of your sprite, position or anything. He "box2d" just knows fixtures ect. If your sprite does not match the size of the fixture, does not know, is not malfunctioning, but you expect something different.
So using the tool, I said, you can adjust the fixture friendlier way to the shape of sprite.
This is an image simulating as could be, the fixture is an image in Gimp, it's just to see the idea:
Like Angel Angel said,
Box2D has bounding volumes to detect collisions, which are not pixel perfect and do not know anything about the Sprite itself. This is for a reason of performance, since collision detecting has a huge impact on performance.
The solution is to make the bounding box more accurate. You can use PolygonsShapes or make your bounding rect smaller.
In your case i would consider using a PolygonShape.
I've been working a lot with AGAL vertex and fragment shaders. I've got individual objects lit correctly (including specular shading) but I'd like to have objects cast shadows on OTHER objects. I have looked online, but I think most people working directly with AGAL have built custom Stage3D libraries and the shadow-casting solution doesn't seem to be in the public domain. Anyone willing to change that?
I'd like to know how to get an object to cast a shadow on another. I can't post what I've tried, because I can't get my head around where to begin on this problem. How would you pass the information (whether other objects are blocking the light) into another object's shader?
Thanks.
IT's called Deferred shading, you have to do 2 pass of vertex and fragment shaders.
In the first pass you accumulate informations about distances, normals, occlusion...
In the second pass you render and apply the informations of the first pass to make shadows.
Another options is ShadowMapping:
Basic shadowmap
The basic shadowmap algorithm consists in two passes. First, the scene is rendered from the point of view of the light. Only the depth of each fragment is computed. Next, the scene is rendered as usual, but with an extra test to see it the current fragment is in the shadow.
The “being in the shadow” test is actually quite simple. If the current sample is further from the light than the shadowmap at the same point, this means that the scene contains an object that is closer to the light. In other words, the current fragment is in the shadow.
I'm absolutely new to Pixel Blender (started a couple of hours ago).
My client wants a classic folding effect for his app, I've shown him some example of folding effect via masks and he didn't like them, so I decide to dive in Pixel Blender to try to write him a custom shader.
Thx God I've found this one and I'm modyfing it by playing with values. But how can I trace / print / echo values from Pixel Blender ToolKit? This would speed up a lot all the tests I'm doing.
Here i've found in the comments that it's not possible, is it true?
Thx a lot
Well, you cannot directly trace values in Pixel Bender, but you can, for example, make a certain bitmap, then apply that filter with requested values and trace reultant bitmap's pixel values to find out what point corresponded the one selected.
For example, you make a 256x256 bitmap, with each point (x,y) having "x" red and "y" green component value. Then you apply that filter with selected values, and either display the result, or respond to clicks and trace underlying color's red and green values, which will give you the exact point on the source bitmap.