Calculating every cartesian point in a moving cicrle - java

I have an array: int[][] lawn = new int[980][1280];
wich stores the values of the height of blades in the virtual “lawn”.
In my simulation i have a robot that goes around the lawn and cuts the blades.
My robot has the form of a circle with a diameter (rDiameter). The coordinate system is done with Double and my lawn is in Integer.
I have to develop an algorithm that puts to 0 all the cells touched by the robot when it moves around.
I have the start and end point of the movement, which are stored in a Line2D.Double form and I want to set on 0 all the cells touched by robot (image).
Any ideas?
(Here my previous question on the same argument every cartesian point in a circle

Break the problem in 3 parts. Part 1 is to set to 0 all points in a semi-circle. Part 2 is to set to 0 all points in a rectangle. Part 3 is to break the path into two semi-circles (at the ends) and a rectangle (joining them).
Note that the semi-circles and the rectangles have, in general, lines that are not axis aligned. There are plenty of references out there on rasterizing polygons and circles. You could look up Jack Bresenham's algorithms. Or you could flip open any classic computer graphics text.

Related

How to find points that lie inside a 3D boundary

I'm currently working on a project in Android that is Java-based. I am using OpenGL-ES 3.0.
In my project, I have a large, complex 3D object (a human head) with 100000+ vertices and 400000+ triangles. The object's vertices are stored in an array. The object can be rotated relative to its centroid.
I am trying to implement a function where the user selects a set of points on the head, and a line is drawn connecting each point (kind of like "connecting the dots"). In other words, the user selects points on the head (p1, p2, p3...pn), and the chosen points will change color. Then, after choosing the last point (pn), an algorithm runs to calculate all the vertices that lie between each point (for example, p1 and p2). Those vertices will then change color, so the user sees a line (or close to a line) between each point they chose.
I have already implemented a way of allowing the user to select the points, and see those points by changing their colors. The difficulty I am having is the implementation of the lines in between the points.
The only idea I currently have to draw these lines is to make use of code I have already programmed. I have code that allows the user to move a ellipsoid (they can choose the dimensions and rotate the ellipsoid). They move the ellipsoid to a location on the head and run an algorithm that calculates all of the points of the head that lie inside the volume of the ellipsoid, and changes those points' colors.
My idea was to take the midpoint of p1 and p2, set that as the center of the ellipsoid, and rotate/stretch the ellipsoid dimensions so that one axis runs from p1 to p2 and the other one is some large value. Basically, it would look like taking a flattened ball-like shape (something like a red blood cell) and putting one end of the ball on p1, and the opposite end on p2. I could then run the algorithm that I already coded to find all the points inside that ellipsoid. Then I can change the color of these points and the user can see a line between each point they selected.
Does anyone have any criticisms of this technique? Are there other ways I could possibly achieve the result I desire?
To check if a point touches or is inside an ellipsoid:
Translate the world by -x, -y, -z (x, y and z being the ellipse's
center) such that (0, 0, 0) becomes the ellipsoid's new center.
Rotate the world so that the ellipsoid makes 0 degrees.
Scale the world by 1/a, 1/b, and 1/c (a, b and c being the length 3
elliptical axes).
if sqroot(a^2 + b^2 + c^2) <= 1, then the point lies
inside the sphere.

libgdx 3d infinite scrolling floor

I'm making a 3D game, and looking at the best method to have a floor plane as part of the 3D. If you imagine those games like TempleRun, but no turns, where you are moving forwards and the floor is scrolling backwards (towards the user) infinitely.
I already have my 3D object moving around X axis suitably (on a fixed Z plane), but now looking at adding a background, floor, etc.
I've been reading on Decal and Plane but uncertain as to which would be the best approach. I'd have the same floor structure on each level, but with different textures. Does one offer more expandability in the future? Eg, slopes, hills, etc. Each floor structure (whether Decal or Plane) would be repeating endlessly, and I'd see if I can create a fog effect which makes the floor fade with distance. Also it would have a tileable texture (eg grass, tarmac, dirt) which would be repeating backwards.
Perhaps even a static structure, and simply move the texture backwards?
Any thoughts would be great, thanks.
I would say don't go with decals, as these don't react to libgdx's environments (lights, shadow, an so on). And a Plane is just some class that represents plane and can't be rendered at all.
Simples solution: go to blender or any other 3d editing tool of your choice create a plane and apply your tillable texture to it.
Export it as .fbx and convert it to .g3db (libGDX's format).
Load it into your game and and make a couple of copies of it (ModelInstances).
Place those on the floor one after another so that no gap appears and they all form a nice repeating ground.
As your character moves forward check its position on X axis (or whatever axis you want to move on) and if the position is greater than position of your last plane on the floor - some distance than update floor as follows:
Remove the first plane from ground and add it after the last plane that you have.
(Render everything with ModelBatch)
If you have enough of these planes the player wont notice them appearing in the distance. Also use fog to completely hide this transitioning of planes.
The same can be applied not only to planes but repeating hill or any other tillable 3d object.
You could have some Planes made with meshpartbuilder (Like explained here) or even a Model made with an Application like Blender.
As you can create many ModelInstances out of one Model and give them different Materials, you can use the same Model over and over again. So you could create a 4x4 meter (for example) big Model (it will only be 1 face) on the XZ-Plane. Next you create, lets say 10, ModelInstances out of it and add them to each other:
first Plane: x = 0-4m, y = 0-0, z = 0-4m
second Plane: x= 4-8m, y = 0-0, z = 0-4m
And so on. As soon as your character is at a X Position > 4, you can translate the ModelInstance firstPlane foreward to the end of the 10th plane. So its new x would be 44-48 meters.
As soon as you reach a new level or another ground or whatever you can simply change the Material of the ModelInstance.
For the background i would suggest to read something about SkyBoxes.
Hope i could help

Pacman collision detection with varying character speeds?

I am programming a 2D, grid-based Pacman game. All the tiles are 8x8 in size. In-game, the map tiles are treated as 16x16, and the characters (Pacman and the ghosts) are treated as 32x32. In actuality, they are all pulled from a spritesheet of 8x8 tiles. I store positions as the center point of each character. Since the character tiles are bigger than the map tiles, the map is built in a way that requires the characters being able to "overlap" onto blocked tiles.
To deal with this set of problems, I created an invisible Rectangle and attached it to the character's position. Where the position is an (x,y) point, the Rectangle is a box surrounding that point. This rectangle is essentially 16x16 in-game, and is in the center of the character, which allows for the overlap necessary.
This works fine if you're working with 8px as the global movement speed, but I'd like to treat 8px as "100% speed" and have complete control over character speed with a double that is in the range [0,1). The positions are stored as double points, so on that level, this is fine. I read the positions back as integers, though, since I'm working with pixels.
So the question I ask is essentially "if this moves X amount of pixels to direction Y now, will my collision box be touching a blocked tile? But if you're moving 5px at a time, this eventually causes a very obvious issue. Say you're at x = 0, moving right. The tiles are 16x16 in-game, as stated before, and you have two of these open before the third, which is blocked. So you move, x = 5, x = 10, x = 15, x = 20, we just got to the 2nd tile, x = 25, x = 30, x = 35 now we're in the 3rd tile... but wait. We can't go there, because X = 35 collides. And unfortunately, we needed to turn and start moving down, but we can't, because now our Y-axis isn't aligned properly with the grid. Our X position needs to be 32, but can't.
My question for everyone here is, what are my options? What are some ideas or insights you have? I have a feeling I'm making it more difficult than I need to.
sounds like you have...
Why not give your "pac-man" sprite a velocity vector? The vector will describe not only the speed at which "pac-man" is traveling but in what direction, meaning you can see ahead.
"pac-man" should be calculating and ultimately making a decision based upon the following conversation..."hey, moving at this speed and in this direction..in so many seconds I'm going to hit a wall, when does that happen?". The seconds don't even have to be seconds...they could be "squares".
You would need a function which takes in the initial movement vector (direction and speed) which returns a coordinate of an X,Y point where "pac-man" must stop, where he cannot go further and must change direction (the center of a tile adjacent to a wall). Each time "pac-man" changes direction, run this calculation again...you do not need to keep checking if the next square is passable. If his direction hasn't changed and his speed is constant..you only need calculate once and let the coordinate system do the rest.
With this approach, square size and velocity is irrelevant...until "pac-man" hits or within his next movement exceeds the stopping point, continue to move along the vector.

How to rotate a single shape in 3d space in opengl?

so I'm pretty new with opengl and creating 3d shapes. So for my example I have two squares, one with a height/width 2 with the center at the origin coordinate (0,0,-10), and one that is to the far left side of the window. I am trying to rotate the square that lies in the origin along the x-z plane without rotating the square that is located to the far left side of the screen. My approach to this was to save each xyz coordinate of the center square to a variable, and creating a method that uses the behavior of cos(theta) to rotate the square along the x-z plane. My code works, but I assume this is a horrible approach as there must be some more efficient method that is already created that can do the same functionality. I looked at glRotatef(), but from what I understood this only rotates my camera view which in the end would rotate both the middle square and the far left square whereas I only want to rotate the middle square. Is there some other method that already exists that can easily rotate a single 2d shape in 3d space?
In case its relevant, I have included the rotating code I made myself for the middle square: (btw the blue class is just some class I made that has the squares coordinates and the circle degree for cos(theta))
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) {
blue.setCircle(blue.getCircle()+1f);//getCircle is initially zero and gets incremented by 1 for everytime the program loops with the user holding the left button.
blue.setXfrontTR((float)Math.cos(Math.toRadians(blue.getCircle())));//Changing top-right x coordinate of the middle square
blue.setZfrontTR(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+270f)))); //Changing top-right z coordinate of the middle square.
blue.setXfrontTL((float)Math.cos(Math.toRadians(blue.getCircle()+180f)));
blue.setZfrontTL(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+90f))));//Changing top-left x,z coordinates
blue.setXfrontBL((float)Math.cos(Math.toRadians(blue.getCircle()+180f)));
blue.setZfrontBL(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+90f))));//Changing bottom-left x,z coordinates
blue.setXfrontBR((float)Math.cos(Math.toRadians(blue.getCircle())));
blue.setZfrontBR(-10f+ ((float)Math.cos(Math.toRadians(blue.getCircle()+270f))));//Changing bottom-right x-z coordinates
}
If you give each object that requires independent movement a model-view matrix you can achieve this. The other option to quickly draw/move a few independent objects is to:
for each object:
pushMatrix()
draw object
popMatrix()
while in the modelview matrix...
The method of drawing depends greatly on the OpenGL version you're coding to but the above will work for simple drawing. I'm not an expert on OpenGL / 3D programming so, if you wait a bit you may hear(see) better wisdom than what I offer :)

