Java recursive drawing - java

This is an assignment, so I'd prefer an explanation more than straight code.
The task is to draw circles recursively like the picture
I can't work out the pattern to draw it though. A turtle class is used to draw the circles.
This is what I've managed so far
private void draw(int level, double size){
if(level < 0) return;
turtle.setPenDown(true);
turtle.drawOval(size);
//Here I think there needs to be code to move to the positions of the other circles?
draw(level-1,size/2);
}
But of course it only draws this at the moment:
EDIT:
Maybe some code could help, this isn't going to well for me.

If it's got to be recursive then I would suggest something like the following, I'll just talk algorithm wise since you asked to not have code come back.
If we're going to think of this as a long series of self-similar circles then the general process is pretty straightforward.
Check if there are n circles in your current row. The row is defined as whatever direction is forward/backward for your turtle. If you want you could store the circles drawn in a 2-d list or array to make checking easier.
If there are n circles, then turn right, otherwise draw a circle and move forward.
That will draw the circles from the outside in. If you want it from the inside out it probably makes less sense to do recursion but it would be something like this:
Function: Draw circle. Turn right if you can, otherwise move forward.
Stop when the total number of circles is equal to n^2
Hope that helps.

Related

Tetris clone in Java, out of bounds while moving a piece?

I'm trying to write a tetris clone. My board is a 10x20 array of integers on which 0 means no tile, 1-7 means a tile of specific color. It is constantly translated to graphic interpretation. My shapes are 4x4 arrays of integers. I've just come to a realisation that while making all of the shapes 4x4 makes some things easier, it also causes a problem when moving a piece left and right. Let's say we've got the I shape:
0010
0010
0010
0010
Now, if I move it to left wall there will always be a two units long gap, since the 0s cant move outside of the main board array. What would be the easiest way to allow the 1s to move to the left wall without causing an out of bounds exception?
Using your described method, one way to simply avoid getting the IndexOutOfBoundsException would be to expand your board to be 18 x 24 instead of 10 x 24, and then write in additional code that doesn't let you move a block left/right if there would be any 1's in the object array that leave the middle 10 squares of the grid. By adding this 'padding' to your grid, you avoid the exception and should still be able to implement.
I hope this approach makes sense to you. If not I can provide a more pseudo-code driven answer, but I hope you get the idea. (Just comment if you have any questions.)
BTW, #assylias makes a very good point. It is important to have a good design/plan before you start implementing things to avoid road-bumps like these. It comes with experience, so keep practicing and you will get the hang of it.
NOTE: As Nick pointed out in the comment, another way of doing this is to simply check if any 1's leave the grid before moving any of the arrays. This is certainly possible (and arguably a more elegant/simple solution), although it may be a bit harder to get right.
You need a way of detecting collisions with borders and existing pieces.
You would probably have a fixed handle on each piece, you'll also have an X and Y offest for the piece which indicates it's position as it moves down the grid.
To stop a piece moving out of bounds, loop through the 4*4 matrix of the moving piece and for the bits which are set to 1 simply check
to make sure that the X position + X offset is >= 0 and <=9 and the Y Position is >=0 and <=19 if either of these checks fail
then your piece would be moving outside the limits of the board array so stop the change to x or y offest as appropriate.
Translating the co-ordinates of the set bits in your piece matrix with the board array also allows you to check and see whether your piece has collided with a tile already in the board.
You should be doing these collision checks when a piece rotates as well I would have thought.

Java Collision Detection Not Working

I have made threads in the past about similar questions but because of my lack of detail the answers have not really been related to what I needed so I am going to try explain my question in as much detail as I can and hopefully it will be easier for you to understand what I require.
I watched Bucky's slick game tutorials on youtube and made a 2D Java game, the game is basically a 2D player viewed from above (birds eye view) can move around a 2D map with user key input (up, down, left, right). The map the player moves around is very small so that meant boundaries had to be set so that the player could not walk off of the map, to give you a better idea of how this was done, here is the tutorial for setting up the voundries:
http://www.youtube.com/watch?v=FgGRHId8Fn8
The video will also show you exactly what the game is like.
The problem is, these boundaries only require one axis meaning that if the player is walking down you say something like "if player gets to the coordinate (number) on the X axis change the player movement to the opposite direction so that he can not go any further."
Now this creates a problem for me because this only requires one axis so it easy to set up and understand but if you look on the video, on the map there is a house and I want my player not to be able to walk over that also but this deals with 2 dimensions, I have looked at things like rectangle collisions and have seen things relating to them in the other posts but I get confused because I am new to Java and havent really done much with it at the moment apart from watching Bucky's tutorials.
My code at the moment for my game class has got the following methods: init, render and update. So to sum it up I really just want to set up a way of not letting my player walk through the house, I will mention also (I should have mentioned it in my other threads) as I am very new to Java, could you please take a step by step method of showing me how to set up the collisions, I mean even the basics of things like making the rectangle if required.
If my code is required please tell me and I will post it as soon as possible.
Thank you in advance.
You can set up the board as a 2x2 grid of a class that has has property such as 'isBlocked'. By default the edges of the board would have this property set to true to prevent the character from walking off the edge. When you add other obstacles such as a house or a wall the grid position(s) the object occupies would also have the property set to true. Then when moving a character you just check if the grid position the character moves to has the property set to false to see if it's an allowable move. This also makes it quite trivial to save the level data so you can just load them from disk later on.
Two possible options:
Extend Shape or Rectangle or the relevant Slick objects (they should exist IMO) and just check for intersect()
Look for (x1,y1) and (x2,y2) values such that it starts outside and ends up inside.
Assuming you have intersect() methods:
//Grab the previous position
prevPosX = movingobject.X;
prevPosY = movingobject.Y;
//Update the position
movingobject.update();
//Test for collision
if(movingobject.intersects(targetobj)) {
//If it collided, move it back
movingobject.X = prevPosX;
movingobject.Y = prevPosY;
//And reverse the direction
//(might want to do other stuff - e.g. just stop the object)
movingobject.speed *= -1; //Reverse the speed
}
in this case your update class should also add one more condition to look for the house. let say the cooridnates of house(assuming rectanglular house here for other shape just change x and y values) are (x1,y1)(x1,y2)(x2,y2)(x3,y1) you have to add a condition to make sure
x value is not between x1 and x2 and at the same time y value cannot between y1 and y2.

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.

