HOME | DD

Sebioff — Collisions...in Flash?

Published: 2006-05-29 12:06:11 +0000 UTC; Views: 3289; Favourites: 23; Downloads: 226
Redirect to original
Description Since Flashs build-in collision detection is pretty useless if you want an absolutely EXACT detection which is needed in some cases (like in this experiment) I tried to do the detection myself. It's not that easy though if you want to let a ball collide with something as thin as in this example lines and the ball can either come from below or above the line...(see...if you know that the ball will always come from above or always from below it wouldn't be that much of a problem. In this case you can easily build a 100% working solution, like in Dynamic, Interactive Creature ).
This is what I've got so far, and I guess it's quite realistic...although there are still some problems, for example if you throw the ball too fast it might just fly through the line, I didn't find a 100% working solution for this yet. The better your CPU the faster the ball can get without flying through it.
If someone's got an idea how to solve this I'd appreciate it if you'd share it with me

So, this works without Flashs hitTest or something similiar.
Drag the ball and throw it around.
Drag the grey dots to change the lines.

Flash 8 required!
Zoom in to play.


Edit: Doh. I just realized that this is running WAY slower if you open it in your browser. If you open the .swf without browser its fluently working. Oh well.
Related content
Comments: 28

TechneCreations [2007-10-02 03:06:23 +0000 UTC]

I made a basket with the lines at one end and played b-ball... x_x That is AWESOME!

👍: 0 ⏩: 1

Sebioff In reply to TechneCreations [2007-10-02 11:22:49 +0000 UTC]

Lol
Remembers me that I had a prototype for basketball somewhere...

👍: 0 ⏩: 1

TechneCreations In reply to Sebioff [2007-10-03 02:12:33 +0000 UTC]

kewl

👍: 0 ⏩: 0

starearo [2007-07-16 01:48:46 +0000 UTC]

Heh, it goes really crazy if you put it in a tiny space xD

👍: 0 ⏩: 0

DampeS8N [2007-07-15 18:37:47 +0000 UTC]

ack.. in other words. Flash is basing it's locations of the ball based only on visible frames. The frames are the ticks, and the frames are the krux here. If the ball moves fast enough to be on one side of the line in frame A, and the other side of the line in frame B. There is no collision.

👍: 0 ⏩: 0

DampeS8N [2007-07-15 18:34:52 +0000 UTC]

Also. If you didn't know. The reason the ball will pass through the line, is because in frame A, it is beyond the radius and in frame B it is beyond the radius in the other direction. If you do what I said above, you would know where the ball is, and where it WILL be in the next frame. With that info, you can tell if the path will CROSS the line. Ergo, if it will collide. Incase I wasn't clear enough above.

👍: 0 ⏩: 0

DampeS8N [2007-07-15 18:32:10 +0000 UTC]

it is always a problem with flash, since you are almost forced to do it graphically. If you know the end and start points of a line, and you know the center and radius of a circle, you can check if they pass eachother BETWEEN frames. Do so with math. You should be able to find some formulas online, algebra, or possible calc equations to tell you if or when these objects will pass or meet. Then you would be able to predict one frame ahead of the collision, if it will collid.

That would be the trick. To calculate in the prior frame, what the next frame will do.

Typically in flash, you tell things to do things in the frame they are doing it... Move from here to here. Well.. this would be saying. In the next frame, move here to here. And within that, you calculate any collisions.

Is this possible to do in flash? I don't know.

👍: 0 ⏩: 0

amorphousbeing [2007-07-15 13:18:24 +0000 UTC]

i... lost the ball

👍: 0 ⏩: 0

kormyen [2007-07-15 11:53:50 +0000 UTC]

Hey man, this is really quite cool XD

I probally expect that you have a solution to your question by now, but ill put in my two sence anyway ^-^

" for example if you throw the ball too fast it might just fly through the line"

I read about this in a tutorial somewhere... I think =B. Thier solution is to draw a line from the "balls" current frame possition and its last frame possition and check that for a collition with the wall. I'm sure you understand(?) ^-^

