I'd like to create a btConvexHullShape for my game with libgdx.
When I tried to do so with the sample code from github:
public static btConvexHullShape createConvexHullShape (final Model model, boolean optimize) {
final Mesh mesh = model.meshes.get(0);
final btConvexHullShape shape = new btConvexHullShape(mesh.getVerticesBuffer(), mesh.getNumVertices(), mesh.getVertexSize());
if (!optimize) return shape;
// now optimize the shape
final btShapeHull hull = new btShapeHull(shape);
hull.buildHull(shape.getMargin());
final btConvexHullShape result = new btConvexHullShape(hull);
// delete the temporary shape
shape.dispose();
hull.dispose();
return result;
}
this did not work.
My 3D object was just an object which looked like a demolished cube with unnecessary vertices and lines (tested with debugdrawer).
Then I tried to enter the second item of the array and built a convex hull from it with
final Mesh mesh = model.meshes.get(1)
this did give me another part of my 3D object which was a gutter of a bowling alley. But also this was demolished and too small.
Finally I tried two other methods I found online:
obtain a btCollisionShape via Bullet.obtainStaticNodeShape(model.nodes);
which resulted in a perfect collision shape, but did not detect collision.
Another problem with that is that it is only for static shapes, which are not intended to move and in my gamer are several objects which need to move.
The last possible and most cumbersome try I made was creating a .bullet file of my model with blender export and load it in libgdx (method can be seen in http://www.badlogicgames.com/forum/viewtopic.php?f=11&t=15263).
This gives me the right CollisionShape and if you set the Gravity in blender to the right one even the objects fall the right way, from up to down the screen.
The only problem with that is as soon as a collision is detected the program crashes.
Unfortunately I can not approximate my shape using primitives as it was suggested in multiple forums. Is there any possibility to create a correct btConvexHullShape in libgdx maybe by manually write a method to create a btConvexHullShape from a set of vertices?
I appreciate your help
EDIT:
You are right for this one as the alley itself does not need to be dynamic. But unfortunately the collsion of the bowling ball (which has a sphere collision shape) was not detected with the static collision shape of the bowling alley I got with Bullet.obtainStaticNodeShape(...), so I thought I'd need another approach to that and I'd also like to know just for educational purposes how it is possible to create a btConvexHullShape from any object I enter.
Currently I'm trying to load an .obj, create a Mesh from it and then tried to create a btconvexHullShape with
final btConvexHullShape shape = new btConvexHullShape(mesh.getVerticesBuffer(), mesh.getNumVertices(), mesh.getVertexSize());
At the moment I think, I'M close to the solution but I still don't got it right, as the cube I'm exporting from blender (as a test object) does not create the right collisionshape (it looks like a damaged cube again).
This method seems to work though, because when creating a mesh like in the link: https://github.com/libgdx/libgdx/blob/master/tests/gdx-tests/src/com/badlogic/gdx/tests/gles2/Shapes.java#L24, and creating a btConvexHullShape from it, I get the correct collisionShape.
Related
I'm new to the game development and I'm trying to code 2D RPG game. im using png images to create objcts and move around. So it is pretty easy to detect collision between rectngles and other simple shapes like this.
if(object1.collides(object2)){
}
what is best way to detect collision of image objects like player, or npc?
The most common way is to create a hitbox that is the approximate shape of your object. The hitbox can be very simple like a rectangle or circle around your object, which is actually invisible.
Have a look at this example, the black box is the hitbox of Mario and you only need to check its boundaries:
But if you really need precision, then you will need to go through the pixels of an image and create the custom polygon. However, the more complex your polygon is, the harder it is to detect collision. Most of the time, game developers rely on physics engines to deal with it.
Hey I am working on a small project in the aim to accelerate my learning and have come into a problem , I have an arraylist of class instances each with and x,y location and each have been mapped to fit inside a window , and am wondering how I may go about implementing functionality which would allow user to click on a node( an ellipse on screen at the mapped x,y value from a instance of class) and for my program to somehow have a toggle to display information about this node in another part of the screen , I have looked for code examples and havent found one that I can get working with my senario here is my class
class Ship{
float yPos;
float xPos;
Ship(String line){
String[] parts = line.split(",");
xPos = float(parts[4]);
yPos = float(parts[5]);
}
}
I am taking in data from a csv file and splitting it etc,
I have alot of code so if my example isnt enough I will add specific parts if needed,
Thanks in advance ,
Kind Regards,
Andrew
You have to add logic for detecting whether the mouse is inside the object.
Exactly how you do this depends on how your object is shaped.
If your object is a circle, you can simply use the dist() function to check whether the mouse is inside the circle.
If your object is a rectangle, then you just have to check whether the cursor position is inside that rectangle. Draw out a few examples on a piece of paper to help figure this out.
If your object is in a grid, then you can map the cursor position to a grid position.
If your object is more complicated, like a polygon, then your logic will have to be more complicated. Google is your friend with this one.
In any case, you're going to have to try something, put together an MCVE (note: this should not be your whole sketch, it should be a small example that we can copy and paste to run ourselves) and ask a more specific question. Good luck.
I just started to write the game of snakes in java. (See this if you don't know what that is: http://codeincomplete.com/projects/snakes/ or http://elgoog.im/snake/). So, when the snake eats an object, its tail grows. In order for the turning physics of the game to work properly, each segment of the tail needs to have its own java.awt.Rectangle hitbox. My question is how can I link these hitboxes/segments of his tail so they always stay together, but are seperate components on my JPanel. Otherwise, if there is a better way to do this then let me know. Thanks.
There s should be a snake object, with a ordered set of simple snake piece, each piece showing type. Practice model view controller, each Snake piece should know nothing about graphics. When you at a segment, put by the last noon trail piece. So, your Snake would contain a list of pieces, like this:
List<SnakePieces> mPieces
SnakePieces should be simple, something like this
public class SnakePieces {
public enum Type {Head,Body,Tail};
public Type type;
}
Feel free to add other functions to SnakePieces as required. When you add a new piece, add it at the location
mSnakePieces.add(mSnakePieces.length()-1,newSnakePiece);
If you can, separate out the model (Snake movement), the view (Puts in the graphics for the piece depending on the type of SnakePiece), and the Controller (Feeds the inputs to the model). That's more advanced than required, but helpful. See Wikipedia on Model View Controller.
Also, see the Android Snake Game, which no doubt has some similarities to your application. Android does it via this:
/**
* mSnakeTrail: a list of Coordinates that make up the snake's body
* mAppleList: the secret location of the juicy apples the snake craves.
*/
private ArrayList<Coordinate> mSnakeTrail = new ArrayList<Coordinate>();
It just knows to draw the first and last tiles slightly differently.
I am doing some java games, and I figure it would be cool if the game is made without importing images. Therefore I need to create custom shapes and hand it to Graphics object to draw. The major character in my game will be a dango which is much like a slime, composing from a imperfect circle and two vertical lines as eyes. I should be able to construct dango by given a parameter indicating size. Also, it will be better if I can modify the position of the eyes, or the bottom curve to present the interaction with the floor. Further more, I would be glad if I can fill it with color and give it some texture or something. But all things start from a circle and two lines.
I checked some APIs including Shape, GeneralPath, PathIterator, Area, Ellipse, and some source code. I learnt how to use GeneralPath to draw straight line, quadratic curve, and bezier curve. But still I don't know how to implement my custom shape. I found this question in stackoverflow but no good answer is posted.
In case someone just read the title and skips the content of this question, I shall emphasize that this question is about create the custom shape, which means to implement the 'Shape' interface. Not just to draw a shape.
So after a day of research, I finally did it. For anyone who holds the same problem with me, I recommend you to do what I have done.
First, refer to the java api source code, here I chose the source code of Ellipse2D.class. Following the source code, you may ignore the 2 inner static class Ellipse2D.Double, Ellipse2D.Float, they are not so important at this point.
To implement the Shape interface, the most important method is
public PathIterator getPathIterator(AffineTransform at) {
return new EllipseIterator(this, at);
}
this method is called by paintComponent to get a PathIterator to draw. So as what the source code does, you may create your own ShapeIterator.
Then the source code of EllipseIterator. As you can see, there are 4 methods (excluding the constructor and the duplicate). You may leave getWindingRule() for furthur research. While isDone() and next() are rather simple to understand.
Then let's focus on public int currentSegment(float[] args).
The return values is int, which should be the static final int fields: SEG_CLOSE, SEG_CUBICTO, etc.. They give instructions on drawing your shape. SEG_MOVE will move the start point, SEG_LINETO will draw a straight line from start point to end point. There are few more like the quatratic curve and Bezier curve, you may check the details at java api.
The argument float[] args should also be regarded as return value statement. It delivers the parameters for the instructions above. For SEG_MOVETO, SEG_LINETO, you need 2 params, so modify args[0] and args[1] ( x and y). For SEG_QUADTO, you need 4 params, and SEG_CUBICTO needs 6.
Carefully follows the source code, it won't be hard to create a shape. I haven't complete all the methods in Shape interface yet, but the shape can already be drawn by a g2d instance.
I am new to the world of libgdx and the world of game programming in general. I want to create a game, but not any game. I have created some basic game like breaks, and pong. But I still cant go any further, I google for good articles, but I always have problems with collision, especially between entities! I want to create a game with slopes like sonic.
Why not use Box2D (libGDX extension) ? It's perfect for platformers.
Do you know how to create rectangles. I assume that you know about rectangles.
if you want to check collision of two rectangles you can do as follows:
Rectangle a = new Rectangle(), b = new Rectangle();
in constructor set rectangles
a.setRectangle(yourX, yourY, yourWidth, yourHeight);
b.setRectangle(yourX, yourY, yourWidth, yourHeight);
in render check collision like this:
if(a.overlaps(b))
{
//do your work
}
U can use OverlapTester class given in SuperJumper Project by LibGdx
Create Your bounds using rectangle class in Libgdx and test them using Intersector class.
This class has many function to test overlapping of rectangles, circles etc..
I recommend you to use box2d if you know the basics.
if you know how to use a rectangle, sprite batch, camera etc. Then you should proceed to Box2d if you don't just take some good tutorial and try to make the application without any extension.this will make your concept clear and you will easily able to grasp the logic behind the game.