Drawing on the android screen with a coordinate system - android

My question is probably going to be initially very confusing to read, so just bear with me. I'll start it off with a little preface for context:
Preface:
I have an app that will be using an array for path-finding from a map.
^That is very vague: There will be an array of characters, representing walls, stairs, etc., and there will be a function that finds the best path.
I want to display the path on the android screen.
There will be characters that are generated by the array function that represent the generated path (probably "x" or something).
Okay, to make it more clear: There will be a "path" of 'x's in an array. These 'x's represent the path that is going to show up on the Android screen.
My actual question:
How do I translate an 'x' in the array to displaying a line on the screen? I had the idea to use a for loop/if statement that checks if there is an 'x' and if there is, then to display a little red dot/line in a second array that represents the actual screen.
I was trying to find this, but it's such an awkward thing to type into google, so I finished my research with nothing.
Is there some sort of built-in android function that lets you assign different colours to different coordinates?
This is kind of what I want to appear on the screen. If this were the app, the blue would be represented by 'x's in the first array.

there could be several ways to achieve this effect of having a coordinate system mapping to a matrix that describes a path.
Depending on the size of the array and the frequency of update calls (it sounds like the path finding runs once with a single render after), it probably wouldn't be too expensive to just loop through. What I personally would do is start to look at how to draw on a canvas, get the screen size, and adjust the bounds accordingly.
Get screen dimensions in pixels - How to get screen dimensions
http://danielnadeau.blogspot.com/2012/01/android-canvas-beginners-tutorial.html - A nice tutorial on canvases
Once you can draw to a scaled canvas, it is simply a matter of running a loop that looks something like:
float scale_x = screen_width/columns;
float scale_y = screen_height/rows; //pixels per grid square
for( int x = 0; x < columns; x++)
for( int y = 0; y < rows; y++)
if( data[x][y] == 'x') drawRect(x*scale_x, y*scale_y, scale_x,scale_y) //if something found, draw a colored square

Related

Generating Objects outside of Screen and then Moving them to Center?

Is it possible to generate a series of objects outside of the screen, and then making those objects move inwards? I am creating a live-wall paper with circles that start outside of the screen, and move inwards and bounce off the walls. I have created an illustration to better describe what i mean:
The 2 Issues im facing are:
Generating Objects outside of screen
Making them move inward and then bounce off the edges
How can i achieve this?
One solution to this problem would be to create a class which would contain following attributes:
X and Y coordinated (could be Point)
speedX
speedY
Then you could create objects with coordinates:
(X < 0) or (X > screenWidth)
and/or
(Y < 0) or (Y > screenHeight)
and give them appropriate speed (so they move towards the screen boundaries).
In each step you would:
update each object's coordinates, moving it in appropriate direction corresponding to its current speed
redraw all objects on your canvas
The offset of object's coordinates depends on time step between each two redraws. It's up to you how you want to evaluate it.
Until an object reaches screen boundaries it will be drawn outside the screen and not visible.
To draw the objects on canvas you could extend View class (or SurfaceView - difference between these two is discussed here) and override onDraw() method. You can follow this tutorial or find another one by yourself (there are lots of it).
If an object reaches the screen boundary from its inside (i.e. when its X is in range [0, screenWidth] and its Y is in range [0, screenHeight]) you can negate its speed (in X or Y direction, depending on which boundary has been reached) so it would go in the other direction (like in an elastic collision with a wall).
You can adjust speedX and speedY minimum and maximum values to see which give the most satisfying results.

CCTMXTiledMap rendered in incorrect position - iOS, Android and Win32