Java - filling an overlapping polygon

I'm trying to draw a 5 point star in AWT.
Each point in the 2d grid is 72 degrees apart - so I thought I could draw the polygon using only 5 points by ordering the points 144 degrees apart, so the polygon gets fed the points in order 1,3,5,2,4
Unfortunately, this involves a lot of intersecting lines, and the end result is that there are 5 triangles with my desired colour, circling a pentagon that has not been coloured.
Looking through, it has something to do with an even-odd rule, that intersecting points will not be filled.
I need my star to be drawn dynamically, and using the specific shape described (for scaling and such).
If I manually plot the points where it intersects, I get some human error in my star's shape.
Is there any way to just turn this feature off, or failing that, is there a way to get the polygon to return an array of x[] and y[] where lines intersect so I can just draw another one inside it?
Thanks.
Draw it with ten points, 36 degrees apart, at two alternating radii.
Establish the 10-point Polygon in cartesian coordinates, as suggested by relet and as shown in this example. Note how the coordinate system is centered on the origin for convenience in rotating, scaling and translating. Because Polygon implements the Shape interface, the createTransformedShape() method of AffineTransform may be applied. A more advanced shape library may be found here.
Is there a way to get the polygon to return an array of x[] and y[] where lines intersect?
Although typically unnecessary, you can examine the component coordinates using a Shape's PathIterator. I found it instructive to examine the coordinates before and after invoking createTransformedShape().

Categories

Resources