I think this is rather strange that I haven't found anything... which means the answer is really simple but I'm not getting it or it's something else.
I'm using haxe and openFL and the exercise is specific about not using any other frameworks. I'm building an Atari breakout clone. I'm trying to handle the collision event between the block and the ball. All the blocks are stored in an array map[] which is being iterated.
if ( (ball.x > (map[i].x) && ball.x < (map[i].x+20)) && (ball.y > (map[i].y) && ball.y < (map[i].y+20)) ) {
this.removeChild(map[i]);
ballMovement.y *= -1;
}
The ball has 10px radius and each block is 20x20. (Also, yes the ball will bounce awkwardly but I haven't had time to make a decent bouncing function yet, so that'll have to do for now)
I'm getting some unexpected behaviour. Yes the blocks disappear now, but Sometimes it seems the ball bounces off invisible blocks. The question is, am I removing those elements properly? Is the collision detection not properly set?
Probably you should remove your block(map[i]) from map also, because seems that you check collision with map elements, not display elements.
Related
I recently separated my render timestep from the physics timestep. Physics is running at 20hz and the client interpolates the sprite position from the previous position to the current physics position every render call. I'm using a technique from Gaffer on Games: Fix Your Timetep!. This code is in the render(delta) call.
if (delta > 0.25f) delta = 0.25f;
accumulator += delta;
while (accumulator >= physicsStep) {
update(physicsStep);
accumulator -= physicsStep;
}
interpolate(accumulator / physicsStep);
renderWorld();
This is update(physicsStep)
prevPosition.set(position);
physPosition.add(velocity.cpy().scl(physicsStep));
bounds.setCenter(physPosition);
This is interpolate(alpha)
position.set(prevPosition).lerp(physPosition, alpha);
It renders smoothly most of the time, but as I said everyone once and a while the character will start to jitter. It becomes smooth again after a while. Any ideas on how to deal with this?
I've been messing with it and the jitter is being caused by the interpolation "slowing down" right before it reaches the target position (current physics position). It doesn't slow down when it runs smoothly, only when it jitters.
I've been trying to find out how to block the player from moving the correct way. However, the way I've been doing now, stops the player from moving at all.
I want the player to stop moving horizontally if it touches the side of the block's collisionArea, and if it touches the top or bottom of the block's collisionArea, I want it to stop moving vertically only. So that way you can still move up and down when you touch the side, and side to side when you touch top or botttom. Thanks.
if (player.collisionArea.hitTestObject(block.collisionArea))
{
player.y -= vy;
player.x -= vx;
}
Your basic approach is to move your object, test if the current position hits your obstacle, then move it back if it does. This could be adapted to separate out the x and y axis quite easily (reduced code for clarity):
player.x += vx;
if(player.hitTestObject(block)){
player.x -= vx;
}
player.y += vy;
if(player.hitTestObject(block)){
player.y -= vy;
}
However, there are potential problems with this. First of all, DisplayObject/x and y round their values to "twips", so your addition and subtraction of vx and vy will not necessarily result in the object landing back in the original position exactly. This can produce problems with objects getting stuck in walls and such. Sound strange? Try this:
var s:Sprite = new Sprite();
s.x = 1.1925;
trace(s.x); // 1.15
So, to avoid this, you can store the position separately instead of relying on the DisplayObject x and y property.
var oldPosition:Point = new Point(player.x, player.y);
player.x += vx;
if(player.hitTestObject(block)){
player.x = oldPosition.x;
}
player.y += vy;
if(player.hitTestObject(block)){
player.y = oldPosition.y;
}
Second, I thought I would just mention that while hitTestObject() is a convenient API to check the intersection of two display object bounding rectangles, in my experience when you mix it with movement and collision response it starts to break down, because it relies on the state of the display, which is subject to oddities like the twips rounding, and inflexible with timing (you can't, for example, project all your movements, then check collisions, since you need to move your display object to get a valid result from hitTestObject()). The better way IMO is to make collision detection purely math based, for example circle intersection/distance checks. All that hitTestObject() is really doing is Rectangle/intersects() based on display objects bounds anyway. Pure math based collision detection is more work to setup, but more flexible and in my experience more predictable. Just a thought for the future, perhaps!
First of all, this question is very genuine - Just for the next time, make sure you clarify the background of your question (i.e I`m making a game with an actionscript 2d graphics, blah blah blah).
Anyhow, I'll copy you a part of a code i written back in the day when i was into developing javascript games. Since i dont know how your objects are arranged, i`ll modify it in a way that'll just introduce the general algorithm.
// player: a class that holds an array of moves, and other relevant information. Varys on your program.
var optimizePosition = function (player) {
//after each time the player attempts to move, i check if he touches of the blocks. if he does, i move him back.
foreach(Block block in blocks)// replace this with an iterator for all the blocks
{
if (player.x-block.x < 32 || player.y-block.y < 32 ) // if player is touching a block:
undoLastMove(getLastMove(player),player);
}
// a little extra: always good to check if he's going out of the screen in the same function:
if(player.x > canvas.width - 64 || player.y > canvas.height - 64)
undoLastMove(getLastMove(player),player);
}
and each time the player moves, i call the function:
optimizePosition(player);
The content of undoLastMove(player) will contain the code you written above.
I am trying to create a simple Tower Defense Shooting game via the help of an online tutorial. The tutorial doesn't address an issue though.
This code is supposed to remove a bullet fired once it leaves the stage, but the bullet is only being removed upon leaving through the top or left sides of the stage.
My understanding is that the stageWidth/Height are supposed to handle the top and left and the <0 handles the bottom and right. I cannot see anything that would be an issue. Can anyone see why it isn't working for the bottom or right sides of the stage?
if (bullet.x < 0 || bullet.x > stage.stageWidth || bullet.y < 0 || bullet.y > stage.stageHeight){
bullet.removeEventListener(Event.ENTER_FRAME, moveBullet);
bullet.parent.removeChild(bullet);
bullet = null;
}
the bullet's coordinate system isn't necessarily the same as the stage's-- are they attached to the stage or maybe some other movieClip?
I'd suggest debugging, or adding trace statements, to see what the bullet's coordinates, and the stageWidth and Height are in the above code:
trace("bullet.x="+bullet.x+", stage.stageWidth="+stage.stageWidth);
For a project I need to make a flashgame with the Kinect for Xbox.
Using the AS3NUI library to get acces to most of the Kinect functions worked well for me.
It's a pong game where you have to move your paddle vertically through hand movement.
I've tried a few ways of moving the paddle, but none of them is really accurate. And if they are more accurate, they aren't smooth at all anymore.
My first methode was setting the paddle.y to the value of the hand.y.
_paddleLeft.y = (userLeft.leftHand.position.y);
Of course the result was terrible.
This was my next try:
_paddleLeft.y = (userLeft.leftHand.position.rgbRelative.y * stage.stageHeight*2);
This was pretty accurate, but not smooth at all, and sometimes it was very hard to reach the top of the stage, or the bottom.
At last, I tried something else. I saved the hands last Y position, and checked if the new position was higher or lower, and on that I moved the paddle up or down.
currYLeft = userLeft.leftHand.position.rgb.y;
currYRight = userRight.rightHand.position.rgb.y;
if (currYLeft > prevYLeft + 10){
isDown1 = true;
isUp1 = false;
}else if( currYLeft < prevYLeft - 10){
isDown1 = false;
isUp1 = true;
}
prevYLeft = userLeft.leftHand.position.rgb.y;
prevYRight = userRight.rightHand.position.rgb.y;
In another enterframehandler, I make them move up or down.
This is the most smooth way I achieved, but it isn't very accurate anymore. There is some kind of delay wich you don't have with the first two ways.
I would like to know if it is possible to make the movement smooth AND accurate at the same time? And how this is possible.
If you already have an accurate solution, May be you can try putting some easing in it for the smoothness. You might wanna try using this instead in the enterFrame loop
_paddleLeft.y +=
((userLeft.leftHand.position.rgbRelative.y * stage.stageHeight*2)
-_paddleLeft.y)*0.3;
0.3 can be altered to other numbers between 0 and 1, depending on the easing speed you want...That's my best guess, hope this helps ^^
I need to do bounding box collision detection manually. My current implementation sees bullets passing right by enemies. Not only that, sometimes it seems they get hit later..
For the objects that I want to hittest, I have getters like this:
public function get left():Number{
return x - width / 2;
}
(The 'crosshairs' are at the center of the movieclips)
When I check for collisions, I use this:
if(this.leftX >= Main.player.leftX && this.rightX <= Main.player.rightX && this.topX >= Main.player.topX && this.downX <= Main.player.downX){
The bullet has a x of 4, a y of 13, and the player is a 20 by 20 square.
Are ther better ways to do this or should I fudge the numbers a bit?
You are checking "this square is inside player's square", you want to check if any of corners are inside the other one. Distance between centers will be easier to write and may work as well for such a small objects.