View Full Version : hitTest glitch with ball and several walls
xZeRo23
February 8th, 2009, 11:48 PM
I'm making a game where the ball bounces off several walls to get to its destination: the portal. When the ball reaches a high speed it glitches through most of the walls.
Here is my scene for the 3rd level of my game where the most glitches occur (attached file)
In the picture there lists all the variables i put in the objects. You will also notice that I put a "Stuck" button since there were many glitches.
Here is my code:
onClipEvent (load) {
power = 1;
yspeed = 0;
xspeed = 0;
friction = 1;
gravity = 0.5;
thrust = 1.6;
}
onClipEvent (enterFrame) {
if (Key.isDown(Key.LEFT)) {
xspeed -= power;
}
if (Key.isDown(Key.RIGHT)) {
xspeed += power;
}
if (Key.isDown(Key.DOWN)) {
yspeed += power*thrust;
}
xspeed *= friction;
yspeed += gravity;
_y += yspeed;
_x += xspeed;
if (_root.wall2.hitTest(_x-(this._width/2), _y, true) or _root.wall2.hitTest(_x+(this._width/2), _y, true)) {
xspeed *= -1;
}
if (_root.wall1.hitTest(_x, _y-(this._height/2), true) or _root.wall1.hitTest(_x, _y+(this._height/2), true)) {
yspeed *= -1;
}
if (_root.bounce.hitTest(_root.hero)){
_root.hero.yspeed*=-1;
}
if (_root.bounce1.hitTest(_root.hero)){
_root.hero.yspeed*=-1;
}
if (_root.bounce2.hitTest(_root.hero)){
_root.hero.yspeed*=-1;
}
if (_root.portal.hitTest(_x, _y, true)) {
_root.gotoAndStop(4);
}
}
rrh
February 11th, 2009, 04:37 PM
Quick side comment. If you generate an array like so:
bounceArray = [bounce,bounce1,bounce2];
then you can loop through the array rather than copy and paste your code every time you add a new bounce.
The likely source of your problem is the trouble is this. You reduce the speed and apply it like so:
xspeed *= friction;
yspeed += gravity;
_y += yspeed;
_x += xspeed;
Then when you hit something, you reverse the speed to get out:
_root.hero.yspeed*=-1;
With the expectation that in the next iteration of the loop, it will leave the platform the same way it came in.
The problem is that your yspeed is then altered again before it gets that chance:
yspeed += gravity;
You see what I'm saying? On one iteration, you'll go downwards at a speed of 10, say. Then it detects you've collided and so reverses the speed to -10. Then the next iteration it will add gravity (let's say gravity is 1) so the speed is -9. So then it moves up 9 pixels. If by chance that one pixel difference was the threshold between hitting and not hitting the platform, then it will hit it again, and then reverse down to +9 speed.
dandylion13
February 11th, 2009, 07:56 PM
Hello,
The problem is that if the speed of your object is greater than the width of the wall, the object will "jump over" the wall and never detect a hit.
To fix this make sure that your object never travels faster than the width or height of the walls. You can do this by adding some kind speed limit control. You can also make your walls higher and wider (or at least appear to be so.)
You can used some advanced techniques like multisasmpling or a grid based collision detection system to fix this if you really need to, but that's probably overkill for this project. It's a lot of work.. I'd avoid this if you can ;)
One last thing is that you should know that your bounce system isn't actually accurate acording to how objects bounce in the real world. The bounce will appear fine from one angle in one direction, but not any other angles or directions. However, that may not matter in this game. If it does though, let us know :)
Good luck!
xZeRo23
February 12th, 2009, 04:59 PM
Hello,
You can used some advanced techniques like multisasmpling or a grid based collision detection system to fix this if you really need to, but that's probably overkill for this project. It's a lot of work.. I'd avoid this if you can ;)
Good luck!
I'm interested in this grid system even though it might take a while to learn it. Can you teach me how to use this method? You also said that it wasn't a real bounce engine. How can I make it like real life?
dandylion13
February 14th, 2009, 04:15 AM
Hi :)
These are both pretty big topics that you might stuggle find good resources for or simple tutorials on the net.
For grid based collision, you can check out the chapter on it in this book:
http://www.friendsofed.com/book.html?isbn=9781430216087
It's excellent and very clear.
For accurate bounce physics, it'a a bit harder, as I do not know of any clear or accurate tutorials on this. You could take a look here:
http://www.harveycartel.org/metanet/tutorials/tutorialA.html
... but it's pretty vague on specifics.
scottc
February 14th, 2009, 05:22 AM
What:
Actually the real reason why most beginners have trouble with overlapping objects, objects that get stuck on each other etc.
Why:
The fact of, checking for collisions when it's to late (once the objects are already overlapping!).
Solution:
Don't use hitTest directly on your display objects. (you can only detect collisions once the shapes are already overlapping, and thus they can get stuck on each other.)
You can still use hitTest, just don't do it in such a way that you cannot check collisions before you update your sprite positions.
Separating your game logic from your display generally fixes the problem... (There are other advantages such as being able to switch your display without breaking your game logic. for example switching to papervision3d if you needed to sometime in the future.)
xZeRo23
February 16th, 2009, 11:02 PM
Hi :)
For accurate bounce physics, it'a a bit harder, as I do not know of any clear or accurate tutorials on this
I should have mentioned this before but I am using ActionScript 2.0 not 3.0... Anyway I found a good tutorial that talks about bounce physics:
http://www.emanueleferonato.com/2007/02/17/create-a-flash-draw-game-like-line-rider-or-others-part-3/
She uses trigonometry to find the accurate physics of the ball depending on the angle.
The thing I need help with is understanding how she codes the hitTest. Can someone help me with this?
rrh
February 17th, 2009, 11:57 AM
She explains it in more detail in part 2.
http://www.emanueleferonato.com/2007/02/03/create-a-flash-draw-game-like-line-rider-or-others-part-2/
xZeRo23
February 26th, 2009, 08:58 PM
I have read her tutorial but I can't quite understand the code. Can anyone help me understand the physics of the ball in the actionscript? I am also having some glitches when the ball bounces off the walls after formatting her code into mine. The plus side is that the ball doesn't go through anymore :D.
Actionscript in the ball: onClipEvent (load) {
gravity = 0.9;
yspeed = 0;
xspeed = 0;
radius = 25;
friction = 0.90;
precision = 360;
bounces = 0;
power = 0.6;
thrust = 0.75;
}
onClipEvent (enterFrame) {
collisions = 0;
sum_x = 0;
sum_y = 0;
if (Key.isDown(Key.LEFT)) {
xspeed -= power;
}
if (Key.isDown(Key.RIGHT)) {
xspeed += power;
}
if (Key.isDown(Key.DOWN)) {
yspeed += power*thrust;
}
yspeed = yspeed+gravity;
for (x=1; x<precision; x++) {
spot_x = _x+radius*Math.sin(x*360/precision);
spot_y = _y-radius*Math.cos(x*360/precision);
if (_root.wall1.hitTest(spot_x, spot_y, true)) {
collisions++;
sum_x += spot_x;
sum_y += spot_y;
}
if (_root.wall2.hitTest(spot_x, spot_y, true)) {
collisions++;
sum_x += spot_x;
sum_y += spot_y;
}
}
if (collisions>0) {
_root.last_hit._x = old_x;
_root.last_hit._y = old_y;
bounces++;
ball_dir = Math.atan(yspeed/(xspeed*-1))/(Math.PI/180);
if ((xspeed*-1)<0) {
ball_dir += 180;
}
if ((xspeed*-1)>=0 && yspeed<0) {
ball_dir += 360;
}
spot_x = sum_x/collisions;
spot_y = sum_y/collisions;
x_cat = spot_x-_x;
y_cat = spot_y-_y;
ball_coll = Math.atan(y_cat/x_cat)/(Math.PI/180);
if (x_cat<0) {
ball_coll += 180;
}
if (x_cat>=0 && y_cat<0) {
ball_coll += 360;
}
ground_rotation = ball_coll-90;
if (ground_rotation<0) {
ground_rotation += 180;
}
bounce_angle = 180-ball_dir-2*(ground_rotation);
if (bounce_angle<0) {
bounce_angle += 360;
}
speed = Math.sqrt((yspeed*yspeed)+(xspeed*xspeed));
xspeed = speed*Math.cos(bounce_angle*Math.PI/180)*friction;
yspeed = (speed*Math.sin(bounce_angle*Math.PI/180))*-1*friction;
_x = old_x;
_y = old_y;
}
_y = _y+yspeed;
_x = _x+xspeed;
}
pantas
February 27th, 2009, 07:21 AM
it begins in part one. you ned to follow all parts to understant the dynamics
cbeech
February 27th, 2009, 11:22 AM
wasn't there just a tutorial on this listed on the kirupa front page?
http://www.kirupa.com/developer/as3/physics_bounce_effect_pg1.htm
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.