just trying to load a layer from my tilemap in for collision detection, but having problems. Apparently it tells me my layer is out of range.
private void getTiles(int startX, int startY, int endX, int endY, Array<Rectangle> tiles) {
TiledMapTileLayer layer = (TiledMapTileLayer)map.getLayers().get("layer2");
rectPool.freeAll(tiles);
tiles.clear();
for(int y = startY; y <= endY; y++) {
for(int x = startX; x <= endX; x++) {
Cell cell = layer.getCell(x, y);
if(cell != null) {
Rectangle rect = rectPool.obtain();
rect.set(x, y, 1, 1);
tiles.add(rect);
}
}
}
}
I've also tried putting just the integer in as well, same result, only one that works is 0. My .tmx map in Tiled has two layers, "layer1" and "layer2". The map displays fine, so I know I've loaded the .tmx correctly (used the TmxLoader to create a TiledMap called "map"). Not sure what's going on.
Related
I am trying to make a program where there are lines in a grid pointing towards the mouse like magnets. I am a beginner in Processing, can someone point me towards a tutorial on how to do that or give me some code and explain what it does?
int x1 = 0;
int x2 = 0;
int y1 = 0;
int y2 = 0;
void setup() {
size(200, 200);
}
void draw() {
background(255, 255, 0);
x1 = (mouseX + 100) / 2;
y1 = (mouseY + 100) / 2;
x2 = -1 * x1 + 200;
y2 = -1 * y1 + 200;
line(x1, y1, x2, y2);
}
There's plenty of solutions for this project. One of the easiest is to use Processing's PVector class.
The PVector class can be used for two or three dimensional vectors. A vector is an entity that has both magnitude and direction. The PVector class, however, stores the components of the vector (x,y for 2D, and x,y,z for 3D). The magnitude and direction are calculated from the components and can be accessed via the methods mag() and heading().
A two dimensional vector in Processing is defined through x and y components:
PVector v = new PVector(xComponent, yComponent);
With some mathematical formulae, you can determine magnitude and direction using the x- and y-components. But we don't need to determine these.
Below, I've attached completed solution code. Most of it should make sense to you. But it's worth understanding what is going on with PVector.
A nested for loop within void draw() contains x and y variables that represent the coordinates of each grid vertex.
We first define PVector v as a vector given by an x-component of mouseX - x, or the difference between the x-positions of the mouse and each grid point. Similarly, the y-component given by mouseY - y has the same difference.
Creating a variable PVector u initialized from v.setMag(15) holds a PVector that has the same direction as v, but with a length of just 15.
Now to draw the lines. Vectors represent an offset, not a position (in this case), so drawing a line from a grid point to an offset of a grid point is key.
Hence line(x, y, x + u.x, y + u.y), where u.x and u.y are the x- and y-components of the vector u.
void setup() {
size(600, 600); // Set the size of the canvas to 600x600.
}
void draw() {
background(255);
stroke(200); // Set the stroke color to black
int distVertLine = width / 10; // This variable defines the distance between each subsequent vertical line.
for(int i = 0; i < width; i += distVertLine) {
line(i, 0, i, height); // Draw a line at x=i starting at the top of the canvas (y=0) and going to the bottom (y=height)
}
int distHorizLine = height / 10; // This variable defines the distance between each subsequent vertical line.
for(int i = 0; i < width; i += distHorizLine) {
line(0, i, width, i); // Draw a line at y=i starting at the left of the canvas (x=0) and going to the right (x=width)
}
stroke(0); // Set the stroke to black.
// Use a nested for loop to iterate through all grid vertices.
for(int x = 0; x <= width; x += width/10) {
for(int y = 0; y <= height; y += height/10) {
PVector v = new PVector(mouseX - x, mouseY - y); // Define a vector that points in the direction of the mouse from each grid point.
PVector u = v.setMag(15); // Make the vector have a length of 15 units.
line(x, y, x + u.x, y + u.y); // Draw a line from the grid vertex to the terminal point given by the vector.
}
}
}
The answer already given by Ben Myers is excellent! The code below has a few small modifications:
the two for loops for the grid lines have been combined (since width and height are equal);
the construction of the vector is combined with setting the magnitude;
some minor changes to colors and comments.
Modified code:
void setup() {
// Set the size of the canvas to 600x600 pixels.
size(600, 600);
}
void draw() {
// There are 10x10 grid cells that each have a size of 60x60 pixels.
int gridSize = width / 10;
// Set the background color to anthracite and the stroke color to orange.
background(56, 62, 66);
stroke(235, 113, 52);
// Draw vertical and horizontal grid lines.
for (int lineIndex = 0; lineIndex < gridSize; lineIndex++) {
line(lineIndex * gridSize, 0, lineIndex * gridSize, height);
line(0, lineIndex * gridSize, width, lineIndex * gridSize);
}
// Set the stroke color to blue.
stroke(0, 139, 225);
// Use a nested for loop to iterate through all grid cells.
for (int x = 0; x <= width; x += gridSize) {
for (int y = 0; y <= height; y += gridSize) {
// Define a vector that points in the direction of the mouse from
// each grid point and set the vector length to 15 units.
PVector vector = new PVector(mouseX - x, mouseY - y).setMag(15);
// Draw a line from the grid point to the end point using the vector.
line(x, y, x + vector.x, y + vector.y);
}
}
}
I have a Texture that needed to be erase on some point of game. I think its best to clarify my question with picture.
Below is the actual texture.
It is place inside an actor and being call inside the draw after adding to stage.
Below is the partial code of the texture
#Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
batch.draw(trLine,position.x,position.y);
}
I wanted to erase some part of the texture using a Rectangle with x,y,width And height.And also its can be more than one time. How do I achieve this?
Added: I have found a article that use pixmap to load the texture and modifying it using mask. below is the code, but unfortunately , it draw nothing.
Pixmap mask = new Pixmap(trLine.getRegionWidth(), trLine.getRegionWidth(), Pixmap.Format.Alpha);
mask.setBlending(Pixmap.Blending.None);
mask.setColor(0,0,0,0);
mask.fillRectangle((int)rec.x,(int)rec.y,(int)rec.width,(int)rec.height);
Pixmap fg = new Pixmap(trLine.getRegionWidth(), trLine.getRegionWidth(), Pixmap.Format.RGBA8888);
for (int x = 0; x < trLine.getRegionWidth(); x++) {
for (int y = 0; y < trLine.getRegionHeight(); y++) {
int colorInt = fg.getPixel(trLine.getRegionX() + x, trLine.getRegionY() + y);
fg.drawPixel(colorInt,trLine.getRegionX() + x , trLine.getRegionY() + y);
}
}
fg.drawPixmap(mask, fg.getWidth(), fg.getHeight());
mask.setBlending(Pixmap.Blending.SourceOver);
trBlendedLine=new Texture(fg);
So I am working on a game and I need to get connected textures working.
The maps are randomly generated so all I have is a byte array of what tile should be where.
And I have an idea that if I split each tile into 4 8x8 tiles and combine them into a single 16x16 tile so I can connected textures with minimal work.
So my main question is how can I take a texture sprite sheet with 8x8 tiles then take for tiles and arrange them into a single 16x16 tile for use with a StaticTiledMapTile.
MapLayers layers = map.getLayers();
TiledMapTileLayer baselayer = new TiledMapTileLayer(128, 128, 16, 16);
for (int y = 0; y < LevelHight; y++) {
for (int x = 0; x < LevelWidth; x++) {
Cell cell = new Cell();
Tile tile = getTile(x, y);
cell.setTile(new StaticTiledMapTile(atlas.findRegion(tile.atlasName)));
baselayer.setCell(x, y, cell);
}
}
public Tile getTile(int x, int y) {
if (x < 0 || y < 0 || x >= 128 || y >= 128)
return Tile.rock;
return Tile.tiles[tiles[x + y * 128]];
}
Let me know if you need any more info.
I am working on a game in Android and I have Bitmap objects being drawn. They are rotated using the Matrix class. The problem I am having is being able to access the pixels of the rotated Bitmap. When I access them after rotation the pixels are still representing the unrotated version. Does anyone have any ideas? Maybe I can rotate the pixel array based on an arbitrary number of degrees? I have seen solutions that create a new bitmaps on the fly based on the newly rotated images, but I can't do this because of performance issues. Thanks!
Here is how I draw the bitmap currently (The bitmap is drawn using the matrix as its position)...
canvas.drawBitmap(bitmapPlayer, positionMatrix, null);
Here is how I create the positionMatrix variable...
Matrix m = new Matrix();
m.postRotate(degrees, bitmapCenterX, bitmapCenterY);
positionMatrix.set(m);
Here is how I access the pixels currently (This accesses the pixel array I created below)...
getPixel(x, y);
Here is how I build the pixel array that I have tried to modify...
// Build pixel 2d array
pixelData = new int[bitmapPlayer.getWidth()][bitmapPlayer.getHeight()];
for(int x=0; x<bitmapPlayer.getWidth(); x++) {
for(int y=0; y<bitmapPlayer.getHeight(); y++) {
pixelData[x][y] = bitmapPlayer.getPixel(x, y);
}
}
Here is how I have tried to manipulate the array...
public void rotatePixels(int degrees) {
int[][] newPixelData = new int[bitmapPlayer.getWidth()][bitmapPlayer.getHeight()];
double cos = Math.cos(Math.toRadians(degrees));
double sin = Math.sin(Math.toRadians(degrees));
for(int x=0; x<bitmapPlayer.getWidth(); x++) {
for(int y=0; y<bitmapPlayer.getHeight(); y++) {
if(pixelData[x][y] != 0) {
int currentX = x + getTopLeftX();
int currentY = y + getTopLeftY();
int nextX = (int)((currentX * cos) - (currentY * sin));
int nextY = (int)((currentX * sin) + (currentY * cos));
if(nextX >= 0 && nextX < bitmapPlayer.getWidth() && nextY >= 0 && nextY < bitmapPlayer.getHeight()) {
newPixelData[nextX][nextY] = 1;
}
}
}
}
this.pixelData = newPixelData;
}
Have you tried:
Matrix mat = new Matrix();
mat.postRotate(45);
Bitmap newBm = Bitmap.createBitmap(bitmapPlayer, 0, 0, bitmapPlayer.width(), bitmapPlayer.height(), mat, true);
and then accessing the pixels of the new Bitmap?
I think right now you just draw a rotated bitmap on the canvas, you're not actually rotating the bitmap
EDIT:
The way you do it in your orinigal post won't work because you start at (0,0) and work your way down the left column... Depending on the rotation degree you'd have to start at a different place and then go down a different column. i.e. a small ctrclkwise rotation and you start at the top right index.
Bitmap bm = someImage;
int startx, starty;
int degree = rotateDegree % 360; // counterclockwise
if (degree >= 0 && degree < 90) {
startx = bm.getWidth();
starty = 0;
} else if (degree >= 90 && degree < 180) {
startx = bm.getWidth();
starty = bm.getWidth();
} else if (degree >= 180 && degree < 270) {
startx = 0();
starty = bm.getWidth();
} else {
startx = 0;
starty = 0;
}
And then try traversing the image that way, starting at (startx, starty)
and adding or subtracting the correct angle (you can use some boolean to keep track
of whether you're adding or subtracting from x or y respectively). Let me know if this works,
and if it doesn't can you be a little more specific about where you think the problem might be?
I'm making my first game using Java on Android. I need to draw a lot of pixels which together should create a line. My first approach was to make a large array of booleans, create a loop, and draw a pixel when the associated boolean was true.
It wasn't a good idea of course (the array is about 200x300). Now I remember only the position of the first pixel of the line, and every next pixel has to remember his follower. It works pretty well, but when the line gets longer (but still not very long), the efficiency is bad (<20 fps after 4000 frames).
This is the function that I use to draw a line (only one for now). Can anybody help me improve its efficiency?
public void drawLine(Canvas canvas, int beginx, int beginy) {
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
int x = beginx;
int y = beginy;
while(C.mGrid[x][y].nx != -1) {
//canvas.drawLine(x, y, C.mGrid[x][y].nx, C.mGrid[x][y].ny, paint);
canvas.drawPoint(x, y, paint);
Grid temp = C.mGrid[x][y];
if ((C.mGrid[x][y].nx == x) && (C.mGrid[x][y].ny == y)) break;
x = temp.nx;
y = temp.ny;
}
}
and Grid.java:
package com.qwak.achtung;
public float x = 0,y = 0;
public int px = -1, py = -1, nx = -1, ny = -1;
public Grid(float x, float y) {
this.x = x;
this.y = y;
}
public void set(int px, int py, int nx, int ny) {
this.px = px;
this.py = py;
this.nx = nx;
this.ny = ny;
}
public void setp(int px, int py) {
this.px = px;
this.py = py;
}
public void setn(int nx, int ny) {
this.nx = nx;
this.ny = ny;
}
PS: It looks like this http://c.wrzuta.pl/wi10559/11f7d10b00110e504e25ebd3/0/andek 14 is fps (on my phone (samsung Spica) it run better - 40 but after a while it decreases to 20 and even less) and 983 is number of frames at all.
There is a drawLine method in the canvas object.
Use the example here: How to draw a line in android
canvas.drawLine(0, 0, 20, 20, paint);
If you want to draw a curve. Find the function of the curve. A Parabola for example is x=y^2. You can get points from the curve: 1 = 1, 2 = 4, 3 = 9, 4 = 16... etc.. If your drawing pixel by pixel you can plug in your x and get your y and draw it.
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
paint.setStrokeWidth(3);
for(int i = beginx; i < CanvasWidth; i++)
{
int x = i;
int y = i * i; //x=y^2
canvas.drawPoint(x, y, paint);
}
To keep a record of points that were visited you could do the following:
class Point
{
int x;
int y;
}
List<Point> points = new List<Point>();
onMove(int newX, int newY)
{
Point p = new Point();
p.x = newX;
p.y = newY;
points.add(p);
}
onDraw()
{
for(Point p : points)
{
canvas.drawPoint(p.x, p.y, paint);
}
}
You want to look into the bresenham algorithm. A bresenham algorithm is a method to draw or rasterize a line. It's a bit different from the subdivision of a grid in a certain angle for example a morton-curve. It's a bit like compute the scalar product for every angle like recall here Traversing a 2D array in an angle.