I'm trying to draw shapes in libgdx that change constantly so I don't think using sprites will work, and I was wondering if it is possible to draw a shape using a function that is called for each pixel to determine if it should be drawn.
What I need to draw is part of a washer (an area bound by two concentric circles and two radiuses), with the circles and radiuses changing constantly.
What I want to know is wether here is a way to draw complex shapes that are determined by a function (the shape would consist of those (x,y) for wich theFunction(x,y)=true) instead of an image
Everything is possible. The best solution really depends on the details of what you want to create. Perhaps you can show an example of what you mean?
Without seeing an example, it looks you might want to start with ShapeRenderer. See the javadocs for detailed information on how to use it. That should get you started and if you find it to be insufficient in some way then you at least you have a more specific question.
Btw, using a Pixmap as suggested by #Ludevik is also possible, but since you want it to change constantly that would imply uploading the entire image each frame which is not very performant.
Would use of Pixmap help?
You can create a pixmap:
Pixmap pixmap = new Pixmap(300, 300, Pixmap.Format.RGBA8888);
and then draw pixels with specific color on that pixmap:
pixmap.drawPixel(x, y, color);
Then you can then create a texture from that pixmap and draw the texture. I'm not sure about the performance of such solution though.
See also Pixmaps in the libgdx wiki.
Related
Theres 2 parts to my problem and they are related. I have a weird shape on my interface illustrated below, I am trying to randomly spawn MovieClips within its' boundaries but I am having some trouble finding a good way to do it.
Question 1: I can run an If condition to check with bitMapData.hitTest to see if the MovieClip has randomly spawn within this shape, if it doesn't simply retry with a new set of random coordinates. However, is there a better way? Like a way to only take into account coordinates within the shape? There will be plenty of MC spawned at one go so I am hoping to lessen the load, or at least find an efficient way to do this calculation.
Question 2: The MovieClips spawned within this shape will eventually have collision detection mechanics that will repel itself when interacted with. Is there a way to contain them within this shape via some kind of boundary detection?
If it was a square, we could easily have contained them with a quick check on all 4 edges, but not with this shape. Currently I am thinking of using bitMapData.hitTest again to detect for out-of-bounds after being repelled, but how do I know which Point() is the nearest 'edge' of this shape to return the MC to?
For question 1: I'm going to go on an assumption that you have some geometry data about the shape.
One method you can use to check if a point is within a shape is to take that point, then draw a line from that point to infinity (the edge of the screen) in one direction. Then count how many times that line intersects an edge of the shape. If it's odd, the point is within the shape (or on the edge) and if it's even, than that point is outside of the shape.
First link in google: https://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-inside-a-polygon/
Or can also try a more simple method (at the cost of doing more work): if the above shape is generated with all squares and rectangles and you know the point and size of all of those: can just do a check for the point vs all the squares and rectangles that make up the shape.
For question 2: As Organis mentioned, I'd go with a library like Box2D to do this. You'll most likely spend tons of time (that you may not want to) if you try to implement this alone.
The big issue is how much cpu or gpu the code uses. You're trying to avoid using any collision detection. Collision detection is having code do calculations to determine the edges of an object. It should be the last option.
Most of the time you know there's no need for collision detection. You know where everything is and how big it is. Everything has a centerpoint and comparing simple number coordinates is the lightweight way to check if there's a need to check further.
When things get near each other, you only need to do a collision detection on the immediate area around an object. See how your shape fits in a box that is easy to check for collisions? That box should get a collision check before the actual jagged shape inside it.
Yes that collision detection box has to be drawn or mapped but it's done when the object is defined, not when the game is playing. If you are using sprite sheets, keep an xml of the boxes or circles around the shapes.
Is it possible/practical to get a pixmap from a modal;
ie;
ModelInstance >> Material >> Pixmap
possible? insane?
In short;
If I have a arbitrary basic textured quad, with one node, one material, one texture can I can a pixmap (with alpha) of that texture in some way?
Usecase:
I have a game with pickupable items. These objects are represented in the (essentially 3d) game world by textured quads.
To represent picking up, I was going to change the mouse cursor - which is already manually controlled by a sprite layer rendered ontop of the model layer
It would be useful to be able to thus get the image of the object being picked up so I can quickly just add a little arrow to it and use that.
I suspect the answer is "cant be done", or at least not without some ott render-offscreen business.
Now, one workaround I know is just to remember where the image resource originally came from and recreate it for the pixmap.
Before doing that, just want to ensure I am not missing something.
Thanks,
Thomas
Additional note; These images are quite small - 100x100 alpha, no more then one being picked up at a time occasionally. Performance thus isn't much of a concern.
This visualization is created using Away3D (Flash):
www.guardian.co.uk/world/interactive/2011/mar/22/middle-east-protest-interactive-timeline
Would it be possible to create something of the same quality (re. interaction, animation, performance, pixel-perfection etc.) using WebGL?
Bonus: How would one set up the basic scene? (without interaction and animation)
Yes, it would be. The scene is not complex at all, so it would have good performance. Interaction, yes, depends how you implement it, but if you are doing project with, for example, Three.js it wouldn't be a problem. Pixel perfection, obviously, yes.
In the scene, you could have curved plane, with texture that is changing UVs of vertices when you use 'navigate'. Pins could be done with particle, or better, simple quads with transparent textures. To have pixel-precise pin picking, you could depth-test pins on ray trace, or use pin-shaped geometry with shader, which probably the best solution.
So, basic scene - curved plane with per-vertex-changing-UV, pin-shaped and texture pins.
Alternatively, you could do exactly the same thing with 2D canvas. All elements are just drawn and scaled, text would be much simpler, and picking would be with 2d calculations.
Hope this helps.
I'm new to html5. And I'm trying to create a basic painting tool.
What I want to do in this tool is to have one or more shapes(maybe overlapping) and to paint the shapes without getting the colors overlapped. If a circle is drawn inside a rectangle and if I start coloring the circle, the rectangle should not be painted even if the mouse is dragged over it unless the dragging starts inside it.
To achieve this should I use multiple canvases or shapes?
Thanks in advance.
Well, first you need to program in the idea of keeping track of separate shapes. If you haven't already done that see here for a tutorial.
I imagine your shapes will all be kept as images or in-memory canvases themselves. I'm not sure how else you can do it.
There are a million ways you could do this, here's one:
When you start your drawing operation you need to detect which shape you're on. Then you draw that shape to an in-memory canvas and switch that temporary canvas' globalcompositeoperation to source-atop. This will make sure the paint can only paint in the already opaque regions of that shape (if that's your intent here, which it seems to be).
All while you are painting you will want to update the temporary canvas and redraw the main canvas constantly. While you are redrawing the main canvas, instead of painting that shape's image file you'll want to paint the temporary canvas (if you use canvases to keep the shapes you can just update those in real time).
If you are not using temporary canvases for each shape, when you stop the drawing operation you are gonna have to update the image associated with the shape to complete the operation.
Using an in-memory canvas (not added to the DOM) for every shape (that is the size of the shape and no larger) will make coding things slightly easier and might not be that bad on performance. I'd give it a try with 100 and 1000 (or more) in-memory canvases on your targeted platforms to see though.
The alternative is to use one in-memory canvas and have an HTMLImageElement (png) that represents every shape, but using the canvas.toImageURL function can be a bit of a performance hit in itself. I'd try both methods to see which works best in your case. If the shape count is small enough, it probably doesn't matter which.
I have an image of a basic game map. Think of it as just horizontal and vertical walls which can't be crossed. How can I go from a png image of the walls to something in code easily?
The hard way is pretty straight forward... it's just if I change the image map I would like an easy way to translate that to code.
Thanks!
edit: The map is not tile-based. It's top down 2D.
I dabble in video games, and I personally would not want the hassle of checking the boundaries of pictures on the map. Wouldn't it be cleaner if these walls were objects that just happened to have an image property (or something like it)? The image would display, but the object would have well defined coordinates and a function could decide whether an object was hit every time the player moved.
I need more details.
Is your game tile based? Is it 3d?
If its tile based, you could downsample your image to the tile resolution and then do a 1:1 conversion with each pixel representing a tile.
I suggest writing a script that takes each individual pixel and determines if it represents part of a wall or not (ie black or white). Then, code your game so that walls are built from individual little block, represented by the pixels. Shouldn't be TOO hard...
If you don't need to precompute anything using the map info. You can just check in runtime logic using getPixel(x,y) like function.
Well, i can see two cases with two different "best solution" depending on where your graphic comes from:
Your graphics is tiled, and thus you can easily "recognize" a block because it's using the same graphics as other blocks and all you would have to do is a program that, when given a list of "blocking tiles" and a map can produce a "collision map" by comparing each tile with tiles in the "blocking list".
Your graphics is just some graphics (e.g. it could be a picture, or some CG graphics) and you don't expect pixels for a block to be the same as pixels from another block. You could still try to apply an "edge detection" algorithm on your picture, but my guess is then that you should rather split your picture in a BG layer and a FG layer so that the FG layer has a pre-defined color (or alpha=0) and test pixels against that color to define whether things are blocking or not.
You don't have much blocking shapes, but they are usually complex (polygons, ellipses) and would be unefficient to render using a bitmap of the world or to pack as "tile attributes". This is typically the case for point-and-click adventure games, for instance. In that case, you're probably to create path that match your boundaries with a vector drawing program and dig for a library that does polygon intersection or bezier collisions.
Good luck and have fun.