How to do a wave effect line in java? - java

I am doing a game like Geometry dash in java , I almost do all of the mechanics but I cant achieve wave effect , I try using a arrylist of lines then draw then in the points of my arrow , but it dint work or maybe I'm very confused
here is a capture of how look right now:
When I press X the arrow goes up and leave a diagonal trace (line) then if I released x its goes down I another line appear in the point of the last line , that whats is look like a wave effect of curves
here is a link of wave mechanic in geometry dash
[geometry dash wave effect][2]
Does I need to use sine wave? becaus its graph look pretty similar but I am not confident about that
I'm having a lot of problems with this mechanic and hope someone could helpe me with any idea and support
thanks for your idea i already solved it ! i finally get the wave effect c: , i draw the lines between the points of my arrow using a normal array
capture3

Don't store lines. Store points. Then draw lines between those points. At a minimum that'll stop it from breaking. Typically you're going shift those points to the left repeatedly at the same speed as the board gets shifted. You're just messing up the math, but, with points it'll at least make it very clear what you're actually getting wrong as well as keeping a contiguous set of lines.
It's not a sine wave or some bezier curves, it's a bunch of line segments as points for the previous position of your ship or whatnot changes.

Related

How to draw an "unordered" line chart with Java with reasonable effort?

I want to draw a line through a number of points with Java. I researched JFreeChart and drew around for a half a day but linechart and all other relevant classes I could find drew an "ordered" graph dependent on time. However, I need something like this in which the line can go "back left again".
I want to draw something like this
Most likely I just need to be told how this kind of chart is called in English and I will be able to research how to draw it.

Java wiggling line

I have an unusual question that hopefully someone can help me with. I want to draw a wiggling or waving line with Canvas (android), but I can't get my head around how to do it. It's going to be a tadpole's tail, so ideally I want it more triangle shaped, larger at one end but that's not essential. I expect it's achievable somehow using a Path, but I'm not sure how. I've Googled around but can't find a solution, suggestions are much appreciated.
Thanks.
Create a sine wave generator that takes the phase (angle) to start from as a parameter and have it return an array of plot points ([x][y]). Then plot these points on your canvas. You can make the line wave by varying the starting phase from 0 to 360 over time. How quickly you vary x determining how fast your line waves.
Try starting here for sine wave generators:
http://www.drdobbs.com/jvm/music-components-in-java-creating-oscill/230500178
If you need a lot of tadpoles, then this might be too processor intensive in which case, you could draw a set of sine waves, say 30 with the beginning angle in steps of 12, then draw these to your canvas in sequence.

How do I iterate through the points on a straight line?

