I am creating a custom libgdx group to show a card image. It contains labels and images like these.
My first image is a texture region which is the actual size of the group. I now need to add the second image to the top left using local coordinate and a label for the name of the suit. This is the card I am using.
public class Card extends Group {
private Player player;
private AtlasRegion cardImage;
private Label lblCard;
public Card() {
super();
init();
}
#Override
public void draw(Batch batch, float alpha){
super.draw(batch, alpha);
batch.draw(cardImage, getX(), getY());
//lblName.draw()
lblCard.draw(batch, getOriginX() + 70..values I have used for this arent working so far);
}
#Override
public void act(float delta){
}
#Override
public void setSize(float width, float height) {
super.setSize(width, height);
}
private void init() {
cardImage = Assets.instance.cards.hearts;
// Add player name
lblName = new Label("H", Assets.instance.menu, "knul-medium", Color.BLACK);
}
}
I have tried using getX() and getY() values but when I draw the group inside a table, only the cardImage fits onto the table whereas the labels all draw at one point in the screen. How can I draw the labels and other texture regions inside my Card using local coordinates and appear at same appear as one image whenever I create a new instance of Card in another class?
Related
I am following this tutorial on 3D game development with LWJGL and OpenGL. And in the series, the instructor is using LWJGL 2 while I'm using LWJGL 3 with GLFW. And in a certain section he uses LWJGL's input implementations only available in LWJGL 2, so I am forced to use GLFW's callback system. I was able to add input functionality for key input, cursor movement, and mouse button input, but when I tried to add a scrolling callback, it seemed that the offsets only return -1 and 1 for the scrolling directions, and when added, the (3rd person) camera translated forward and backwards indefinitely, and the intention was for every scroll movement the camera translates forward or backward a certain distance. In actuality I was expecting to get delta values for each scrolling movement. Previously I was messing around trying to see if I could set the offset values for an amount of delay time then set them back to 0 but to no avail. I tried searching for an answer, but I found nothing related to my problem. I may be stupid, but here's the code.
MouseScroll callback class:
import org.lwjgl.glfw.GLFWScrollCallback;
public class MouseScroll extends GLFWScrollCallback {
private static float XScroll;
private static float YScroll;
private static float DXScroll;
private static float DYScroll;
#Override
public void invoke(long window, double xoffset, double yoffset) {
XScroll = (float) xoffset;
YScroll = (float) yoffset;
// Don't know how to achieve this here
}
/* Returns raw offset values */
public float getXScroll() {
return XScroll;
}
public float getYScroll() {
return YScroll;
}
/* Returns the rate of each scrolling movement */
public float getDXScroll() {
return DXScroll;
}
public float getDYScroll() {
return DYScroll;
}
}
Method for getting YScroll in Input class:
public static float getMouseYScroll() {
return mouseScroll.getYScroll(); // mouseScroll is reference to MouseScroll callback
}
Method using the callback in Camera.java:
private void calculateZoom() {
float zoomLevel = Input.getMouseYScroll() * 0.1f;
distanceFromPlayer -= zoomLevel;
}
Update method in Camera.java:
public void move() {
calculateZoom();
calculatePitch();
calculateAngleAroundPlayer();
float horizontalDistance = calculateHorizontalDistance();
float verticalDistance = calculateVerticalDistance();
calculateCameraPosition(horizontalDistance, verticalDistance);
this.yaw = 180 - (player.getRotY() + angleAroundPlayer);
}
And finally there is camera.move() that gets called every loop iteration
The GLFWScrollCallback is invoked whenever the user generates some scroll operation (up por down or even left or right). As a result, you will set XScroll / YScroll to -1 or 1 and leave it as such until the next scroll event happens. You need to implement some way to reset it to zero. Potentially you could do something like
public float getYScroll() {
float value = YScroll;
YScroll = 0.0f;
return value;
}
but this will only work if you call getYScroll only once per frame. It is not exactly clear how you want to deal with such events (i.e. do you want to store them until consumed - potentially not every frame), but most likely you get away by simply adding some reset method to MouseScroll which you call at the end of every frame - since you query the scroll wheel every frame, no event will be lost.
I currently have a program where I can add rectangles to the scene. These can be selected, rotated, resized and deleted. If I select on a rectangle and click rotate it works, if I add another rectangle and click rotate it still works. However, If I 'DELETE' one of the rectangles then I cannot rotate the other one, or any additional added ones anymore.
I have the following fields and two methods:
private List<Node> selectedShapes = new ArrayList<>();
private double angle[] = {0};
#FXML
private AnchorPane container2;
Rotation:
public void rotateObject(ActionEvent event) throws IOException{
angle[0] = angle[0] + 45;
selectedShapes.get(0).setRotate(angle[0]);
}
Deletion
public void deleteButton(ActionEvent e) {
deletebutton.setOnAction(a -> container2.getChildren().removeAll(selectedShapes));
selectedShapes.remove(0);
}
I feel I am selecting or deleting the selected shapes in the incorrect way. Could someone guide me as to what the correct way to select the current item would be. So if I delete one of the rectangles, the other one should still be rotatable. Thanks
You want to create a Rectangle List, so instead of going through the base class Node why not use list of Rectangle/Shape directly :
List<Shape> selectedShapes = new ArrayList<>();
And for your problem, you use an array angle[], this one can not be extended, and there you call only index 0, i understand (if I'm not mistaken) that this array is to store the angle of each Rectangle to solve it uses a List there too:
List<Double> angle = new ArrayList<>();
And finally to find yourself in your list, uses an int that you increment (at the time of the addition) and decrement (at the time of the deletion) :
int Index = 0;
Edit:
Here's a simple methods that you could follow to organize your project :
private List<Rectangle> recs = new ArrayList<>();
private final double width = 50, height = 50;
private final double x = 200, y = 200;
public void add(){
recs.add(new Rectangle(x, y, width, height));
/*Properties of your rectangle (fill,shape...)*/
}
public void delete(int deleteIndex){
/*Here the root is the container of your Rectangles*/
root.getChildren().remove(recs.get(deleteIndex));
recs.remove(deleteIndex);
}
public void rotate(int recIndex, double angle){
recs.get(recIndex).setRotate(recs.get(recIndex).getRotate()+angle);
}
Good luck !
I have a diamond-sprite and I want to be able to change the colour of the diamond from white, it's original colour to green. However, I can not figure out how to do this.
public class MoveControl : MonoBehaviour {
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.A) )
{
if (GetComponent<Renderer>().material.color == Color.white)
{
GetComponent<Renderer>().material.color = Color.green;
}
}
}
}
This above code is what I have right now and it only works if the material applied to the sprite, being white, is a sprites/default shader. This may not sound like a big problem but whenever I apply a different material of a different colour, such as blue, and change its settings so it has a sprites/default shader, the sprite becomes invisible.
I'm new at Unity and if someone could help me out, it would be very much appreciated
I'm not sure what you're trying to do but this may help you.
public class Material : MonoBehaviour {
Renderer renderer;
Color currentColor;
Color originalColor;
// Use this for initialization
void Start () {
renderer = gameObject.GetComponent<Renderer>(); //If you don't know the gameObject is the object this script is attached to
originalColor = renderer.material.color; //The original color ( in your case white )
}
// Update is called once per frame
void Update () {
if (Input.GetKey(KeyCode.A)) {
if (renderer.material.color == originalColor) { //Here we are checking if the color on the object is the original color
renderer.material.color = Color.blue;
currentColor = renderer.material.color; //Here we are assining the current color
}
}
}
}
I created a material and assigned it to the gameObject in the editor.
Use the code below, if I turn the light off, the blue box will be black.
But there seems no effect on the entity, it is still colorful. What's wrong with the code? Please help thanks.
package com.louxiu.game;
/**
* Created by louxiu on 2/22/16.
*/
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.*;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute;
import com.badlogic.gdx.graphics.g3d.loader.ObjLoader;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.math.Vector3;
public class TestApp implements ApplicationListener {
private PerspectiveCamera camera;
private ModelBatch modelBatch;
private Model box;
private ModelInstance boxInstance;
public Model entityModel;
public ModelInstance entityInstance;
private Environment environment;
#Override
public void create() {
// Create camera sized to screens width/height with Field of View of 75 degrees
camera = new PerspectiveCamera(
75,
Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
// Move the camera 3 units back along the z-axis and look at the origin
camera.position.set(0f, 0f, 3f);
camera.lookAt(0f, 0f, 0f);
// Near and Far (plane) repesent the minimum and maximum ranges of the camera in, um, units
camera.near = 0.1f;
camera.far = 300.0f;
// A ModelBatch is like a SpriteBatch, just for models. Use it to batch up geometry for OpenGL
modelBatch = new ModelBatch();
// A ModelBuilder can be used to build meshes by hand
ModelBuilder modelBuilder = new ModelBuilder();
// It also has the handy ability to make certain premade shapes, like a Cube
// We pass in a ColorAttribute, making our cubes diffuse ( aka, color ) red.
// And let openGL know we are interested in the Position and Normal channels
box = modelBuilder.createBox(2f, 2f, 2f,
new Material(ColorAttribute.createDiffuse(Color.BLUE)),
Usage.Position | Usage.Normal
);
// A entityModel holds all of the information about an, um, entityModel, such as vertex data and texture info
// However, you need an entityInstance to actually render it. The entityInstance contains all the
// positioning information ( and more ). Remember Model==heavy ModelInstance==Light
boxInstance = new ModelInstance(box, 0, 0, 0);
String entity = "creeper/creeper";
ObjLoader loader = new ObjLoader();
entityModel = loader.loadModel(Gdx.files.internal(entity + ".obj"), new ObjLoader.ObjLoaderParameters(true));
entityInstance = new ModelInstance(entityModel, 0, 0, 0);
Texture texture = new Texture(Gdx.files.internal(entity + ".png"));
entityInstance.materials.get(0).set(TextureAttribute.createDiffuse(texture));
// Finally we want some light, or we wont see our color. The environment gets passed in during
// the rendering process. Create one, then create an Ambient ( non-positioned, non-directional ) light.
environment = new Environment();
// environment.add(new DirectionalLight().set(1f, 1f, 1f, -1f, -0.8f, -0.2f));
// environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.8f, 0.8f, 0.8f, 1.0f));
}
#Override
public void dispose() {
modelBatch.dispose();
box.dispose();
}
#Override
public void render() {
// You've seen all this before, just be sure to clear the GL_DEPTH_BUFFER_BIT when working in 3D
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT | GL30.GL_DEPTH_BUFFER_BIT);
// For some flavor, lets spin our camera around the Y axis by 1 degree each time render is called
camera.rotateAround(Vector3.Zero, new Vector3(0, 1, 0), 1f);
// When you change the camera details, you need to call update();
// Also note, you need to call update() at least once.
camera.update();
// Like spriteBatch, just with models! pass in the box Instance and the environment
modelBatch.begin(camera);
modelBatch.render(boxInstance, environment);
// modelBatch.render(entityInstance, environment);
modelBatch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
You wavefront model probably does not have normals, which is required for lighting to work.
Check the log, there should be an error message showing you that you should not use ObjLoader altogether. Instead use G3dModelLoader or even better: use AssetManager and the g3dj or g3db file format.
Export your model from your modeling application into e.g. the FBX file format and convert it using fbx-conv. Do not convert your .obj file into a .g3dx file using fbx-conv, that won't work.
Btw, although not related you might want to take into consideration that:
your camera far/near ratio is very high, you usually should never use a near value below 1.
Unlike what your comment says, ModelBatch is not used to batch geometry and not that comparable to SpriteBatch.
ObjLoader has a loadModel method which accepts a boolean, so you don't have to create ObjLoaderParameters for that (although, as said, you shouldnt be using ObjLoader altogether).
You are creating a Texture without properly disposing it when no longer needed. This will cause a potential resource leak.
Creating a new Vector3 every frame is going to pressure the GC and will cause hick-ups. Simply use Vector3.Y instead of new Vector3(0, 1, 0) to fix that.
I want to make achievements table in my libgdx game. Is there any component in libgdx wchih i can use? For example something that allows me to turn screen into html page or something like this.
simple test for you start:
Variable Class:
Stage stage;
ScrollPane scrollPane;
Table outerTable, innerTable;
.
#Override
public void show() {
stage = new Stage();
outerTable = new Table();
innerTable = new Table();
image = new Image(new Texture(
Gdx.files.internal("badlogic.jpg")));
//innerTable.add(YourActor); for example Image, or TextButton
innerTable.add(image);
scrollPane = new ScrollPane(innerTable);
outerTable.setPosition(0, 0);
outerTable.setSize(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
outerTable.debug();
outerTable.add(scrollPane).fill().expand();
stage.addActor(outerTable);
}
#Override
public void render(float delta) {
Gdx.gl20.glClearColor(0, 0, 0, 1);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
}
look this: https://github.com/libgdx/libgdx/wiki/Table
I hope I have understood correctly, and that this is what you want.