I am using SVG extension in andengine.
This is how i load in my svg textures
this.hugoRegion = SVGBitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.hugoBuildableTextureAtlas, this, "Hugo_Sprite.svg",Width,Height,Columns, Rows);
As you see i have the the tiled region.
Everything works fine until i attach the player sprite that uses the svg to the scene and it extends the sprite beyond the image. If it collides with another sprite it collides like a meter away from the sprite's body.
For example her is a sprite, where the red box is thats how far the sprite is extended. I want it to WRAP exactly around the image..
Anyone ever ran into this issue?
I dont think that would be possible .... you need to create the svg such that it wraps the sprite, programmically that would be tough to achieve...
2nd way would be, if you dont want to re-create the svg then, i guess create a rectangle in your class which extends sprite, keep the rectangle dimensions such that it wraps the sprite and then implement the collides with function with the rectangle instead of whole svg.
Related
I'm using libgdx in android studio, and I am working with sprite collision detection. I need to know how to detect a collision between two irregularly shaped sprites. My sprites are created from png image textures. Is it possible to use the border of the sprite as the detection surface? Using a rectangle or circle drawn around the sprite will not be accurate enough. Thank you for the help!
Use Box2D which is a great and easy to learn library for physics and lighting. After setting bodies correctly, you can set body shape as rectangle, circle, square or any custom shape using paths, you don't need to worry about collision at all. You only take care what should happen while or after collision
I'm trying to build an progress bar in libGDX, for that I have one full horizontal image and in two lines I trying to display 2 different widths of that image:
imageFull:
imageFull.draw(batch,10,80,600,50);
imageFull.draw(batch,10,20,100,50);
the result is:
Its looks like when the width is 'small' its stretched and looks bad.
Why I can't display only part of the image without destroying the left side of the image?
Any ideas how to fix it?
That is normal behavior. If you stretch a image without keeping it's aspect ratio it will deform, it does not know the stretchable part by itself.
9-patch will help you here but you cannot simply draw the sprite as you are doing now (maybe with SpriteDrawable though?).
If I where you I would use the Scene2D Actor named ProgressBar. Feed that a 9-patch image, then it should stretch correctly. Or just use a Image if you want to control it yourself, and feed this image a ninepatch.
A quick way to create a ninepatch is to specify it's stretching regions yourself by hardcoding it.
texture = new NinePatch(yourTexture, A, B, C, D)
Where ABCD corresponds to the following image:
Now create a Scene2D Image with that ninepatch and it should stretch properly.
If you have a this texture already in a Atlas you can also supply the line split:a,b,c,d to the image data in the .atlas file and Scene2D will automatically pick it up as a ninepatch.
If you don't want to use Scene2D and/or ninepatch (but I recommend you to use it) you have to code the behavior yourself. Or cut the texture up yourself and stick the caps on the left and right side of your rectangle. But Scene2D is invented for this and a ton more GUI functionality.
I am currently working on a game in which i need pixel perfect collision with objects. The only obstacle i had gotten to however, is with actually getting a mask of the object. I thought about rendering a sprite of an object (which is properly scaled and transformed) to FrameBuffer, which then i'd convert to a pixmap, but i had no luck doing it (tried many methods, including this)
So, my question is: Is there a way of rendering a single sprite to a pixmap in LibGDX?
Sounds like putting the horse behind the cart. A Sprite is a region of a Texture, which in turn is an image loaded from file aka Pixmap. If you just want to load the image from file then you can do: new Pixmap(Gdx.files.internal("yourfile.png"));. You can also scale and transform your coordinates without rendering to a FBO first.
That said; getting the Pixmap of a FrameBuffer is as "simple" as taking a screenshot while the FBO is bound, because it is exactly that.
fbo.begin();
Gdx.gl.glClear(...);
... //render to the FBO
Pixmap pixmap = ScreenUtils.getFrameBufferPixmap(0, 0, fbo.getWidth(), fbo.getHeight());
fbo.end();
Note that this will look like it is up-side-down.
But you are probably only interested in the pixel data, so you might as well skip the Pixmap step in that case and use the ScreenUtils.getFrameBufferPixels method.
However, that said; using the pixel data for collision detection is most likely not the best solution for whatever it is you are trying to achieve. Instead I'd advise to look into other options, for example have a look at this tool which can be used to convert your images into a collision shape.
So I'm creating a 2D game platform that works with Tiles. Currently it won't let the player go through the any tiles and works fine. Though rather then stopping the player at a solid tile. I would like to stop the player at an actual object. Pretend the triangle is in a tile.
Whats Happening:
What I want:
I would like the player to be able to walk through the tile until their is no more transparents. Basically walk on the triangle.
Player Class http://pastebin.com/SJrzSvVV
Tile Class http://pastebin.com/V3nqxh61]
TileMap Class http://pastebin.com/fuj8dR5K
You should check out the intersects() method provided by the Java2D API.
You can define the two sprites as shapes and adjust the coordinates when the two shapes intersect. I'm guessing you are splitting up a BufferedImage to create the animation frames.
You can draw a Rectangle or Ellipse frame around your sprites and check those for collisions. The frames do not have to be visible but making them visible helps when debugging.
Figured it out, essentially what I did was I was creating this game using tiles. I created something to scan a tile and find out where it's transparent, and allow players to go through it or not depending on whether the transparency is already inside something in the tile or not.
I am making a drawing program, using the Graphics 2D objects (lines, rectangles and ovals, namely) by placing them on a panel. With that in mind, I have 2 questions:
1) How can I store the images currently portrayed on the panel as PNG, JPG or similar file onto disk?
2) I have added a drag function. How can I implement a function so that one can see the "outline" of the rectangle, line or oval, before it is actually put onto the canvas (but not placing the outline on the canvas after the mouse button has been released)? I can't see that any of the MouseListener methods can do such a thing.
1) How can I store the images
currently portrayed on the panel as
PNG, JPG or similar file onto disk?
You can create a BufferedImage and paint any component onto it. The Screen Image class does this for you.
2) How can I implement a function so that one can see the "outline" of the rectangle, line or oval?
In this example, the shape itself may be dragged, rather than its outline, but the draw() method of class Node may be modified as desired. A rectangular outline is used for selection, as on a desktop.
1) ImageIO
http://www.java-tips.org/java-se-tips/javax.imageio/how-to-save-a-bufferedimage-to-a-png-file.html
2) Can't think of an answer for 2.