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.
Related
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.
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.
Ohh.. damm Math !! once again stuck. It seems to be easy but i think its not that easy,
Problem Statement: I want to rotate the 3 fixed points which lies on a fixed circle.
1.when 1 point is selected remaining 2 points should be static mode and only selected point should move/rotate on circumference of circle.
2.And all 3 points are connected via 3 lines as shown in images..when we select a point and rotate it,connected lines also increase and decrease..
I already tried to solve this problem finding angle at each instant after touch.but its not quite working as per my need..
something like this
I hope the following explanation enable you to put the steps into your coding language.
Presumption is that the vertex to be moved has already selected and so the calculation of (xcnd,ycnd) as defined below is used to set the selected vertex of the triangle.
Let the constraining circle have centre at (cx,cy) and radius r.
Let the coordinates of where the screen is touched be (xtch,ytch)
Let the coordinates of where the screen is touched relative to the centre be (xrel,yrel)
then xrel = xtch - cx and yrel = ytch - cy
Let the coordinates of the point on the constraining circle when the screen is touched at (xtch,ytch) be (xcnd,ycnd).
xcndrel = xcnd - cx, and ycndrel = ycnd - cy give the coordinates on the constraining circle relative to its centre,
Note that
xrel and xcndrel will have the same signs (ie both positive or both negative)
and yrel and ycndrel will also have the same signs.
the function abs(x) = x if x>=0 and -x if x<0 should be available in whatever language you are using
the function sign(x) may or may not be available, sign(x) =1 if x>0 and -1 if x<0 and undefined for x=0
If not available then sign(x)=x/abs(x)
Check if xrel=0
if xrel=0 xcndrel=0, ycndrel=r*sign(yrel)
Otherwise work in first quadrant ie where x>0 and y>0 using abs(xrel) and abs(yrel)
find angle where screen is touched relative to centre of circle using
theta=arctan(abs(yrel)/abs(xrel))
find the coordinates (xcndrel, ycndrel) by using theta in the first quadrant and then placing in the correct quadrant using the signs of xrel and yrel
xcndrel = sign(xrel)*r*COS(theta)
ycndrel = sign(yrel)*r*SIN(theta)
Screen coordinates can now be found
xcnd = xcndrel +cx
ycnd = ycndrel + cy
Assuming that i have an android phone with max x=400 and max y=300
I have a 5000 by 5000 px bitmap and i display a 400 by 300 part of it on screen. I scroll it around using touch events. I want to draw a smaller bitmap(30 by 30px) onto that larger bitmap at location (460,370). But i cant statically do it(as my game requires). I want to draw and undraw that smaller image according to the player's input or collision detections.
What i am confused about is that suppose currently (50,50) to (450,350) of the larger image is displayed on the screen and now ,due to a certain requirement of the game at this moment, i have to draw that 30by30 bitmap at (460,370) but this point is in two systems - 1)In the big bitmap's co-ordinate system it's 460,370 but when it will come inside the visible area of screen it will have values something between (0,0) and (400,300) depending upon its position on the screen as per the player's movements...
So how do i track that image- i mean using which co-ordinate system, and how do i go about doing all this?
I know I've put it here in a very confusing way but that's the best way i can put it and i'll be really glad, and grateful to you if somebody could help me on this.....
Terminology:
Let's call the coordinates on the big map world coordinates
Let's call the coordinates on the screen view coordinates. You are talking to the drawing system (canvas?) in view coordinates.
Your currently displayed region of the bigger map is 100% defined by an offset, let's call this (offsetX, offsetY). Formally, this offset maps your world coordinates onto your view coordinates.
If you have the small image displayed at position (x, y) in world coordinates, all you need to do is subtract your offset to get to view coordinates, i.e.
viewX = worldX - offsetX;
viewY = worldY - offsetY;
I am trying to learn opengl stuff on Android. In the gl.gltranslatef(x,y,z) call, I am shifting my texture by some units in the +ve x direction. But I am unable to find the number of pixels does 1 unit of x belong to?
Here is what I am doing:
I call gl.glviewport(0,0,width,height); // This will set my rectangle with 0,0 as lowerleft corner and then extend it to accommodate width and height.
Then
I call to gl.glfrustrum(-5,5,-7,7,3,7); // I am little confused how this call is using the dimensions I set in gl.glviewport.
How will -5 to 5 units from left to right in the above call, translate to pixels on the screen of android?
I mean if width = 320 and height = 533 pixels, then what will be the number of pixels occupied on the screen due to the gl.glfrustrum call?
I am experimenting in the gl.gltranslatef call by specifying xshift as 5.0, but it does not translate the bitmap at the right or left corner of the screen, when I increase it to 6, part of it is still visible on the screen.
Thanks
Siddhesh
In short, I am searching for the maximum number of units (in terms of X) which will represent extreme corners of my android phone screen.
glViewpoint tells it what rectangle (in pixels) your OpenGL output should be displayed in.
glFrustum tells it what coordinates in your "world" units should be mapped to that viewport.
An important point: your glFrustum call includes not only a height and width, but also a depth. Since you are specifying a Frustum, not a cube, that means anything with a Z coordinate anywhere but the very front of your frustum will be scaled down appropriately for its distance from the viewer.
As such, when you to a glTranslatef, the distance by which a particular object will move (in terms of pixels) will depend on its distance from the viewer. The further away it is from the viewer, the fewer pixels a particular sideways or up/down will translate to.
Depending on what else you're doing, one easy way to deal with this might be to use glOrtho instead of glFrustum. glOrtho gives orthographic mode, which means no perspective scaling is done, so a given X or Y distance will translate to the same number of pixels, regardless of distance from the viewer.