`kormyen

👍: 0 ⏩: 0

NightMargin [2007-05-23 15:13:52 +0000 UTC]

that's amazing! how did you do it?

👍: 0 ⏩: 1

Sebioff In reply to NightMargin [2007-05-24 14:07:31 +0000 UTC]

Heh, I explained it one comment above

👍: 0 ⏩: 0

little-teenager-sam [2006-09-08 13:25:44 +0000 UTC]

this is very nicley coded, and great physics too. how could u do it without hitTest?

👍: 0 ⏩: 1

Sebioff In reply to little-teenager-sam [2006-09-10 17:46:54 +0000 UTC]

Well, if you want to create accurate physics in flash you CAN'T use hittest.
Hittest can be good for games or something similiar.
Anyway, you know the positions of the ball and the endpoints of the lines, so basically you just calculate
the distance of the ball to the lines. If the distance is smaller than the radius of the ball you know that there must be a collision

👍: 0 ⏩: 1

axcho In reply to Sebioff [2007-02-26 00:49:15 +0000 UTC]

Hmm, that's an interesting way of doing collision detection. I wonder if I could use something like that.

And by the way, your interactive creatures are really great.

👍: 0 ⏩: 1

Sebioff In reply to axcho [2007-02-26 15:55:10 +0000 UTC]

Thanks
Actually, I started working on a 100% working version and I'm also quite finished....but not completely. And I didn't find the time to finish it yet.
Oh well.

👍: 0 ⏩: 1

axcho In reply to Sebioff [2007-02-27 00:32:39 +0000 UTC]

Are you talking about the collision detection or the creatures? Either way I'd be interested in seeing what you have of it so far. Maybe in your scraps?

👍: 0 ⏩: 1

Sebioff In reply to axcho [2007-04-06 13:39:43 +0000 UTC]

Hey,
sorry for not answering for such a long time :/
I was talking about the collisions.
Actually I got a few started flash-projects that I couldn't finish yet, and I got no idea when I'll be able to
Maybe one day

👍: 0 ⏩: 1

axcho In reply to Sebioff [2007-04-19 18:53:50 +0000 UTC]

I was just trying to figure out the exact algorithms to get this working on my own, but I'm kind of stuck. Would you mind explaining in more detail how it works? Thanks.

👍: 0 ⏩: 1

Sebioff In reply to axcho [2007-04-23 16:31:11 +0000 UTC]

Well, try it like this:
Calculate the intersection points of the direction vector and the line vectors. Take the one which is the closest to the ball. Now, check whether the ball will move past this point if you add its speed to its position. If not - don't do anything. If yes - there must be a collision. Change the balls direction vector according to the angle of the line. Change the balls position so that its distance to the intersection point you calculated is the same as the balls diameter; the new position as well has to be a point on the perpendicular of the line the ball collided with and has to go through the intersection point (if you leave this position-placement out the collision might look weird or you might get strange sideeffects (like the ball flying through lines)).
This SHOULD be a 100% accurate collision detection (at least I think so), but obviously it's more like 90% accurate...
Didn't find the mistake in my thoughts yet.

👍: 0 ⏩: 1

axcho In reply to Sebioff [2007-04-25 05:28:44 +0000 UTC]

Thanks! I'll let you know what happens when I try it out.

👍: 0 ⏩: 0

flashGamer [2006-05-29 16:08:40 +0000 UTC]

Nice work. A way to get it to not get through the wall would be to check in intervals. Such as breaking the balls movement into 2 or 3 segments and checking at each interval. This would help eliminate the ball jumping through the wall. Maybe you implement it so that the fast it is moving the more you check so its not processor intense when it doesnt need to be. Just an idea. Anyway, nice work. I like it.

👍: 0 ⏩: 1

Sebioff In reply to flashGamer [2006-05-29 18:05:55 +0000 UTC]

thanks.
yeah, I've been afraid I had to do it kinda like this. been too lazy though and hoped there'd be a better way^^
good idea with the amount of intervals!

👍: 0 ⏩: 1

flashGamer In reply to Sebioff [2006-05-29 20:05:24 +0000 UTC]

Thank you. Cant wait to see ur finished product and what you do with it. So many possibilities!

👍: 0 ⏩: 0

flashGamer [2006-05-29 16:07:16 +0000 UTC]

Nice work. A way to get it to not get through the wall would be to check in intervals. Such as breaking the balls movement into 2 or 3 segments and checking at each interval. This would help eliminate the ball jumping through the wall. Maybe you implement it so that the fast it is moving the more you check so its not processor intense when it doesnt need to be. Just an idea. Anyway, nice work. I like it.

👍: 0 ⏩: 0

ExosSho [2006-05-29 13:33:57 +0000 UTC]

i dont know if u need this, but in case u did in a diffrent way, this maybe usefull fpr u(mybe) [link]

👍: 0 ⏩: 0

kingtut98 [2006-05-29 12:14:41 +0000 UTC]

That's really cool- one of the things you see all the time in flash, but I never realised how difficult it was to do from all angles.

I can't help with your "sliding through the line" problem, but I would suggest you put lines in around the side of the screen so the ball can't fly out of it if it breaks through.

👍: 0 ⏩: 1

Sebioff In reply to kingtut98 [2006-05-29 12:31:16 +0000 UTC]

True, that'd be usefull^^
Did it.

👍: 0 ⏩: 1

kingtut98 In reply to Sebioff [2006-05-29 13:00:39 +0000 UTC]

Excellent

👍: 0 ⏩: 0