XNA Tile Based Collision - xna

I have a tile map of 64 x 64 pixel tiles (10 rows, 10 columns) I want to detect collision between the moving units and any walls or objects. How can i keep the units centered in the tile map while moving then detect if a unit should change direction without updating his position to soon, and throwing him off the center of the tile?
Example: If there is an object at TilePositionX = 3, and TilePosition = 0 and that object makes a unit change his direction but always stay centered. If the unit is heading right towards the object at a XVelocity = 1.0f (this could be any velocity) every update. Do i have to detect the center position of the unit then add an offset and check if I'm completely inside a tile? I can't of a good solution for my problem.

What I did in a similar situation was work out the target destination of the unit (in pixels) which was the centre of the tile it was moving toward, and then every update move the unit closer to that target. Only once the unit was in the centre of the tile did it check for a new destination.
For example: Unit A is at coordinates (0,0) which in pixels (32,32) (the centre of that tile)
A moves toward tile (0,1), so new destination is (32,96). Every update the unit moves (0,1) pixels down. Once the unit is a (32,96) which is the centre of the tile (0,1), it can then decide where it is going to move next.

The way i handled this in a few of my games was i held a Tile and a Pixel offset, the Tile always stays a solid number I.E 1,2 and the pixel offset was moved negative positive based on direction. When it got above the size of a tile we would reset it to zero and add or subtract to the corrosponding Tile type. From there it becomes fairly easy to determine if you are hitting a tile that you cannot move on.

Related

Isometric scene - how?

I programmed a simple 2d app/ game.I just noticed, that an isometric scene instead of the 2d one would look gorgeous . I did not use any SpriteKit, etc. , it's just a simple single-view-app.
Now I draw some nice isometric vectors of e.g. a petrol station, which I would love to use instead of the plain 2D-images. For sure I can just use them as an imageview. But my idea was that I may animate driving cars in straight ways, so that they seem to be 3D (isometric), but are just images moving along a given path. What is the best way to do this? Can I use my isometric image as a Gamescene (never used)?
Greetings!
If you’ve been doing without SpriteKit so far, I don’t see any need to use it now. Keep them as UIImageViews, and animate their positions normally, just make sure that the point you move them to makes sense, so as not to break the illusion.
You can open up your image in Preview, click and drag along it’s direction to make a rectangular selection along that direction, and use the width and height of that box to make a ratio, like 20:25, which then simplifies to 4:5. Meaning for every 4 points it moves along x-axis, it should move 5 points along y-axis. Then store this ratio as a CGPoint somewhere in your code, note that all your isometric images should give you this same ratio.
Then you could make an abstraction that moves an imageview x points along its direction, using that ratio. Say you want to move it 100 points along its direction, and say the ratio is 4:5. The ratio is a triangle of width 4, height 5. You use a^2=b^2+c^2 to calculate the hypotenuse of that triangle. Then find a k such that ck=100. Then multiply a and b by that k to give you your delta x and delta y. Apply those deltas to its current position and you have the final position to animate to, which will be 100 points along its direction.

how to find orientation of a picture with delphi

I need to find orientation of corn pictures (as examples below) they have different angles to right or left. I need to turn them upside (90 degree angle with their normal) (when they look like a water drop)
Is there any way I can do it easily?
As starting point - find image moments (and Hu moments for complex forms like pear). From the link:
Information about image orientation can be derived by first using the
second order central moments to construct a covariance matrix.
I suspect that usage of some image processing library like OpenCV could give more reliable results in common case
From the OP I got the impression you a rookie in this so I stick to something simple:
compute bounding box of image
simple enough go through all pixels and remember min,max of x,y coordinates of non background pixels
compute critical dimensions
Just cast few lines through the bounding box computing the red points positions. So select the start points I choose 25%,50%,75% of height. First start from left and stop on first non background pixel. Then start from right and stop on first non background pixel.
axis aligned position
start rotating the image with some step remember/stop on position where the red dots are symmetric so they are almost the same distance from left and from right. Also the bounding box has maximal height and minimal width in axis aligned position so you can also exploit that instead ...
determine the position
You got 4 options if I call the distance l0,l1,l2,r0,r1,r2
l means from left, r means from right
0 is upper (bluish) line, 1 middle, 2 bottom
then you wanted position is if (l0==r0)>=(l1==r1)>=(l2==r2) and bounding box is bigger in y axis then in x axis so rotate by 90 degrees until match is found or determine the orientation directly from distances and rotate just once ...
[Notes]
You will need accessing pixels of image so I strongly recommend to use Graphics::TBitmap from VCL. Look here gfx in C specially the section GDI Bitmap and also at this finding horizon on high altitude photo might help a bit.
I use C++ and VCL so you have to translate to Pascal but the VCL stuff is the same...

Why does DirectX use a flipped Y axis?

