I'm trying to upload my 3d gun model from blender into Libgdx's latest version and I am unable to see my model. This project is due in two weeks and I really need some help here.
public static Entity loadGun(float x, float y, float z) {
ModelLoader<?> modelLoader = new G3dModelLoader(new JsonReader());
ModelData modelData = modelLoader.loadModelData(Gdx.files.internal("data/GUNMODEL.g3dj"));
Model model = new Model(modelData, new TextureProvider.FileTextureProvider());
ModelComponent modelComponent = new ModelComponent(model, x, y, z);
modelComponent.instance.transform.rotate(0, 1, 0, 180);
Entity gunEntity = new Entity();
gunEntity.add(modelComponent);
gunEntity.add(new GunComponent());
gunEntity.add(new AnimationComponent(modelComponent.instance));
return gunEntity;
}
This can be a fiddly thing to accomplish, below are the things I've come up against before:
Ensure you are setting the fbx export scale to 0.01 in blender. This is because libgdx imports objects in centimetres.
Make sure you flip the y axis when using fbx-conv. (add the -f option).
Loading using the LibGDX AssetManager is usually preferred to manually using readers e.g.
AssetManger am = new AssetManager();
am.load("data/GUNMODEL.g3dj", Model.class);
am.finishLoading();
Model model = am.get("data/GUNMODEL.g3dj");
N.B. regarding your code, the ModelLoader should be disposed of at the end of the program.
Related
I'm working on a new game written with LibGdx Engine and Java.
I've got a problem with some of the physics in this game.
I want to shoot the arrow in a ballistic trajectory (angry bird style)
and can't find the equation to do so .
I am using these velocity equations:
float velx = (float) (Math.cos(rotation) * spd);
float vely = (float) (Math.sin(rotation) * spd);
I add this to the current position and the arrow shoots in one direction - straight.
I thought maybe changing the rotation would help me achieve what I want (a ballistic path).
It does help, but I want to have the trajectory as well.
I saw this
ProjectileEquation class that someone already posted but didn't know how to work with it:
public class ProjectileEquation
{
public float gravity;
public Vector2 startVelocity = new Vector2();
public Vector2 startPoint = new Vector2();
public Vector2 gravityVec = new Vector2(0,-10f);
public float getX(float n) {
return startVelocity.x * (n ) + startPoint.x;
}
public float getY(float n) {
float t = n;
return 0.5f * gravity * t * t + startVelocity.y * t + startPoint.y;
}
}
I'm looking for some help to help me use this class for ballistic trajectories.
This is how I tried using it:
for(int i =0;i<30;i++)
{
Texture f = ResData.Square_1;
ProjectileEquation e= new ProjectileEquation();
e.gravity = 1;
e.startPoint = new Vector2(bow.getX(),bow.getY());//new Vector2(-bow.getX(),-bow.getY()); //My bow is opposite so it suppose to work fine
e.startVelocity = getVelocityOf(bow.getRotation());
Vector3 touchpos = new Vector3();
s.draw(f,e.getX(i) ,e.getX(i),5,5);
}
The ProjectileEquation class you post looks like it'll calculate the X and Y position given a time delta, so the float you pass in should be the time delta since you started the arrow moving (in seconds).
That code will not give you the angle of the arrow though. To find that, I would suggest you keep hold of the previous X and Y, then you can use Math.atan2() to calculate the angle based on the previous XY and the current XY. Google atan2 for a load of info on how to use it.
The very best way to do this however would be to use Box2d and model the scene correctly. Then you wouldn't have to get involved in the maths at all. I read somewhere that that's what Angry Birds uses, and is an excellent choice for modelling these sorts of physics games.
I hope your game goes well.
I am working on a game. I constructed my player as here: (I am using a gravity on my world)
private ArrayMap<String, GameObject.Constructor> constructors = new ArrayMap<String, GameObject.Constructor>(String.class, GameObject.Constructor.class);
private ArrayList<GameObject> instances = new ArrayList<GameObject>();
assets.load("hand.obj", Model.class);
...
model = assets.get("hand.obj", Model.class);
constructors.put("hand", new GameObject.Constructor(model, new btBoxShape(new Vector3(2.5f, 7.5f, 2.5f)), 1f));
...
hand = constructors.get("hand").construct(); // that construct method returns me model, shape and constructions.. the GameObject extends ModelInstance, so i can use it like a modelinstance
hand.transform.setToTranslation(x, y, z);
hand.body.proceedToTransform(hand.transform);
hand.body.setUserValue(instances.size());
hand.body.setCollisionFlags(hand.body.getCollisionFlags()| btCollisionObject.CollisionFlags.CF_CUSTOM_MATERIAL_CALLBACK);
world.addRigidBody(hand.body);
hand.body.setContactCallbackFlag(OBJECT_FLAG);
hand.body.setContactCallbackFilter(OBJECT_FLAG);
Then, in render method I am moving it:
if (!hand.body.isActive()) hand.body.activate();
if (Gdx.input.isKeyPressed(Keys.W)){
hand.body.translate(new Vector3(0,0,-1));
}
else if (Gdx.input.isKeyPressed(Keys.S)) {
hand.body.translate(new Vector3(0,0,+1));
}
That's nice! The moving now works good, when I am moving at the flat ground. Whenever there is an object before me, it is not as expected. Because my player shape is biger than
object shape (which is 2.5f, 2.5f, 2.5f), it kind of falls on it. So I would like to set the rotation to be still the same, so the object will not be rotating (so it will not "fall" on the object before). And so I tried to do it, and I failed. Because there are functions like rotate, and I want to something like setRotation
. And so, there is a setToRotation, but you can not pass there a Quaternion.
I need help. I tried to use a btKinematicCharacterController but it was bad. The ghostObject every time falled through object, but the objects got a collision from him.
and so I want to create a player movment, like in games like Wow, minecraft, and so on.
I looked at the btKinematicCharacterController again. The reason why my ghostobject falled through the ground was. Generally, I don't know the reason: D probably I was using another broadphase for ghost, that for world. This line fixes it: characterController.setUseGhostSweepTest(false);
and I am getting another problem, when I am walking on my ground (a lot of objects), the character is getting to lesser Y position. I don't know why.
Here is my construction:
btPairCachingGhostObject ghostObject;
btConvexShape ghostShape;
btKinematicCharacterController characterController;
Vector3 characterDirection = new Vector3();
Vector3 walkDirection = new Vector3();
...
ghostObject = new btPairCachingGhostObject();
ghostObject.setWorldTransform(hand.transform);
ghostShape = new btCapsuleShape(5f, 0.5f);
ghostObject.setCollisionShape(ghostShape);
ghostObject.setCollisionFlags(btCollisionObject.CollisionFlags.CF_CHARACTER_OBJECT);
characterController = new btKinematicCharacterController(ghostObject, ghostShape, .00001f);
// And add it to the physics world
characterController.setUseGhostSweepTest(false);
world.addCollisionObject(ghostObject,
(short)btBroadphaseProxy.CollisionFilterGroups.CharacterFilter,
(short)(btBroadphaseProxy.CollisionFilterGroups.StaticFilter | btBroadphaseProxy.CollisionFilterGroups.DefaultFilter));
world.addAction(characterController);
... (in render - moving)
if (!load)
{
if (Gdx.input.isKeyPressed(Keys.LEFT)) {
hand.transform.rotate(0, 1, 0, 5f);
ghostObject.setWorldTransform(hand.transform);
}
if (Gdx.input.isKeyPressed(Keys.RIGHT)) {
hand.transform.rotate(0, 1, 0, -5f);
ghostObject.setWorldTransform(hand.transform);
}
// Fetch which direction the character is facing now
characterDirection.set(-1,0,0).rot(hand.transform).nor();
// Set the walking direction accordingly (either forward or backward)
walkDirection.set(0,0,0);
if (Gdx.input.isKeyPressed(Keys.UP))
walkDirection.add(characterDirection);
if (Gdx.input.isKeyPressed(Keys.DOWN))
walkDirection.add(-characterDirection.x, -characterDirection.y, -characterDirection.z);
walkDirection.scl(4f * Gdx.graphics.getDeltaTime());
// And update the character controller
characterController.setWalkDirection(walkDirection);
// And fetch the new transformation of the character (this will make the model be rendered correctly)
}
world.stepSimulation(delta, 5, 1f/60f);
if (!load)
ghostObject.getWorldTransform(hand.transform);
How to fix this?
I set up the debugDrawer, so i was able to see the shapes of the bullet objects.. and my problem was that: the ghostObject(charController) was pushing my objects down.. Although my objects were static. So i set the mass of the objects to 0 and problem is fixed. But I still dont know, how it could push static objects. But i dont care. :)
EDIT: i will accept this answer in 2 hours, because now i cant.
I want to create some effects in cocos2d-x by updating raw color data of sprite, does cocos2d-x supply any ways to do that?
Update: My buffer is 4-bytes (A-R-G-B) for each pixels, viewport dimensions are 640x480. So, the buffer has 640 * 480 * 4 = 1228800 bytes in length and I update its content frequently.
This solution regenerates the texture each time it is changed.
Note: the texture in this code uses the format RGBA - not ARGB.
The data(/texel) array m_TextureData and the sprite are allocated only once but the Texture2D object has to be released and recreated every time which might be a performance issue.
Note: the class names are the new ones from Cocos2d-x 3.1.x. In the main loop there's an alternative part for 2.2.x users. To use that one you have to use also the old class names (like ccColor4B, CCTexture2D, CCSprite).
in header:
Color4B *m_TextureData;
Texture2D *m_Texture;
Sprite *m_Sprite;
in implementation:
int w = 640; // width of texture
int h = 480; // height of texture
m_TextureData = new Color4B[w * h];
set colors directly - e.g.:
Color4B white;
white.r = 255;
white.g = 255;
white.b = 255;
white.a = 255;
m_TextureData[i] = white; // i is an index running from 0 to w*h-1
use data to initialize texture:
CCSize contentSize;
contentSize.width = w;
contentSize.height = h;
m_Texture = new Texture2D;
m_Texture->initWithData(m_TextureData, kCCTexture2DPixelFormat_RGBA8888, w, h, contentSize);
create a Sprite with this texture:
m_Sprite = Sprite::createWithTexture(m_Texture);
m_Sprite->retain();
add m_Sprite to your scene
in main loop:
to change color/texels of texture dynamically modify m_TextureData:
m_TextureData[i] = ...;
in Cocos2d-x 2.x:
In 2.2.x you actually have to release the old texture and create a new one:
m_Texture->release(); // make sure that ccGLDeleteTexture() is called internally to prevent memory leakage
m_Texture = new Texture2D;
m_Texture->initWithData(m_TextureData, kCCTexture2DPixelFormat_RGBA8888, w, h, contentSize);
m_Sprite->setTexture(m_Texture); // update sprite with new texture
in Cocos2d-x 3.1.x
m_Texture->updateWithData(m_TextureData, 0, 0, w, h);
Later, don't forget to clean up.
in destructor:
m_Sprite->release();
m_Texture->release();
delete [] m_TextureData;
I am creating a Windows Phone 8 app and I'm working with camera. When I don't use any shader, my C# code works perfectly:
void photoDevice_PreviewFrameAvailable(ICameraCaptureDevice sender, object args)
{
sender.GetPreviewBufferArgb(captureData);
previewTexture.SetData<int>(captureData);
}
...
spriteBatch.Begin();
spriteBatch.Draw(previewTexture, new Vector2(backBufferXCenter, backBufferYCenter), null, Color.White, (float)Math.PI / 2.0f,
new Vector2(textureXCenter, textureYCenter), new Vector2(xScale, yScale), SpriteEffects.None, 0.0f);
spriteBatch.End();
I am getting camera input in realtime. However, I'm (just trying to passthrough the input) trying to use a pixel shader:
Texture2D MyTexture : register(t0);
sampler textureSampler = sampler_state{
Texture = (MyTexture);
Filter = MIN_MAG_MIP_LINEAR;
};
...
float4 pixelShader(float4 color : COLOR0,
float2 texCoord : TEXCOORD0) : SV_Target0
{
float4 textureColor = tex2D(textureSampler, texCoord);
return textureColor;
}
The shader runs fine (assigning it at the beginning of the sprite batch) with no exceptions etc but all I'm getting is red color. The whole output is pure red. What could be the reason? I am new to shaders and I'm trying to understand how they work, especially with samplers. Thank you.
If I'm not wrong, you need to get the pixel data in BGRA format, not RGBA. Could you check if it works for you?
You can check this article.
Creating a Lens application that uses HLSL effects for filters
Regards,
Pieter Voloshyn
I am fooling aroung with Papervision3D in as3. I am currently stuck with a BitmapEffectLayer. WHen I want to add an effect to an object, the object with the effects will always be rendered infront of everything. That means, even though its behind another object in the cordinat system, it gets drawn infront of it.
Heres some source code, dunno if it helps.
spherer = new Sphere(shadedMaterial, 120, 20, 14);
//spherer.x = 0;
//spherer.y = 0;
//spherer.z = 0;
displayEarth = new DisplayObject3D();
displayEarth.x =0;
displayEarth.y = 0;
displayEarth.z = 0;
displayEarth.addChild(spherer);
smallSphere = new Sphere(flatMaterial, 10, 10, 10);
smallSphere.x = 0;
smallSphere.z = 130;
smallSphere.y = 00;
displayEarth.addChild(smallSphere);
//scene.addChild(smallSphere);
scene.addChild(light);
var partMaterial:ParticleMaterial = new ParticleMaterial(0x000000, 1.0, ParticleMaterial.SHAPE_CIRCLE);
var part:Particle = new Particle(partMaterial, 3, 0, -150, 30);
var part2:Particle = new Particle(partMaterial, 3, 0,0,135);
var partsHolder:Particles = new Particles();
partsHolder.addParticle(part);
parrr.push(part);
partsHolder.addParticle(part2);
parrr.push(part2);
var effectLayer:BitmapEffectLayer = new BitmapEffectLayer(viewport, stage.stageWidth, stage.stageHeight, true, 0x000000, BitmapClearMode.CLEAR_PRE);
effectLayer.drawLayer.blendMode = BlendMode.OVERLAY;
effectLayer.addDisplayObject3D(smallSphere);
viewport.containerSprite.addLayer(effectLayer);
displayEarth.addChild(partsHolder);
scene.addChild(displayEarth);
effectLayer.addEffect(new BitmapLayerEffect(new BlurFilter(2,2,2)));
And now, the "smallSphere" which is attached to the effectLayer, will always be rendered infront of the "sphere".
Any help is appreciated!
- David
When you set a DisplayObject3D to it's own layer (.useOwnContainer = true; being the simplest option, essentially you get that 3D object rendered into a separate 2D sprite on the 2D typical display list. Using ViewportLayer makes it easier to control this stacking/ordering, so be sure to read through Andy Zupko's detailed post on ViewportLayers. The idea is that if you add 3D objects to 2D render layers, you'll have to deal with sorting. You can for example check the z position of 3D objects and based on that sort the layers if the objects move a lot in 3D. You obviously loose speed when doing this type of operations, so it's best to plan things a bit (e.g. what moves in the scene, what doesn't, what are pros and cons of adding an effect, etc.)
Also it's probably a good idea to bear in mind that the Papervision3D project has not being updated in some time now. It currently only supplies software rendering (with Flash Player 9 and I think partially with Flash Player 10's new drawing API, but probably not in the stable branch). You might want to have a look at Away3D as it's still currently being developed. You can use the Flash Player 10 software rendering API or even the light weight Away3DLite version (which is faster than Papervision3D I think) but there's also the Away3D 4.0 version which uses hardware acceleration.