How to have accurate collisions in Flash CS6 (AS3) - actionscript-3

i am creating an endless runner game and the player as to jump over an obstacle, and if he touches it he looses health. The current collision code is this:
if (man_mc.hitTestObject(crate_mc)) {
health--;
health_txt.text=health.toString();
which is inside a loop. But the problem is that my obstacles are triangles, so when you jump over them if you touch the 'hit box' or dimensions of the triangle you loose health. So how do i make it so the collision is only true if my player is touching the triangle. (and the triangle is a png with transparent background)
EDIT: I found that i could use hitTestPoint instead of hitTestObject but how do i know what co ordinates to put into the parameters?

I would abandon the use of hitTestObject() (or even hitTestPoint()) for collision detection. Instead, I'd use another algorithm for collision detection in your game
I'll assume your player is a (pretty) skinny box and you said that your obstacles are triangles. I'd first try using a basic method of checking if any of the triangle's points are within the player's bounding box. If it is, then you have a collision.
You could also go the other way and check if a player's points in the bounding box are within any triangle, but you may end up with an odd case where a triangle is poking in to the player, but none of the player's points are within the triangle (which can happen if a player gets a triangle right up between the legs >.> ). Also, by going triangle first, there are less points to check per player and triangle pair.
Note that using this simple method won't tell you how much intersection there is, but I'm guessing you may not need that, since you were using the hitTestObject() method before.
Following my comment
I won't write the code for you line by line, but it's pretty easy to figure it out. So I'll describe one method:
Let's say your player is a rectangle that is 50px wide by 100px tall and the center of the player rectangle is set as the player's position. Half the width of the player is 25px and half the height is 50px.
So if the player is at say 240, 370 (a random spot I made up on screen), then to check if a point falls within you'd check if the x point is within 215 to 265 (240 +/- 25px) and if it is, then you'd check if the y point is within 320 and 420 (370 +/- 50px). If both these conditions are true, then the point is either touching or within the player box (so there is a collision). If any of them are false then the point is not (you could stop checking if the first condition is false, saving you a extra check operation).
Once you have that working (a point vs box test), then you'd just run a triangle's points through it to see if the triangle is intersecting the player box. How you get your triangle's points would vary based on your triangles.

Related

Detect coordinates within a shape

Theres 2 parts to my problem and they are related. I have a weird shape on my interface illustrated below, I am trying to randomly spawn MovieClips within its' boundaries but I am having some trouble finding a good way to do it.
Question 1: I can run an If condition to check with bitMapData.hitTest to see if the MovieClip has randomly spawn within this shape, if it doesn't simply retry with a new set of random coordinates. However, is there a better way? Like a way to only take into account coordinates within the shape? There will be plenty of MC spawned at one go so I am hoping to lessen the load, or at least find an efficient way to do this calculation.
Question 2: The MovieClips spawned within this shape will eventually have collision detection mechanics that will repel itself when interacted with. Is there a way to contain them within this shape via some kind of boundary detection?
If it was a square, we could easily have contained them with a quick check on all 4 edges, but not with this shape. Currently I am thinking of using bitMapData.hitTest again to detect for out-of-bounds after being repelled, but how do I know which Point() is the nearest 'edge' of this shape to return the MC to?
For question 1: I'm going to go on an assumption that you have some geometry data about the shape.
One method you can use to check if a point is within a shape is to take that point, then draw a line from that point to infinity (the edge of the screen) in one direction. Then count how many times that line intersects an edge of the shape. If it's odd, the point is within the shape (or on the edge) and if it's even, than that point is outside of the shape.
First link in google: https://www.geeksforgeeks.org/how-to-check-if-a-given-point-lies-inside-a-polygon/
Or can also try a more simple method (at the cost of doing more work): if the above shape is generated with all squares and rectangles and you know the point and size of all of those: can just do a check for the point vs all the squares and rectangles that make up the shape.
For question 2: As Organis mentioned, I'd go with a library like Box2D to do this. You'll most likely spend tons of time (that you may not want to) if you try to implement this alone.
The big issue is how much cpu or gpu the code uses. You're trying to avoid using any collision detection. Collision detection is having code do calculations to determine the edges of an object. It should be the last option.
Most of the time you know there's no need for collision detection. You know where everything is and how big it is. Everything has a centerpoint and comparing simple number coordinates is the lightweight way to check if there's a need to check further.
When things get near each other, you only need to do a collision detection on the immediate area around an object. See how your shape fits in a box that is easy to check for collisions? That box should get a collision check before the actual jagged shape inside it.
Yes that collision detection box has to be drawn or mapped but it's done when the object is defined, not when the game is playing. If you are using sprite sheets, keep an xml of the boxes or circles around the shapes.

How to make the sprite.BoundingRectangle fit the sprite exactly

In LibGDX, I'm trying to create a game where you play a character who must survive waves of zombies. The plan is, when a zombie touches the player, the player loses health, and the zombie despawns; this works well until any of the sprites are rotated. Here's a screenshot of what I can imagine to be why:
To check the collisions, I use:
if (sprite.getBoundingRectangle().overlaps(Game.pl1.sprite.getBoundingRectangle())) {}
.. this is inside the Zombie class, and Game.pl1.sprite refers to the sprite belonging to the player.
So the red box is where I'm assuming the bounding rectangle is, based on where zombies despawn around it.
This is what I want the bounding rectangle to be:
I'm not sure if I can achieve this just using sprite.getBoundingRectangle so I was thinking perhaps I would need to use Box2D or maybe convert the sprite to a texture.
You could get the dimensions for a rectangle the size of the sprite if it wasn't rotated, and then rotate that.

AS3: having problems implementing correct rotation with atan2

I have the following scenario: player is in the middle of the screen and never moves from those coordinates. If I click with the mouse on the far right of the screen, the A* script kicks in and moves the background the player is on according with its own walkable/not walkable criteria, so for example if there is an obstacle in between the center of the screen where the player always is in the far right of the screen, the background correctly moves in the opposite direction And when needed moves around the obstacle, so you have the illusion of player walking, but instead only the background moves.
my problem is that I don't know how to rotate my player to simulate that he is following the path that the actual background is making. For example, in the beginning, the player is facing down. If I click on the right side of the screen, the player should face right. When an obstacle is present, the player should face the direction of the path around the obstacle and so on
but again technically my player never moves, so I don't know what coordinates should I have the atan2 use. In my mind I thought that the player should rotate toward the center of the next best hop in the array of best path created during the A* script, but for some reason I can't figure out the correct coordinates y and x for the atan2 command
I bet this is a simple thing I overlooked, but apparently my mind is in shutdown mode, so I can use a new fresh perspective :-)
thanks!
First, do not code in brain shutdown mode, this can save you lots of brainhurt time.
You don't need atan2() in this scenario, since your player is technically on a grid instead of on the screen. Give the player internal coordinates on the grid that will be updated as the player walks around the world, and use them in your A* script. The A* script generates a sequence of tiles the player should walk, thus you derive player's facing from current and next tile, for example, if the next tile to walk is adjacent upwards, you make the player face up and continue moving.

