View Full Version : More advanced hitTest Function
Dr Warm
07-07-2004, 10:45 AM
Ok i'm trying to make a 3D pong game so in it i'll have some diagonal walls, but flash doesn't like balls to bounce off them (even when i bumped the frame rate up to 100), heres the hitTest i use
with(_root.ball){
if (_root.rightwall.hitTest(getBounds(_root).xMax, _y, true)) {
xspeed *= -1;
}
}
and then the same with xMin. Is there an easy code to do this with, plz check the fla if u have no idea what i'm talking about, or just download it anyway, i thought it was pretty cool (although dodgy as i just ripped off someones 2d pong game)
Dr Warm
07-10-2004, 06:46 AM
Could someone please post something, even a link to a page that could help. It would be much appreciated
curryjon51
07-13-2004, 09:01 PM
Hi
Is ok game you got there. Think the problem could be that getbounds is not very accurate. Would be better not to use it in your hitTests... maybe use something like this instead:
if (_root.rightwall.hitTest(_x+_width, _y, true)) {
xspeed *= -1;
_root.soundHit.gotoAndPlay(2);
}
if (_root.leftwall.hitTest(_x, _y, true)) {
xspeed *= -1;
_root.soundHit.gotoAndPlay(2);
}
Also sometimes because of stuff not being on exact pixels it is best to calculate where the next move will take you, and if that is outside your area, do not make the move.
hth
ps... Also noticed your ball is not quite round because of the hightlight shape you have at the top of it... the extra height could throw out the calcs??
Dr Warm
07-14-2004, 01:00 PM
I'm not quite sure what u mean about not letting the next move happen, that hitTest without the getBounds works heaps better. But of course it still sometimes goes through the walls, by the way why do you need _x+width only on the right wall, or do i put it on the left as well and then put an _x without the +width? ie:
if (_root.rightwall.hitTest(_x+_width, _y, true)) {
xspeed *= -1;
_root.soundHit.gotoAndPlay(2);
}
if (_root.leftwall.hitTest(_x, _y, true)) {
xspeed *= -1;
_root.soundHit.gotoAndPlay(2);
}
if (_root.rightwall.hitTest(_x, _y, true)) {
xspeed *= -1;
_root.soundHit.gotoAndPlay(2);
}
if (_root.leftwall.hitTest(_x+width, _y, true)) {
xspeed *= -1;
_root.soundHit.gotoAndPlay(2);
}
I've being playing a few pong games, how do people make it so that how far the ball hits from the centre of the bat goes out at a different angle/speed (probably to complicated for me). Cheers, Reuben
curryjon51
07-14-2004, 03:44 PM
Hi
Attachment shows how to calc and allow for the next move before it is made.
The registration point of mc's in mx is usually at 0_x,and 0_y, the top left corner, so you add the _width to the _x,if the obsticle is to the right.And you add the _height to the _y if the obstacle is at the bottom of the screen.
About the angle off the bat thing, never tried it... but it can't be that hard.
If the _x of the ball is < than the _x of the bat+its width/2, add say minus math.random() to the angle of the ball, if the _ x of the ball is more than that, add plus math.random to the ball... something like that should work
hth
Dr Warm
07-15-2004, 03:03 AM
I think i understand what ur saying, so if i say
if(this._x+this.xspeed+this._width > _root.wall._x){
xspeed *= -1;
}
or something like that? instead of using the hitTest altogether, i see how this works but i'm not sure if it will with a wall on a diagonal, because its a different _x for every y position. Anyway i'll give it a shot
BTW has anyone played curveball on albinoblacksheep (go into the games folder) its pretty cool, a little too advanced i think.
Dr Warm
07-15-2004, 03:13 AM
for the left wall i would have to do:
if(this._x+this._width-this._xspeed < _root.leftwall._x){
xspeed *= -1;
}
ok i'll give it a whirl
Dr Warm
07-15-2004, 03:17 AM
Hmm there must be something i'm doing wrong, cos its going all funky, just going straight up and down (just about). I'm not sure but maybe its because the xspeed is large, it thinks the next frame it will hit the wall, but also i think it maybe because of the diagonal, to fix that should i incorporate a _y value into that _x+width>wall._x?
BTW that thing to give a random xspeed depending on if it is lower or higher than the middle works quite well, cheers for that!
curryjon51
07-15-2004, 06:56 PM
Hi
Assuming everything is registered at the topleft corner, as is usually the case in mx, then the _width of the ball must be taken into account for the right wall only. For left, you may have to add the _width of the left wall, or some proportion of it, to the calculation, but not the _width of the ball.
Attachment is better version of what I posted earlier, with a ball bouncing off two sloping walls... not 100%, but ok.
hth
Dr Warm
07-18-2004, 08:53 AM
Cheers
vBulletin® v3.7.1, Copyright ©2000-2009, Jelsoft Enterprises Ltd.