I am saving my driven X/Y coordinates, and then using a function that convert the coordinates to meters, and add 1280 to each point (so it will fit nicely into a 2560x2560 image), and then draw a polygon between the 'points', resulting in a some sort of racing line. But once I have generated the polygon and saved it as an image, it is vertically flipped somehow. Flipping the image vertically will make it match the track bitmaps perfectly. I was told this is due to DirectX internally has the Y axis flipped. Why does DirectX use a flipped Y axis?
Well, the question is, does DirectX have a flipped Y-axis or does the image?
DirectX uses a 3D/4D coordinate system where the X-axis points to the right and Y-axis points upwards when no transformation is applied. This is because the screen (where Y-axis points downwards) is the last instance that has to process the image. Every step before that uses the coordinate system with the upward Y-axis. Since Direct3D is designed for 3D worlds, a coordinate system that is aligned like the world and like most coordinate system in maths is much more convenient for the programmer and designer. Imagine, you would create a 3D model. Wouldn't it be kind of weird, if you design it so that the Y-axis is pointing downwards?
When you have no transformation at all that would allow perspective and so on, you have the same coordinate system. Ignoring the Z-axis, the top left corner is (-1 | 1), the bottom right corner is (1, -1). This is equal to the coordinate systems used in e.g. maths. In the end, this coordinate system is transformed with the viewport which will result in the top left corner to be (0 | 0) and the bottom right corner to be (ResolutionX | ResolutionY).
So all in all, the reason why the Y-axis points upwards is that Direct3D's main purpose is to describe worlds in a convenient way independently of the screen's physical attributes.

drawing overlapping circles in corona

How to draw a circle overlapping another circle in the moved phase of touch event,such that no gap is left out between the circles.The circles must be tightly packed to one another,so that even when user moves his hand on the screen faster or lightly,no gap must be present between the circles.
Just two circles? Or many circles? If just two, then detecting if they overlap is simply verifying that their centers are not closer than the sum of their radii. For example, if Circle1's raduis is 10 pixels, and Circle2's radius is 25 pixels, then they overlap if the center of Circle1 is less than 35 pixels from the center of Circle2.
So if you do your calculations in the "moved" phase and find that they're too close, you have to adjust the position of one of them. How you go about that will depend on the specifics of your application. You could:
Keep the y coordinate of the moving circle the same, and calculate the necessary x coordinate to maintain the required distance.
Same as above but swap x and y.
As above, but move the "unmoving" circle away from the "moving" circle.
Some other calculation that makes sense for your application.
NOTE: You should accept some of the answers you've been given.

What is this rotation behavior in XNA?

I am just starting out in XNA and have a question about rotation. When you multiply a vector by a rotation matrix in XNA, it goes counter-clockwise. This I understand.
However, let me give you an example of what I don't get. Let's say I load a random art asset into the pipeline. I then create some variable to increment every frame by 2 radians when the update method runs(testRot += 0.034906585f). The main thing of my confusion is, the asset rotates clockwise in this screen space. This confuses me as a rotation matrix will rotate a vector counter-clockwise.
One other thing, when I specify where my position vector is, as well as my origin, I understand that I am rotating about the origin. Am I to assume that there are perpendicular axis passing through this asset's origin as well? If so, where does rotation start from? In other words, am I starting rotation from the top of the Y-axis or the x-axis?
The XNA SpriteBatch works in Client Space. Where "up" is Y-, not Y+ (as in Cartesian space, projection space, and what most people usually select for their world space). This makes the rotation appear as clockwise (not counter-clockwise as it would in Cartesian space). The actual coordinates the rotation is producing are the same.
Rotations are relative, so they don't really "start" from any specified position.
If you are using maths functions like sin or cos or atan2, then absolute angles always start from the X+ axis as zero radians, and the positive rotation direction rotates towards Y+.
The order of operations of SpriteBatch looks something like this:
Sprite starts as a quad with the top-left corner at (0,0), its size being the same as its texture size (or SourceRectangle).
Translate the sprite back by its origin (thus placing its origin at (0,0)).
Scale the sprite
Rotate the sprite
Translate the sprite by its position
Apply the matrix from SpriteBatch.Begin
This places the sprite in Client Space.
Finally a matrix is applied to each batch to transform that Client Space into the Projection Space used by the GPU. (Projection space is from (-1,-1) at the bottom left of the viewport, to (1,1) in the top right.)
Since you are new to XNA, allow me to introduce a library that will greatly help you out while you learn. It is called XNA Debug Terminal and is an open source project that allows you to run arbitrary code during runtime. So you can see if your variables have the value you expect. All this happens in a terminal display on top of your game and without pausing your game. It can be downloaded at http://www.protohacks.net/xna_debug_terminal
It is free and very easy to setup so you really have nothing to lose.

Resources