represent 2d pile of ball in java

I'm creating an application where I need to model (and display) a large number of 2-dimensional 'balls' (circles) using java. I've started by creating an ArrayList of Circle objects (a class that I defined, which has x/y pixel coordinates of the center of the circle, radius, color, and velocity in the x & y directions). I use a java.util.Timer object with a repeated TimerTask to control the motion of the circles. The circles vary in size from around 10-50 pixel radii.
What I want to happen is have balls fall from the top of the screen (randomly distributed across the x-axis) until they reach the bottom of the screen, which acts like a floor -- ball that reach it stop moving, and balls that reach stopped balls roll downhill on the other balls until they rest at a low/flat point. In the future I might want to make their behaviour a bit more complicated, so I want to create flexible code so I can easily expand. How it works right now is that every time period each circle check to see how close they are to every other circle. If there are no other circles nearby they continue falling normally. If two (or more) circles are close to each other there is code to determine how they will interact.
I have it working perfectly for small numbers of circles (< 200), but when I get larger numbers of circles (> 400) it starts slowing down significantly. I'm sure I can optimize some of the logic to make the comparisons a bit faster, but I was wondering if there are any ways of storing the circle in some structure besides an unorganized ArrayList that would let me easily find circles that are close to each other, so I don't have to do the N^2 operation of comparing each circle to every other one and, instead, just compare a circle to the 5-10 circles closest to it. For instance, I've considered using a 2D array to represent every pixel (or maybe square of 5x5 pixels) and store a circle where its center lies (so I could check if there are circles in any of the nearby cells, and ignore the rest). However, this still seems pretty inefficient as if I'm using a 800x1000 pixel canvas with 500 circles there would be a TON of empty spaces in the 2d array that I'd waste time checking. I've also considered some sort of hashmap, but I haven't thought of a great way to use that either.
So, can anyone think of an efficient way to store these circles that corresponds to it's location in 2d space and makes it easy to find nearby circles? Thanks in advance!
You can use a QuadTree to find close neighbours. Or you can simply sort by one direction, which would be far easier to implement and should still allow you to reduce the number of candidate neighbours to a small window.
Perhaps your idea of a 2D array is not so crazy. What I think you want is one List of all your circles, and a 2D array that would reference the circles also. So at each time, you can iterate over your List<Circle> to check each one. Each Circle has x,y coords and you only have to loop on your array for (x,y +/- 5). There is no need to check the whole possible space for Circles, because you already are keeping track of each Circle's center. Just grab the center, and check around it for other circles.

Making Character move around circle

So I have a 2d game which normally just has gravity and "flat" levels however I have added in "planets" which have their own gravity.
I have a function called addForce(float xForce, float yForce) that I use to move my character. So say if I called player.addForce(1, -1); the player would move up and to the right(albeit slightly). This worked fine on the levels with regular downward gravity, however with planets it is not so.
There is another float called earthAngle which is:
atan2(player.getY()-earth.getY(), player.getX()-earth.getX());
What I did for the jumping code on the planets is:
player.addForce(cos(earthAngle)*1500, sin(earthAngle)*1500);
which works well. However I am stuck on how to make the character walk around the planet.
Currently for the movement code I have:
player.addForce(25*x_*cos(earthAngle), 25+x_*sin(earthAngle));
which only works on some parts and works in reverse on the bottom as well as being stronger/weaker on some parts, x_ can be either -1(left) or 1(right). I'm guessing their is a really elegant solution I am just overlooking. Thanks.
Since you already have the vector from the center of the planet to the player eg (x,y), you can use a vector perpendicular to that (-y,x) as the direction for the walking force.

Categories