Connecting physics bodies with PhysicsJoint - cocos2d-x

Lately I realized that Chipmunk (am I right?) was integrated with >= Cocos2d-x 3.0. Now I am trying make use of it (I am using cocos2d-x 3.2).
So can you tell if it's possible to connect the bodies (PhysicsBody) with PhysicsJoint (eg. PhysicsJointSpring), but with connection points being placed for example on the edge of the body?
I saw the "Joints tests" from the package with library, but it looks like all of them (joints) are placed in the midlle of the body - you could say that anchor point is placed at Vec2(0.5f, 0.5f).
Test are here (PhysicsDemoJoints class):
https://github.com/cocos2d/cocos2d-x/blob/v3/tests/cpp-tests/Classes/PhysicsTest/PhysicsTest.cpp
Here is an example what I am talking about (two balls, left bottom corner):
https://www.youtube.com/watch?v=ZgJJZTS0aMM
I would appreciate any help! And give you an internet hight five as a gift!

In such cases take a look at the class reference.
You'll find that joints can be created with constructors similar to this one:
static PhysicsJointDistance * construct (PhysicsBody *a, PhysicsBody *b, const Vec2 &anchr1, const Vec2 &anchr2)
The two vectors are the joint anchor points. Unfortunately it's not documented what they are, but typically they'll be in world (scene) coordinates.

Related

How to animate textures in a 3d model?

I wish to have a animated 3d texture in my LibGDX code but I am struggling to find out how to do it.
I assume how this "should" be done is either;
a) Directly accessing and modifying the texture on the model. (via a pixmap? ByteBuffer?)
or
b) Prerendering a big image containing all the frames (say, 20) and then moving the UV co-ordinates to create the illusion of the animation. (akin to ImageStrips in 2d/webdesign).
I did work out how I could completely replace the material eachtime, but that seems a much worse way of doing it. So if anyone could show the commands I need to do either a) or b) (or a similar optimal method) I would be great-fall.
Maths I am fine with. The intricacies of OpenGLES or GDX I am not :)
(The solution should at least work HTML/Android compiles, ideally everything)
Since the latest release it is very easy to play a 2d animation on a 3d surface. First make sure to get familiar with the 2d animation concept, as explained over here: https://github.com/libgdx/libgdx/wiki/2D-Animation. Then, instead of using a spritebatch, you can use the TextureRegion (which Animation#getKeyFrame returns) to set the material of the surface, as shown here: https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/g3d/TextureRegion3DTest.java. So basically you would get in your render method:
attribute.set(animation.getKeyFrame(stateTime, true));
Or if you want a more generic approach:
instance.getMaterial("<name of material>").get(TextureAttribute.class, TextureAttribute.Diffuse).set(animation.getKeyFrame(stateTime, true));
Or, if there's only one material in the ModelInstance:
instance.materials.get(0).get(TextureAttribute.class, TextureAttribute.Diffuse).set(animation.getKeyFrame(stateTime, true));
If you have the memory for it I would definetly choose b), it is easier on the processor. Also, you would only change a uniform's value. However, due to preprocessing it might take some time to open the application.
Get you uniform variable, where you compile your shaders, animationPos should be global.
Gluint animationPos = glGetUniformLocation(shaderProgram, "nameoftheuniform");
Your main loop should pass animationPos value to the shader:
Gluniform1i ( animationPos, curentAnimationIndex);
Add this your fragment shader variables:
uniform int animationPos;
Fragment shader main:
float texCoordY = texCoord.y; //texture coordinates should be passed from vertex shader
float texCoordX = texCoord.x/20.0f; //we are dividing it with 20 since it is the amount of textures that we have and if we use it directly it would try to use all the texture. Whereas the texture stores at 20 different textures.
float textureIndex = 1.0f*animationPos/20.0f; //Pointer to the start of the animation texture.
gl_fragColor = texture2D ( yourTexture, vec2( textureIndex + texCoordX, texCoordY));
Above code assumes that you expanded your textures in the x direction, you can also try to expand it like a matrix, then you need to change the texCoord calculation part. Also that we are using 20 textures.
The option a) is more heavy on the processor and you will be changing the texture every time so it will use pci a bit more, but easier on memory. The question is more like a design decision, but I guess 20 images can be handled so go with option b).
Edit: Added code.

Libgdx bullet offset origin