Is there a way to iterate through all the points on a line in java 2D?
I've looked through the class documentation and can't see anything that does it built in. If not, would it be better to extend the class and write my own method which calculates the equation of the line and then goes through each point? (Would this actually work as the theoretical equation of a line and the pixels that it actually draws in seem to be slightly different)
For something moving on a line with a constant velocity, its coordinates are an affine function of time:
x(t) = vx*t + x0
y(t) = vy*t + y0
where (vx,vy) is the constant velocity (or speed) vector and (x0,y0) the origin position (at time 0).
I suggest reading some basic introductory course on kinematics
So you probably don't want to "iterate on the line" but simply to move something on the screen, that is to compute its position at every time quantum.
(I've learned such equations at high-school, in France)
It sounds like you are trying to do something along the lines of a bounding box. Essentially, you should have an imaginary box around your sprite. Then just check when the bounding box intersects with the line. When it does, you move your sprite in the opposite direction.
This question on Game Development Stack Exchange should help.

designing picture puzzle

I am planning to develop a jigsaw puzzle game.
Now I already have images and image pieces, so we don't need algorithm to cut the image in pieces.
On the UI side there would be two sections
First section contains the broken images in random order.
Second section contains the outline of the full image. User need to drag and drop the the cut images onto the outline image.
I am not sure how can the pieces be matched on the the outline image?
Any idea about the algorithm or the starting pointers?
Allow the user to drag each piece into the outline area. Allow the piece to be rotated in 90 degree increments.
Option 1:
If a piece is in the correct location in the overall puzzle, and at the correct angle, AND connected to another piece, then snap it into place with some user feedback. The outside edge of the puzzle can count for a connection to edge pieces.
Option 2:
A neighbor is an adjacent puzzle piece when the puzzle is assembled. When the puzzle pieces are mixed up, they still have the same neighbors. Each puzzle piece (except the edge pieces) has four neighbors.
If a piece is near one of its neighbors at the correct angle relative to that neighbor, then snap it to the other piece. Then allow the two (or more) pieces to be dragged around as a unit, as is done with a single piece. This would allow the user to assemble subsections of the puzzle in any area, much like is done with a physical jigsaw puzzle, and connect the subsections with one another.
You can check the piece being moved to its four neighbors to see if they are close enough to snap together. If a piece has its proper edge close enough to the proper edge of its neighbor, at the same angle, then they match.
There are several ways to check relative locations. One way would be to temporarily rotate the coordinates of the piece you are testing so it is upright, then rotate the coordinates of all its desired neighbors, also temporarily, to the same angle. (Use the same center of rotation for all the rotations.) Then you can easily test to see if they are close enough to match. If the user is dragging a subassembly, then you will need to check each unmatched edge in the subassembly.
Option 2 is more complex and more realistic. Option 1 can be further simplified by omitting the rotation of pieces and making every piece the proper angle initally.
For a regular shapes you can go with a matrix. I recommend this as the first approach. Dividing the puzzle is as simple as defining X,Y dimensions of the matrix. For each piece you have a series of four values then, one for each side, saying whether it is flat, pointing out, or pointing in. This will give you a very classic jigsaw puzzle setup.
How the pieces actually look becomes a strict GUI thing. Now, for the first draft I recommend getting it working with perfectly square pieces. Taking rectangular bits of an image should be easy to do in any GUI framework.
To go to shaped pieces you'll need a series of templates. These will become masks that you apply to the image. Each mask clips out a tiny portion of the image to produce your piece. You'll probably need to dynamically create the masks in order to fit them to the puzzle. At first start with simply triangular connections. Once you have that working you can do the math to get nice bulbous connector shapes. Look up "clip" and "mask" in your GUI framework.
If you wish to do irregular polygon shapes that don't follow a general matrix layout, then you need to do a lot more work. This is why I recommend getting the square first working as a good example. Now you'll need to delve into graph theory and partitioning. Pick up some books on 3D programming -- focusing on algorithms, as they do partitioning all the time. Though I wouldn't doubt if there is a book with this exact topic in it.
Have fun.
the data structure is simple I guess- each peace will point to it's neighbors and will hold the actual shape to display.
on the MMI (UI) of the app - what is your developing environment ?
If it's windows - I would go with c# and winforms or even better wpf.
if it's unix, you'll have to get someone else's advise, as I'm not an expert there.
1) How to break image into random polygons
It seems that you have figured out this part. (from : "Now I already have images and image pieces, so we don't need algorithm to cut the image in pieces.")
2) what kind of data structure can solve the problem
You can create a Class Piece like Scribble class in this example and your pieces would be array of objects of Piece class.
So, you will have two arrays,
(i) actual image pieces array
(ii) image piece outline array
So, whenever you drag and drop one piece on to the full outline of image, it will check whether the image piece object is intersecting more than 80% and ID (member variable of Piece object) of actual image piece and image piece outline matches, then you got the right piece at right place...
3) UI implementation
Check this out.
You could make an array of objects of the class "PuzzleTile"
Every such tile has an image and an integer
After every move, check if the integers are sorted correctly, means:
123
456
789
You could make a function for that which returns a bool.
Note: I'm currently developing under C#, that's why it's probably easiest to realize especially this concept under C#, although other platforms need none up to barely some modification to this.

Connect the dots - connect the line between contour points