I am using CCTMXTiledMap on cocos2dx-2.2, I created and added the tiled map like this:
// TileMap
CCTMXTiledMap *m_pTileMap = CCTMXTiledMap::create("tilesets/my-isometric-small.tmx");
float fPosX = m_pTileMap->getPositionX();
float fPosY = m_pTileMap->getPositionY();
CCLOG( "TileMapPos: %f, %f", fPosX, fPosY );
this->addChild(m_pTileMap);
The tiled map are created and rendered successfully, but out of position. I use CCTMXTiledMap::getPosition, CCTMXLayer::positionAt, and also examine the CCSprite that I get from CCTMXLayer::tileAt... all of them are returning the correct value based on cocos2d screen coordinate { (0, 0) from bottom left and increasing upward and rightward } However, when viewed on the screen, there is always a slight offset and I can't get where it come from. All the m_obOffsetPosition are confirmed to be zero...
By correct value, I mean the tiles are positioned in the pink area (I getPosition from each of the tile, create CCSprite for each, setPosition of each tile and add it to the screen... They show up in the pink area)
Image supposed to be positioned at shady pink boxes, but instead positioned in the blue area (the entire blue sea is the whole map)
Any ideas are highly appreciated... Thanks!!
After wasting days trying to dissect tilemap_parallax_nodes in cocos2d-x, finally I figured out the culprit... it is the Layer Property cc_vertexz that cause it to be rendered off position. I haven't got the time to figure out how and why it works that way and since I'm not going to use it anyway (I just need flat, single layer, thus no need z order etc), so I just remove that property from all of my Layers and the problem is gone..
Hope it helps someone... Thanks!

Random direction without visually rotating shape opengl-es android

I want to be able to assign a shape a random direction to go in at a regular speed.
I tried assigning a random number to the x and y values of a translation but that caused the shape to sometimes move way too fast or just blip off the screen.
Is there a way to choose a random direction for a shape to move in without rotating the shape (at least as far as the user can tell)?
Also is there a way of re-genereating the random number when an event is called (ie: button click) which would allow a change in direction?
EDIT: Just checked. Using the rotate function with a random angle will shoot the square off in a random direction, but still is there a way to do this without altering the orientation of the shape?
Ok, so you want a shape to move in either the postive or negative x, y or z directions?
If you want this to be random then use the Random class. From this you can use Random.nextInt(1) which will either be 0 or 1, if the result is 0 then set the value to be -1.
The +1 or -1 is now used for your direction.
Now you know which way the object travels you can simply multiply this by the speed of your object
Here is some rough code
Random rand = new Random(); int direction = rand.nextInt(1);
if(direction == 0) direction = -1;
Shape theShape.position.x += direction * theShape.speed
This may not be how your interface is setup (such as theShape.position or theShape.speed) but the idea is the same
After reading this again I think you are trying to rotate your shape lol, rotation is not just adding to the x and y values. Do you use matrices in your application? If not your should use trig to figure out what the x and y values will be after a rotation of x degrees

How do I recursively draw a hex map in opengl?

I'm new to GL and wanted to create a tiled map as a self tuorial. I want to create a small (maybe 7 hexes wide / tall) hex map. My first thought was to just create a method to draw one hex and then just translate the appropriate offset and place the new hex. But this doesn't seem effcient. Any Idea's? Alos as a side question, how do I determine if a MotionEvent is with in the are of a given hex?
Extensive hex grid information.
To determine if a MotionEvent is within a certain hex you have to convert the coords passed in via the motion event to your OpenGL World coords. Its just like a unit conversion, you know the screen goes from 0 - WIDTH and your GL world lets say goes from -1 to 1.
(xCoord / (Width - 0)) * (1 - (-1)) = xCoordWorld
will give you the xCoord from 0 to 2, then subtract 1 to get it in -1 to 1.
As far as the hexes go I've always used 'art' hexes. Draw the hex out in paint then render a bunch of squares with that piece of art on them, fast and easy to swap a hex out for another hex.

How to copy pixels from one bitmap to another shaped like a circle?

I am using setPixel and getPixel functions but they are using matrices which is naturally rectangle.I am trying to copy pixels shaped like circle !
update :
now i am using this but i hope there is something more efficient than this one :
for(int i=0;i<eHeight ; i++)
for(int j=0;j<eWidth ; j++)
if( Math.pow((i-eHeight/2),2) + Math.pow((j-eWidth/2),2) < Math.pow((eHeight/2),2))
mutable.setPixel((int)xpos+j, (int)ypos+i, r[i*eWidth + j]) ;
If your circle is fixed, I bet there's a way to use masks to do it really fast -- googling tells me that PorterDuffXfermode is what you need.
Otherwise, you can save some time by doing the computation more efficiently. First, don't use pow to square things. Second, precompute your radius outside the loop. Your compiler in theory will fix all this for you, but don't count on it.
Third, consider using Bresenham's circle algorithm to find the start and end of each row of the circle, and then copy the pixels one row at a time instead of one pixel at a time.
You will need to do some math to figure out if the pixel you are about to copy should be part of the circle or not.
(x - h)^2 + (y - k)^2 = r^2

Categories

Resources