i am using blender to create my models, and loading them into Libgdx, if i create them with the Origin in the center of the model like below and then use this code to create the rigid body, all works fine
Vector3 hescoWallHalfExtents = new Vector3(hescoWall.calculateBoundingBox(bounds).getDimensions()).scl(0.5f);
however if i place the bottom of the model level with the ground like this
then the btRigidbody is offset like this
is there an obvious way that i can offset the height of the rigidbody?
many thanks.
Spriggsy
The center (origin) of the rigid body is the same as the center of mass and therefor an important property for the physics simulation. You could "move" this center using a btCompoundShape if you like, but this will also influence the physics simulation and therefore probably won't give you satisfying results.
Alternatively, you could compensate for the difference of physics origin and visual origin in your btMotionState. For example by setting ModelInstance#transform to the provided worldTransform multiplied by the a Matrix4 instance which contains the offset (use Matrix4#translate for example).
However, this is probably just making it more complex than it needs to be. You could say that the real question is why you want offset the center of model compared to the body? For example, in you second image the center of the model appears to be same as the in the first image. You only moved the Node, basically indicating that you want to provide an initial value for the ModelInstance#transform member. You can achieve this by instantiating the ModelInstance as follows:
modelInstance = new ModelInstance(model, "coneNode", true);
Replace "coneNode" with the name of the node as created it in your modeling application. The last (true) argument tells the ModelInstance to set its transform member to the transformation you've given it in the modeling application. If needed, you can call modelInstance.transform.translate(x, y, z); or modelInstance.transform.trn(x, y, z); to move the modelInstance relative to this transformation.
A more in-depth explanation of this can be found here: http://blog.xoppa.com/loading-a-scene-with-libgdx/
Note that this only works if you're using .g3db or .g3dj files (e.g. using fbx-conv) created from a file format that supports node transformations (e.g. .fbx, but not .obj)

libGDX: same texture with shaders, different textures without

my name ist Tom (Ger) and i am developing a small 3D game with libGDX.
when i am using a Model, ModelInstance with a ModelBatch and the Environment, i can render different ModelInstances (with different Models) with there right textures.
But i need to use a shader for some wobble effects.
But when i use a shader everything works finde, except for the textures. there are the same for every ModelInscance i want to render.
i guess there is a texture binding problem. I load my Models this way:
assets = new AssetManager();
assets.load("blob.g3db", Model.class);
and fetch them with a simple:
public static Model getModel(String name) {
return assets.get(name + ".g3db", Model.class);
}
So i guess the assetsManager is loading the textures as well (cause it works without the shader).
My Question is:
How can i render differend 3D Objects with a Shader with there correct Textures?
Thanks in Advance...
Tom
The Models and the ModelInstances have a Material, where you can set a Texture, Color and other things to it.
So if 2 ModelInstances share the same Model you can set different Materials to their ModelInstances. By doing this you have different Textures. The DefaultShader implementation takes care about them. If you create your own Shader you need to take care about them.
Important: It does not work without Shader, cause you always render with Shader. You don't set the Shader manually, but libgdx uses DefaultShader by default.
I suggest you read some of Xoppas tutorials.

Shadow-casting shaders in Stage3D

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.

Flash - SideScroller Turret Math

I'm working on a side scroller, and for the enemy I'm making a turret. I'm trying to make the turret aim at the player but I cant seem to get it right. Below is a rough sketch of what I want to achieve:
I want the barrel (dark blue), to aim/rotate to its pointing at the player.
I have uploaded a YouTube video of my scene:
http://www.youtube.com/watch?v=eeP47VoX9uA&feature=youtu.be
This is what I have so far (loop):
function enterFrameHandler(e : Event) : void{
_turretBarrel.rotation = Math.atan2(enTarget.x, enTarget.y) * 180/Math.PI;
}
What this does is only rotate the barrel when I jump, and the barrel isn't even aiming at the player, also the barrel doesn't change rotation when I walk on the other side of the turret.
My enTarget.x is always central to the stage and the scene (including the turret) moves around the player left and right (x)... Only the enTarget.y moves (jump/high platform).
I'm slightly new to Flash and ActionScript. If anyone could help me out, or point me in the right direction then that would be great.
Thanks
1) Make sure you got the right numbers and the position of the avatar and the turret are in the same coordinate space. A simple trace of each would do. In this case you probably want the world (relative to stage) position of both clips. Make sure they make sense compared to top left corner of the screen (0, 0).
2) Remember that _turretBarrel.rotation is a rotation that ranges from -180 to 180 so this would need to be taken into consideration when calculating angles.
3) Make sure you use the corresponding degrees/radians where appropriate.
4) force focus on avatar, run the game and see if the bounds looks ok. Then do the same thing with the turret.
Another good thing in general for debugging purposes is to setup some kind of debug graphics. i.e. draw a line of what you think is the direction vector to verify your numbers and calculations.
On a side note:
This is what the majority of programming is; Debugging. Assume nothing but hard facts, get your numbers from the debugger (probably quicker), or trace output. If you're still using the horrible flash professional IDE. I would really recommend getting one with a proper debugger like FlashDevelop (free) or Flash Builder (commercial)
Oliver, it looks like you are calculating the tangens of wrong angle (between player and X-axis). You need something like the following:
function enterFrameHandler(e : Event) : void{
_turretBarrel.rotation = Math.atan2(enTarget.x - barrel.x, enTarget.y - barrel.y) * 180/Math.PI;
}