AS3 Predictive collisions (Sweep testing) - actionscript-3

I'm making a basic platformer engine in AS3, and I'm currently implementing gravity. The gravity currently lets the player sink a few pixels into the ground when it lands (depending on the player's speed), and I don't want that.
I want to be able to test collisions 1 frame ahead of time (sweep testing), and if a collision will happen in the next frame, I want the player to stop 0px into the ground, regardless of the player's speed.
I also want pixel-perfect collisions that take the shape of the terrain, so I've been using Corey O'Neil's Collision Detection Kit (CDK), which works very well for detecting collisions, but I can't work out how to make it predict them. I'm not sure if this is the best library to use.
I'm not sure about Box2D; I feel it's too much for what I want.
Any idea of how can I do pixel-perfect sweep tests in AS3?

Box2D should be pretty good for small projects as well, also in case if you wish to scale up later...
If you need quicker dive in, have a look at Quick Box 2d.
There are couple of good tutorials (here & here) as well.

I want to be able to test collisions 1 frame ahead of time (sweep
testing), and if a collision will happen in the next frame, I want the
player to stop 0px into the ground, regardless of the player's
speed.
In you game loop, where you are checking the colisions, just add the movement that will be done in the next frame, the current Y speed.
Something like this:
if(currentYSpeed + player.y >= ground.y)
{
player.y = ground.y
}else
{
player.y += currentYSpeed;
}

Related

Bullet - Rigid bodies not colliding at high speed?

I'm trying to make a basic first person shooter game with Bullet and OpenGL. I'm having the issue of my rigid bodies not colliding at high speed.
My bullets will go straight through any other rigid bodies that I have, such as walls. Reducing velocity to less than 10 does result in collisions, but this is too low for a moving bullet. The bullet also moves insanely fast (I know it's a fast moving bullet, but sometimes I can't even see it, not sure if that's expected).
I'm thinking that it's to do with how I'm stepping the simulation? Reading up on it has left me confused. How can I make it so that my objects will always collide (at least, when going reasonably fast), and if possible, is there a way to slow the simulation down whilst maintaining the correct bullet velocity etc. so that I can actually see the bullet moving and colliding?
Here are some approaches to solve:-
It is copied from How can I avoid missing collisions for fast moving objects? - an official FAQ
smaller timesteps
extruding the object along the motion
ray cast to the new position
swept collision test (convex cast, linear cast)
continuous collision detection, including rotational motion
Please read the link for more detail. It is not a trivial issue.
One important thing to ask before try anything: do you really need high speed object?
It is not free (cost more CPU).
Here is another useful link (less useful though) : https://gamedev.stackexchange.com/questions/11961/how-can-i-enable-ccd-in-bullet-physics

as3 - Difference between moving background and stage focusing on player?

When you have a sidescroller and you want the player to appear like it's moving sideways. There are two ways, moving the background when pressing the keys, or making the stage follow the player whenever the player moves.
Could someone tell me what makes the big difference and which one is preferred and the safer and reliable way?
Background moving so player appears to be moving:
background.x = scrollX;
background.y = scrollY;
Stage following player:
background.x = -(player.x - stage.stageWidth / 2);
Feel free to use either method according to your design preference. Both will still have the same visual effect, but if you're looking for a performance increase tell objects to unload when they're past a certain point. That way either way you go you wont have to worry about performance. It's the number of objects you have on stage that counts when it comes to 2d side scrollers.

Detecting point where two objects collide

I'm totally beginner in Flash and Actionscript so sorry if my questuon is stupid.
I have to make a platform game - I'm planning to write something like Icy Tower - my character is jumping from one platform to another to get to the top of a tower of something. And here is my problem - I want to allow the character to jump onto a platform only from the top, but if he collides with whe platform from the bottom, or from the side, I want him to bounce (I hope you understand what I want mean).
So, to do something like that, I need to be able to detect where exactly do these two objects collide - the only solutions that comes to my mind is to keep the coordinates of every platform in some array and compare them with the characeter's position evertime he jumps, but it just doesn't seem right. Is there an better way to solve this problem?
I will be grateful for any advice.
PS. Sorry for my English
You can make use of a game library like flixel, or flashpunk. They are both great.
Essentially, you will need to iterate over all objects that your character can collide with. You can make use of .hitTestObject() to roughly determine if your character is colliding with any of the objects. Thereafter you can then get the coordinates and dimensions of your character on the stage and compare them to the coordinates and dimensions of the hit object to determine directions.
So in psuedo code
for each (var platform:DisplayObject in platforms) {
if (character.hitTestObject(platform)) {
if (collideFromBottom || collideFromSide) {
// allow movement
}
if (collideFromTop) {
// stop downward velocity
}
break; // might as well, as you have detected the collision
}
}
CollideFromBottom would be comparing the top of your character with the bottom of the platform. Remember that as Sprites, both player and platform have [x,y] properties and [width,height], allowing you to determine coordinates.
P.S. this question should have been asked at gamedev.stackexchange.com
I'd recommend you to use a Physics Engine like Box2D(
behold the examples), instead of creating some hitTest colision based game from scratch.
The probability of you having serious code/design problems and leaving the project aside are big.
There's a lot of good tutorials on internet, here a good resource - Using Box2d to Create a Side Scrolling Game:
Part 1
Part 2

Collision and bounce detection from array of points

I have an array of points that I will use to generate a closed polygonal fence on the outside of a game stage (2D). I wish to have collision detection between this fence and a bouncing ball-like object on the inside.
Additionally, I would like to be able to arbitrarily add/remove/redraw the fence in realtime and have the collision detection still operate realistically.
I have considered drawing a Sprite/Shape from the points and doing a HitTest at each frame to check whether to bounce or not.
My question: is this the best/correct way to accomplish this goal? Consider something like JezzBall with diagonal lines of any angle a simulation of what I'm trying to do.
Check the corners of your bouncing ball with four hitTestPoints. If it succeeds, then do hitTestPoints from the fence with shapeflag set to true.
There may be better solutions as I do not know the performance impact of shapeflag, but combined with the corners optimization I think it will be good, but I'm also interested if there is a better way.
Math will be your friend here. Do a quick search for circle-line, or point-line collision (here's one: Circle line-segment collision detection algorithm?).
What you do is run through your array of points, creating lines. So line 1 will be points[0] and points[1], and line 2 will be points[1] and points[2]. After that you check each line against your ball (if you want proper collision that will work no matter the frame rate, then you create a ball line, which is the line that the ball has travelled along between the last frame and this one). Do your collision detection against the ball line and each line made from your points (there's tons of line-line collision detection algos on the web). What you'll get out of an algorithm like that is the time the collision takes place in the current time step, as well as the normal of the colliding line, which will give you the reflection angle.
If you don't know Vector math, then learn it, it'll make your life a ton easier. Again, there are tons of implementations of a Vector2 class on the net.
You can arbitrarily remove parts of the wall as needed by just ignoring those points in your check.
Another lazy solution would be to use a physics engine like Box2D http://box2dflash.sourceforge.net/ or Nape: http://code.google.com/p/nape/ - it might be overkill for what you want for your game, but hey, it's easy.
For bonus points, another technique which might be easier for you is the Separating Axis Theorem, which is used in the flash game N: http://www.metanetsoftware.com/technique.html

AS3 Simple AI, find player in viewfield?

Hey guys, I was looking into a simple flash game, and have a few enemies patrolling the area. I made each of them have a view angle of about 35 degrees, and a distance of 150 px. The problem is that they can see through walls. How would I check to see if a wall was in between the guard's view of the player?
I thought of ray tracing, but don't know a way to implement it. I also thought of maybe drawing a rectangle between the player and the guard, and collision check it against the walls, but again don't know how to implement it..
Any ideas?
Thanks,
Max
Just a thought.
Keep your code just as it is, but do a second test.
If it finds a target with your current code run another test to see if there is a wall between them with a ray type test.
This would keep your working code as it is and minimize the field for ray testingto the width of the target.
I think that you are not that far off.
Since your walls will probably be sprites or some other display object, you know their positions ect.
Then you could draw an opaque sprite with alpha 0 that embodies your enemy viewing range and then do a collision test on all walls and all viewing ranges.
This will also enable you to do a collision detection between the player and all enemy viewing ranges.
After a quick search this might be what you need
Cheers