Flash AS3 - Centering camera on player when scaling in and out (zooming)

I am building a survival horror like game and am hoping to make a very nice camera system to compliment the mechanics. Part of this is the fact that you will be able to crouch down and cover your face. The camera work I want to do with this is to zoom in to the character in order to constrain the view for the player as well.
The current MC structure that I have is:
GameMaster
>
Spawner (this is for the player and all enemies)
>
Player
The issue I'm having is that scaling the GameMaster (which is where side scrolling and other global game effects are happening) causes the centering of the camera to offset based on how far away the player is from 0,0.
You can see the issue clearly in this video. The red arrows point to the 0,0.
On this stackoverflow question the answer says to make a container for everything and center the containers 0,0 over the target that you want to zoom around. This poses a challenge for me because I would then have to get proper coordinates for an object nested 4 MC's in. I'm also unsure what that will then do for my current side scrolling camera.
Is there a way that I could mathematically figure out the offset when the character ducks? It seems like a viable option because you can't move until you let go of crouch and the camera zooms out.
If not, is the container MC a good option or is it just one of those "you gotta do what you gotta do." type situations?
[Added]
I also see something about Transform Matrices or something. Is that something that would work? I know NOTHING about them but I assume they are CPU heavy and wouldn't be a good option for a mechanic prevalent throughout the whole game.
[Added 2]
ALSO, I want to do a rotation camera effect that suffers from the same 0,0 issue. Blatantly showing up as the player and level rotating around some far off pivot point.
If a Transform Matrix can swiftly and functionally offset the 0,0 to the players location so that I can do all the camera effects and alterations. I think that may be the best way to go.
----Close to Conclusion----
In regards to Vespers answer. Would I then be able to tween the resulting transform?
If so then that completely answers my entire problem. If not, I have no clue how to get the result I want.
I think the container is the cleanest solution. Since it'll be centered on the player, rotations and scaling will work normally. You mention getting the coordinates for the nested MC is hard, but there is a built-in function to do exactly that: localToGlobal()
To get the player position in global coordinates, just do player.localToGlobal(new Point(0, 0)). This should return the Player's center in global coordinates. If your main container is not in global coordinates (because it's nested inside another transformed MC, for example), you can use the opposite function on the container to convert from global to local:
container.globalToLocal(player.localToGlobal(new Point(0, 0)))
Now you just need to center the container. That could also be used to simulate the camera movement. If you update the container position at every frame, it'll give the effect of the camera following the player.

Does the player move or the tiles in a tile-based game?

I'm not sure if this question fits properly, but I'm wondering in tile-based games, does the tiles 'shift' when the player is moving (and the player stays in the same place) or does the player move instead (and instead the tiles stay in the same place)?
Visually or in the model?
Visually, there’s usually some code that keeps the player fixed in the middle of the screen while the tiles scroll under him, with the tiles stopping and the player moving to the edge of the screen when the end of the map is reached. Typically this implies some sort of camera that knows how to follow the player, and perhaps changes its behavior depending on which direction the player is facing, and can also be locked for special effects and cutscenes.
In the model, it makes no sense to update the positions of all the tiles and every single object relative to the player, when you could instead just move the player.
Well, the specifics of how you want to draw it are up to you. Some tile-based games show the player fixed in the center of the screen -- in that case, your function to draw the character sprite always centers the character, while the function to draw the background would take an offset.
Others might give the player a pixel offset as well, either to show movement or to give "edges" to a map, not allowing the player to see what lies beyond.
How you implement it in your game is really up to you -- there's no "right" way to draw your sprites.
The tiles do. Unless there's no more to scroll in a certain direction, then the character moves. It's better to have as much as possible that's static cached together rather than move all the tiles individually.
The player moves, and the camera moves. The tiles are drawn shifted by the camera's position so that the world appears to scroll while the player stays roughly centered on screen. The camera follows the player, but usually a little loosely. (For example, you'll notice that you can walk a bit towards the edge of the screen before it starts scrolling in Super Mario Brothers.)