Unity Beginner - 2d game: detect position of tap on quad - android

I have a huge Quad and a PNG as a child object.
The PNG covers the whole game area / screen.
I know how to detect taps, but how can I detect when a user tapped on the left side of the PNG/quad vs. the right?
Should I place 2 gameobjects above the big quad to capture inputs on right or left? Or maybe I could capture the tap on the main big quad but I do a calculation to find out where it as tapped, like this:
if (tappedPosiion > quadWidth / 2)
{
// it's the right side
} else {
// its the left
}
The problem is I don't know how do I find out the width of the current game object!

Renderer.bounds.size should work (or collider.bounds.size)
http://answers.unity3d.com/questions/24012/find-size-of-gameobject.html
You can then access the width by getting the x value:
"size.x is the width, size.y is the height and size.z is the depth of the box."
http://docs.unity3d.com/ScriptReference/Bounds-size.html
Now you have the width, so it should be relatively easy to find out where they tapped.
if(tappedLocation == width/2)
//middle
if(tappedLocation > width/2)
//right side
if(tappedLocation < width/2)
//left
pretty basic but answers your question on how to find width and see where they tapped based on that information.

Related

Warp Image area on touch of a point area?

I need a basic idea for how can i warp image on touch of a particular area. Image filters apply warp on whole image but i want to warp single point, like if i want to warp eye of a person then i will touch on that point. So I need a basic idea about this work.
I have tried this one but its also applies filters on whole image.
https://github.com/Jtfinlay/PhotoWarp
App:
https://play.google.com/store/apps/details?id=hu.tonuzaba.android&hl=en
A warp is not just at a "single point" but over some area that you deform in a smooth way.
To achieve this, you need a geometric transform of the coordinates that works in some neighborhood of the touched point. One way to do this is by applying a square grid on the image and moving the grid nodes around the touched points with some law of yours (for instance, apply a displacement vector to all nodes, with a decaying factor such that far away nodes don't move).
Then you need a resampling function that computes the new coordinates of every pixel and copies the color of the source pixel.
For good results, you must actually work in reverse: scan the destination image and for every pixel retrieve the source coordinates and source pixels. Apply bilinear or bicubic resampling to avoid aliasing.
For ease of implementation, the gridding idea should be adapted as well: rather than deforming the destination grid, keep it unchanged and apply the inverse deformation to the source grid.
Last thing: in the grid approach, see the displacements of the grid nodes as two scalar functions DX(i, j) and DY(i, j) that you can handle separately. From the knowledge of the displacements at the nodes, you can estimate the displacement of any pixel by interpolation (bicubic would be appropriate here).
you can use canvas to detect that portion and stop action on that portion in ontouchlistener
code sample
Bitmap pricetagBmp = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.ic_tag_circle_24dp);
// canvas.drawBitmap(pricetagBmp,left + (right - left) / 2, top + (bottom - top) / 2 - (bounds.height() / 2),circlePaint);
float imageStartX = (left + ((right-left)/2)) - (pricetagBmp.getWidth()/2);
float imageStartY = (top + ((bottom - top) / 2)) - (pricetagBmp.getHeight()/2);
canvas.drawBitmap(pricetagBmp, imageStartX, imageStartY,circlePaint);
and in ontouchlistener if that points detected you can perform no action
Note: you can replace drawBitmap with drawRect or something else with invisible color

Libgdx viewports

It seems like a viewport is useless, but I AM PROBABLY MISSING SOMETHING.
For example I wanted to make a game where I have an object and if I hit the left or the right end of the screen I DIE! So if I used Gdx.graphis.getWidth() and 0 everything would work fine.
OK, but let's setup the viewport = new ExtendViewport(500, 800, camera);
Cool I have not virtual pixels of 500 and 800 === > 5 : 8
Then I type the logic for hitting the end screen, so if object.x < 0 or object.x > 800 ----> object DIES!
But now I start the game on a tablet which has the resolution of 1500 x 2000 ===> 3 : 4, now everything is wrong, the whole screen is moved to the left, so 0 still works but 800 is not the end of the right screen edge.
Then I go use FitViewport and the collision of the left and right edge is now moved, because of a different aspect ratio.
Seems like I can use strechViewport, or just manually code the stuff... But I know I am missing something, because IT MUST HAVE ITS USE.
Would appreciate any help!
First of all in my opinion it is good habit to center the camera at (0, 0) coordinates. Then you will have always -screenWidth/2 as left screen's edge's position and screenWidth/2 for the right side.
Secondly you should not use screen's coordinates directly when using viewport. Viewport has a method to transform screen's to viewport's coordinates:
unproject(Vector2 screenCoords)
it has also backward method
project(Vector2 worldCoords)
These will make you able to keep always same type of coordinates no matter how big display will be.

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.

math.random image re-appearing off screen

Having a small issue with a collision and a math.random sending my images off the screen.
So what I have is a ball hits a brick and the brick will randomly re-locate, but the brick image keeps going off screen. The brick is also rotated 90 degrees so I'm not sure if it effects anything. There is also a 61px tall image at the top of the screen that the image keeps re-locating behind and I was thinking a - 61 added to it would help, but I haven't had any luck.
xRandom = math.random(display.contentWidth - brick.width)
yRandom = math.random(display.contentHeight - brick.height)
transition.to(brick, {delay = 200, x = xRandom, y= yRandom , time=0})
As you have it, the xRandom will be between 1 and display.contentWidth - brick.width: if the anchor point of the brick is its x=0, then this ensures that the brick can't be positioned too far right. Perhaps the x anchor is not at 0, or anchor is x=0 but this does not correspond to left side of image (perhaps the way it was loaded? can't tell without seeing code).
Same goes for y coordinate: is 0 at top of screen and increases downward. So yRandom will be between 1 and display.contentHeight - brick.height which will ensure the brick is visible if its y anchor is at y=0 and that this anchor is the top of the image.
If you have an image that is at top of screen, you should limit the smallest y to be larger than that image's height, ie use yRandom = math.random(topImageHeight, display.contentHeight - brick.height).
It is impossible to tell if part of the problem is due to rotation without seeing your code. If you can post a minimal example that I can run then it will make it a lot easier to provide a more accurate answer.

Gaming, collision on view

I am designing an archer game. When the arrow hits the target it stops moving. What I am trying to accomplish is to define a formula to predict/get location where the target and the arrow meet on the view.
The problem arises when the arrow got a speed varying on initial velocity and the arrow also have angles. Plus, the images on screen are placed by x - bitmapt.getWidth/2 (same for height, h/2..)
If an arrow moves too fast then I need to calculate an error so the arrow does not miss the target even though they are supposed to be on same x and y as we have that, the arrow moves as X + speed pixels. So I came to something like this!
if(arrow[i].getX() + v0x[i] / 2 >= target.getTarget().getX() && arrow[i].getX() <= target.getTarget().getX() + v0x[i] / 2)
I use the velocity of x/2 to specify the error margin.
arrowX + velocity/2 >= collision point >= targetX + velocity/2
However, it does not work.
This is a pure mathematical problem, and there are much literature when it comes to collision detection. I propose that you use a library. For 2D I recommend JBox2D. It is a physics engine (used for example in Angry Birds). You get collision detection and much much more :)

Categories

Resources