I have a gray scale image of 64x64. I found the dots of the contour by a simple algorithm:
find brightest spot (Example: 100)
divide by 2 (100/2 = 50)
define a band around the result (50-5=45 to 50+5=55)
mark all dots that have value in the band (between 45 to 55)
The question now is, how do I decide of the order of connecting the dots?
(All references will be accepted, links, thoughts, etc')
Your algorithm allows the entire image, save for one pixel, to be "the contour". I'm not sure that's exactly what you want; usually a contour is a border between two different regions. The problem with your method is that you can get huge blobs of pixels that have no particularly obvious traversal order. If you have a contour that is a single pixel thick, then the traversal order is much more obvious: clockwise or counterclockwise.
Consider the following image.
........
..%%%%..
.%%%%...
...%%%%.
....%...
........
Here, I've marked everything "dark" (<50, perhaps) as % and everything bright as .. Now you can pick any pixel that is on the border between the two regions (I'll pick the dark side; you can draw the contour on the light side also, or with a little more work, directly between the light and dark sides.)
........
..%%%%..
.*%%%...
...%%%%.
....%...
........
Now you try to travel along the outer edge of the dark region, one pixel at a time. First, you look in the direction of something bright (directly left, for example). Then you rotate around--counterclockwise, let's say--until you hit a dark pixel.
........
..%%%%..
1*5%%...
234%%%%.
....%...
........
Once you hit position 5, you see that it's dark. So, you mark it as part of the contour and then try find the next piece on the contour by sweeping around starting from the pixel you just came from
........
..%%%%..
.0*%%...
.123%%%.
....%...
........
Here 0 is where you came from--and you're not going back there--and then you try pixels 1 and 2 (both light, which is not okay), until you hit pixel 3, which is dark.
In this way, you can walk around the contour pixel by pixel--both identifying the contour and getting the order of pixels--until you collide with the same pixel you started with and will leave from it so that you hit the same pixel that you did the first time you left it. Then the contour is closed. In our example, where we're making an 8-connected contour (i.e. we look at 8 neighbors, not 4), we'd get the following (where # denotes a contour point).
........
..####..
.##%#...
...#%##.
....#...
........
(You have to have this two-in-a-row criterion or if you have a dark region a single pixel wide, you'll walk up it but then not be able to walk back down along it.)
At this point, you've covered one entire boundary. But there might be others. Keep looking for dark pixels next to light ones until you have drawn a contour on top of all of them. Now you've converted your two-level picture (dark & bright pixels) into a set of contours.
If the contours end up too noisy, consider blurring the image first. That will smooth the contours out. (Alternatively, you can find the contours first and then average the coordinates with a moving window.)
In general, a given set of points can be connected in multiple ways to make different shapes.
For example, consider a set of 5 points consisting of the corners of a square and its center. These points can be connected to form a square with one side "dented in" to the center. But which side? There is no unique answer.
Other shapes can be much more complicated, with no obvious way to connect the dots.
If you are allowed to reduce your set of points to a convex hull, then it would be much easier.
I have also tried to create an algorithm that will connect contour dots into the smooth curve. See my open-source project http://outliner.codeplex.com.
The idea is the same as proposed by FUZxxl but I do not understand his worries about complexity: the time of processing is proportional to the total length of all contour strokes.
I don't know if collecting those points will get you far. (I can come up with situations in which it's almost impossible to tell which order they should come in.)
How about going to the brightest point.
Go to the brightness point of, say, 360 points surrounding this point at a distance of, say, 5 pixels.
Continue from there, but make sure you don't go back where you came from :)
Maybe try:
Start at a
Find the nearest point b
Connect a with b
and so on.
Probably not good, as complexity is something like O(n²). You may simplify this by looking only for points near the start, as aioobe suggest. This algorithm is good, if the points are just like 2-3px away from each other, but may create very strange grids.
See also Flood fill
and the lovely applet under SO
mapping-a-branching-tile-path .

Categories