I am using Libgdx along with Bullet physics. I have a vehicle moving. I would like to use a ray from the vehicles origin and in the vehicles forward direction.
How can I create the forward Vector3 so I can apply it to the ray?
I am using Libgdx along with Bullet physics. I have a vehicle moving. I would like to use a ray from the vehicles origin and in the vehicles forward direction.
How can I create the forward Vector3 so I can apply it to the ray?
I am currently working on a dialog window for my android game using libgdx. This dialog window contains a collection of labels and buttons, but should also contain an image. The image represents the “remaining health indicator”, i.e. an empty indicator with a symbol indicating the health of the player. This indicator has to be filled with a colored rectangle representing the remaining amount of health (see screenshot below).
In order to render this on a dialog of libgdx, I have to draw an image and a colored rectangle (the red rectangle indicating the real amount of remaining health). I know that the dialog supports the rendering of images, but I don’t know how to first draw the rectangle.
This is the code I have so far:
public FuelFacilityDialog(GameWorld world, GuiComponent gui) { super("Health check", gui.defaultSkin);
this.world = world;
this.gui = gui;
setModal(true);
setMovable(false);
setResizable(false);
Image healthIndicator = new Image();
Button button1 = new TextButton("heal", gui.defaultSkin);
Button button4 = new TextButton("Exit", gui.defaultSkin);
healthIndicator.setDrawable(new TextureRegionDrawable(AssetLoader.healthIndicatorV));
healthIndicator.setScaling(Scaling.fit);
setObject(button1, true);
setObject(button4, false);
Table imageTable = new Table();
Table buttonTable = new Table();
imageTable.add(healthIndicator).width(100).height(200);
buttonTable.add(button1).width(100).height(50).expandY();
this.getContentTable().padTop(20);
this.getContentTable().padBottom(20);
this.getContentTable().add(imageTable);
this.getContentTable().add(buttonTable).height(200);
getButtonTable().add(button2);
}
Hello I have a body and I want to move it. When I press the mouse button my body should move at that position (on the const speed)on the screen. I have override two methods(x1, y1 – point where my body need to go)
public boolean touchDown(int x1, int y1, int pointer,
int button) {
x = x1;
y = y1;
move.set(x1 - ball.getPosition().x, y1 - ball.getPosition().y).nor();
return false;
}
@Override
public boolean touchUp(int screenX, int screenY, int pointer,
int button) {
move.set(ball.getPosition().x, ball.getPosition().y);
return false;
}
and in my rener method I have write this
ball.applyForceToCenter(move, true);
but my ball don’t move! why? whats wrong? is there are any alternatives how to move mody at specific point on the screen?
I am developing android game using LibGDX . The game data (images , sounds etc.) are maximum 25mb . But when the game is running on Android device it places 110mb there (look at the image)
110mb is really big for 2D game, so what can cause this ?
I currently have something like this using box2Dlights
Ideally, I would like objects casting shadows to be lit, so the object is lit but there is still a shadow cast around it. Right now the object is covered by shadow. I am aware of the soft and softnesslength settings in box2dlights but this is not really what I’m looking for. Softness seems to light up a certain radius from the player rather than just lighting objects. It ends up kind of weird looking:
So for example in the above picture, I would want the entire grey box to be lit, and none of the area to the left/right/behind it to be lit.
Any thoughts would be appreciated. Thanks
I am drawing a continuous line using Pixmap. I want to check for a collision / intersection of my line with the already drawn line.
Now my Image isn’t loading to the screen. I tried to be decorative with the code but since it isn’t working I made it just like the video I’m whatching.
I don’t get any exceptions, but the image doesn’t appear still.
My main desktop launcher class:
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.title = " Game Tests ";
config.width = 500;
config.height = 400;
config.useGL30 = true;
new LwjglApplication(new MyGdxGame(), config);
}
}
My Game class:
public class MyGdxGame extends Game {
@SuppressWarnings("unused")
private TestScreenA GameScreen;
@Override
public void create () {
//loads image
AssetLoaderTest.load();
GameScreen = new TestScreenA(this);
}
@Override
public void render () {
}
}
My Screen class:
public class TestScreenA implements Screen{
@SuppressWarnings("unused")
private MyGdxGame game;
//Camera
private OrthographicCamera camera;
//images etc.
private SpriteBatch batch;
public TestScreenA(MyGdxGame game) {
this.game = game;
//Camera
camera = new OrthographicCamera();
camera.setToOrtho(false, 1920, 1080);
batch = new SpriteBatch();
}
public void render(float delta) {
//clears the screen with white
Gdx.gl.glClearColor(1F, 1F, 1F, 1F);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
//loads images scaled to camera
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(AssetLoaderTest.s, 0, 0);
batch.end();
}
public void resize(int width, int height) {
}
public void show() {
}
public void hide() {
}
public void pause() {
}
public void resume() {
}
public void dispose() {
}
}
My AssetLoaderTest class:
public class AssetLoaderTest {
//Images etc.
public static Texture t;
public static Sprite s;
static void load() {
t = new Texture(Gdx.files.internal("testImgs/badlogic.jpg"));
//Automatically scales texture based off of window size
t.setFilter(TextureFilter.Linear, TextureFilter.Linear);
//a sprite is pretty much the body and the texture is the texture or image of it
s = new Sprite(t);
s.flip(false, true);
}
}
In my app I need a file that I can keep settings like: TutorialSkip: True or AppDev: False and other info. How can I make this file and prevent users editing it?
This has to work in the Core folder for my project as it should work with android windows and ios.
I am creating a top down game and i’m using raycasted lights from Box2Dlights, My light is all set up and working. its attached to the player’s Body, and when the player moves, the sprite I attached to the position moves fluidly, but the light’s position is jerky and lags behind the player, as if I was using the floored the position of the player and so it was only moving on whole numbers.
I create the coneLight like this:
public Box2DConeLightComponent(RayHandler rayH, Color color, float distance){
light = new ConeLight(rayH, 4, color, distance, 0f, 0f, 0f, 45f);
//increasing the rays beyond 4 does nothing
light.setDistance(distance);//10
light.setColor(color);//white
light.setActive(true);
light.setSoft(true);
light.setSoftnessLength(distance);
}
Update the Light like this:
public void setPosition(Vector2 position){
light.setPosition(position);//the position is player.getBody().getPosition()
}
Or I attach it to the player’s Body(i’ve tried both):
public void attachToBody(Body attachment) {
light.attachToBody(attachment);
}
and to start this all off I begin by initializing the lighting like this:
public void initLighting(){
/** BOX2D LIGHT STUFF BEGIN */
RayHandler.setGammaCorrection(true);
RayHandler.useDiffuseLight(true);
rayH = new RayHandler(gWorld);
rayH.setAmbientLight(0.5f, 0.5f, 0.5f, 0f);
rayH.setCulling(true);
rayH.setBlur(false);
rayH.setBlurNum(2);
rayH.setCulling(false);
rayH.setShadows(true);
}
Does anyone know why this is happening?
I’m having some trouble getting the camera to even work at times. Thankfully, its some-what working now. I’m trying to get the camera to follow the players movement but nothing happens.
(cut out the parts that are useless)
public class GameCore extends Game {
private OrthographicCamera cam;
private Player player;
private ShapeRenderer shape;
@Override
public void create() {
cam = new OrthographicCamera(1280 ,720);
cam.translate(Gdx.graphics.getWidth()/2, Gdx.graphics.getWidth()/2, 0);
cam.update();
shape = new ShapeRenderer();
player = new Player();
player.setPos((int)cam.position.x, (int)cam.position.y);
}
@Override
public void dispose() {
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
super.render();
player.render(shape, cam);
player.update();
}
GameCore class (my main one)
public class Player {
public int x,y;
public int dx, dy;
public int width, height;
public double area;
public int hp;
public Player(){
hp = 100;
width = 1150;
height = 500;
area = width*height;
x = Gdx.graphics.getWidth()/2;
y = Gdx.graphics.getHeight()/2;
dx = 5;
dy = 5;
}
public void render(ShapeRenderer shape, OrthographicCamera camera){
shape.setProjectionMatrix(camera.combined);
shape.begin(ShapeType.Line);
shape.circle(x, y, 32);
shape.end();
}
public void update(){
if(Gdx.input.isKeyPressed(Input.Keys.A)){
x -= dx;
}
if(Gdx.input.isKeyPressed(Input.Keys.D)){
x += dx;
}
if(Gdx.input.isKeyPressed(Input.Keys.S)){
y -= dy;
}
if(Gdx.input.isKeyPressed(Input.Keys.W)){
y += dy;
}
}
public void setPos(int x, int y){
x = this.x;
y = this.y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
}
Player class
Hopefully someone here can also point out if i’m doing something wrong with the camera -_-. The docs don’t really provide much information on how these classes are used. Thank you!
So I am trying to combine multiple TextureRegion
s in to one by using a FrameBuffer
. This works extremely well when the TextureRegion
s originate from the same Texture
object/file but as soon as I try to combine TextureRegion
s with different textures of origin, only the TextureRegion
s from one texture file are drawn. The TextureRegion
s with different textures of origin do not get drawn.
For example I have two textures:
Texture tex1 = new Texture(path to texture 1);
Texture tex2 = new Texture(path to texture 2);
TextureRegion reg1a = new TextureRegion(region from tex1);
TextureRegion reg2a = new TextureRegion(region from tex2);
TextureRegion reg1b = new TextureRegion(another region from tex1);
I set up my FrameBuffer
like this:
buffer = new FrameBuffer(Pixmap.Format.RGBA8888, WIDTH, HEIGHT, true);
In my draw method I do the following:
buffer.begin();
batch.draw(reg1a, x, y);
batch.draw(reg2a, x, y);
buffer.end();
batch.draw(buffer.getColorBufferTexture());
Using the above draw method, ONLY reg2
is drawn. reg1 does not show up at all.
buffer.begin();
batch.draw(reg1a, x, y);
buffer.end();
batch.draw(buffer.getColorBufferTexture());
Using this draw method reg1
IS drawn, so there is nothing wrong with the textures.
buffer.begin();
batch.draw(reg1a, x, y);
batch.draw(reg1b, x, y);
buffer.end();
batch.draw(buffer.getColorBufferTexture());
This draw method renders BOTH reg1a
and reg1b
because they both come from the same Texture
obect/file.
What I want to be able to do is draw to TextureRegion
s from different Texture
objects in the same FrameBuffer
and have them both show up.
I have tried having everything from one Texture in one FrameBuffer, and the other in another FrameBuffer, then drawing those two FrameBuffers into a new FrameBuffer, but that produces the same problem.
Any help would be greatly appreciated!
I have been searching everywhere for master volume for sound and music in LibGDX but to no avail. Can anyone tell me if it is implemented somewhere or do i need to implement such functionality myself? Thanks in advance.
I have a model instance of a character, and a model instance of a sword weapon. THe character has a node on his right hand so he can “carry” the sword. Right now I use this code so the sword’s transform follows that of the node.
weaponModelInstance.transform.set(modelInstance.transform).mul(weaponAttachmentNode.globalTransform);
weaponModelInstance.transform.rotate(Vector3.Z, -90); // adjustment for blender coordinates
world.modelBatch.render(weaponModelInstance, world.environment);
this works, except that the model instance gets scaled to the scale of the node, which i do not want, i want the weapon to keep its scale. I’ve tried doing the following code to fix this but I end up getting weird (and wrong) rotations for the sword
weaponModelInstance.transform.set(modelInstance.transform).mul(weaponAttachmentNode.globalTransform);
weaponModelInstance.transform.set(
weaponModelInstance.transform.getTranslation(new Vector3()),
weaponModelInstance.transform.getRotation(new Quaternion()),
new Vector3(1, 1, 1)
);
weaponModelInstance.transform.rotate(Vector3.Z, -90);
world.modelBatch.render(weaponModelInstance, world.environment);
do anyone know how to properly copy the world translation and rotation from the node, but not its scale?
Base question: How can I make a swinging rope in box2d?
Context: The idea is to have a rope where the player can grab it, swing a little bit to get more “momentum” and reach the next platform. Like in the Mario games for Nintendo DS and Wii.
What I did: I constructed a chain like rope with a series of RevoluteJoint
. This is the fixture of the different links:
fixtureDef.shape = robeLinkShape;
fixtureDef.density = 1.0f;
fixtureDef.friction = 0.0f;
fixtureDef.restitution = 0.0f;
fixtureDef.filter.categoryBits = Box2DConst.BIT_ROPE;
fixtureDef.filter.maskBits = Box2DConst.BIT_PLAYER;
BodyDef bodyDef = new BodyDef();
bodyDef.angularDamping = 1.0f;
bodyDef.linearDamping = 1.0f;
I’ve implemented a ContactListener
which creates a RevoluteJoint
to connect the body and on of the links of the body. Now the problem I face is this: The rope does not swing, if I apply a linearImpulse to the player (attached to the rope) e.g. to the left, it goes to the left but it doesn’t swing back. If the player isn’t attached the rope swings.
Last points:
RopeJoint
the last and the first link stay at the same distance but for some reason, the links between still get torn apart. Question is, should I make my rope only of RopeJoins
?I know there are some tutorials out there with some instructions but some of them use a special class which is not available to me and other simply construct a rope and use a MouseJoint
. Again in this scenario my rope thing looks good the problem that when the player body is attached to it it behaves a little bit strange. I know there are also similar posts to mine but since I have some sort of a working solution there solution (if the have any) was not helpful.
Update
If I create the rope solely from RopeJoints I get a nice swinging rope. It behaves exaclty as I woulrd expect. If I then a RevoluteJoint with a density (my player body fixture also has a density) the rope does no longer swing.
I have a game where the user controls a box to collect falling balls. However, very often the balls will just fall through the walls of the box and be flung away. What could be causing the balls to do that?
Here is a video of the issue.
Here’s how I’m creating the box and the balls:
//BOX CODE
private Body pbox() {
pbdef = new BodyDef();
PolygonShape shape=new PolygonShape();
pbdef.type=BodyType.StaticBody;
pbdef.position.set(Gdx.graphics.getWidth()/2,100);
Body body1=world.createBody(pbdef);
FixtureDef fdef = new FixtureDef();
shape.setAsBox(30, 5);
fdef.shape=shape;
fdef.density=1;
fdef.friction=0.5f;
body1.createFixture(fdef);
//
shape.setAsBox(5, 20,new Vector2(-25,15),0);
fdef.shape=shape;
fdef.density=1;
fdef.friction=0.5f;
body1.createFixture(fdef);
fdef = new FixtureDef();
//
shape.setAsBox(5, 20,new Vector2(25,15),0);
fdef.shape=shape;
fdef.density=1;
fdef.friction=0.5f;
body1.createFixture(fdef);
body1.setGravityScale(-3);
body1.setBullet(true);
return body1;
}
//BALLS CODE
private void spawner() {
bbdef= new BodyDef();
FixtureDef fdef = new FixtureDef();
CircleShape circle = new CircleShape();
bbdef.type=BodyType.DynamicBody;
bbdef.position.set(Gdx.graphics.getWidth()/2,Gdx.graphics.getHeight());
bbody = world.createBody(bbdef);
circle.setRadius(5);
fdef.shape=circle;
fdef.density=0;
fdef.friction=0;
bbody.createFixture(fdef).setUserData("ball");
bbody.setBullet(true);
}
I am using a SpriteBatch to render large amounts of sprites very fast. Now I want to modify the sprites with shaders to tint them in a color.
Problem 1: Altering an uniform by getting the shader and setting it between the begin/end of the batch changes the whole scene not only a single sprite.
E.g.:
"EngineView.getShader().setUniformf("u_blue", (float) Math.random());"
Problem 2: It is relatively slow.
Also I found this which seems to cover the same (no proper solution):
(Setting uniform value of a vertex shader for different sprites in a SpriteBatch):
You almost certainly don’t want to actually do this. Changing a
uniform for every quad/sprite is going to severely impact the
throughput of your renderer. Much of the GPU will be sitting idle
while you’re drawing one measly sprite at a time. The whole point of a
“sprite batch” is to draw as many sprites at the same time with as few
draw calls or state changes as possible. If you need random data in
your shader, use a noise texture or a noise function.
stackoverflow.com/questions/4200224/… – Sean Middleditch May 23 at 21:22
My question is, if I don’t want to actually do this, what is it I want to do?
In my Libgdx application I would like to be able to connect to a mysql server securely and read and write data from it without other users injecting, or changing information. I’ve found this: http://www.helloandroid.com/tutorials/connecting-mysql-database but its only for reading what about writing data without people messing with it with other apps? I need a form of unique verification.
I am trying to create game where somenone throw a bomb. I am wondering how I can have an bomb react in a way that is represented by the following picture :
Is there are any tools in Box2D that can help me achieve this?
I have made a card game, and have two methods that sort the players hand.
sortHandBySuit();
sortHandByValue();
I would like one method to be called when the user slides their finger from left to right across the screen, and the other from right to left.
public class InputHandler implements InputProcessor{
.............
public boolean touchDragged(int screenX, int screenY, int pointer) {
world.getHand().sortHandBySuit();
return false;
}
.............
}
How can I use touchDragged to call my methods?
I am a LibGDX beginner implementing a RISK-like game. I want to let the player select territories by clicking on them, but for some reason, the wrong territory is sometimes detected.
The clicked territory is determined like this: I have a Territory
class which extends Actor
. Every territory is the size of the game map, but the constructor takes a .png which determines its shape, by making everything not within that territory transparent. I then ignore clicks on transparent regions of the image.
(I also tried doing it by overriding hit
but then my clicks wouldn’t even be detected.)
All clicks on transparent areas are ignored, but regardless of whether I click on territory 1 or 2, the game reports Territory T1 selected!
.
What might be the issue?
Here’s are the relevant parts of my Territory
class:
public Territory(String filename, String name) {
pixmap = new Pixmap(Gdx.files.internal("Actors/" + filename));
region = new Texture(pixmap);
this.setName(name);
}
public void draw(Batch batch, float parentAlpha) {
Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a*parentAlpha);
batch.draw(region, 0, 0);
}
public void setStage(Stage stage) {
super.setStage(stage);
if(stage != null) {
stage.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y,
int pointer, int button) {
int cX = (int)x;
int cY = pixmap.getHeight() - (int)y;
if((pixmap.getPixel(cX, cY) & 0x000000ff) != 0) {
selected = true;
return true;
}
return false;
}
});
}
}
Here is the part of my Game Screen, where I create the territory objects and add them to the stage:
public GameMap(WorldDom mygame) {
this.game = mygame;
GAMESTATE = 0;
listT = new Territory []{
new Territory("territory1.png", "t1"),
new Territory("territory2.png", "t2")
};
texture = new Texture("DEMO.png");
map = new Image(texture);
table = new Table();
stage = new Stage();
}
public static void draw(){
stage.draw();
//stage.hit(Gdx.input.getX(), Gdx.input.getY(), true);
for(int i = 0; i < listT.length; i++) {
if(listT[i].isSelected()){
System.out.println("Territory " + listT[i].getName()
+ " selected!");
listT[i].setSelected(false);
}
}
}
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if(GAMESTATE == 0) {
draw();
}
if(GAMESTATE == 1) {
/* ... */
}
}
public void resize(int width, int height) {
/* ... */
}
public void show() {
table.setWidth(Gdx.graphics.getWidth());
table.setHeight(Gdx.graphics.getHeight());
table.add(map);
stage.addActor(table);
for(int i = 0; i < listT.length; i++) {
stage.addActor(listT[i]);
}
Gdx.input.setInputProcessor(stage);
}