PDA

View Full Version : line circle collisions



slideth3
August 21st, 2006, 03:07 PM
Hey people

been making a physics based game and need some line collisions with circles. Here is what I came up with today (this isn't the game its just a new approach to the collisions which weren't so good previously, before I kind of rotated the axis which meant I could only do it one sided but back to the point)

http://www.kitegallery.net/v/misc/Slideth/Sam/line+circle+collisions.swf.html

The only problem is that when the ball collides with the lines sometimes there is an overlap between the balls and the lines and the ball gets stuck.

I been thinking about the maths for the last hour and trying to work out how to get the ball to appear at the closest point on the edge of the line to where it is whenever it is overlapped....

I know I need to use the angle of the wall and presumably the distances from the nodes to the ball and stuff. Anyone got any clues lol my brain is fried.

here is a link to the actual game. the things may look ok but bounce to low and you'll fall through which is not good

http://www.kitegallery.net/v/misc/Slideth/Sam/Gball.swf.html

slideth3
August 22nd, 2006, 09:31 AM
anyone got any ideas

http://www.kitegallery.net/misc/Slideth/Sam/Physics.swf.html

in this version I kind of solved it getting caught by making the ball move away twice after colliding. works better but its still not smooth, with the ball in the wrong place, kind of skipping a step. It still sometimes overlaps with the line as well.

What I need is to find the position on the edge of the line where the ball would be if it wasn't overlapping and then move the ball there.

thanks :)

Sam

SacrificialLamb
August 22nd, 2006, 06:07 PM
it's hare to give good ralivent advise with out knowing how to code work's. not that i think i would be abel to help if i was requiered to be more pasifick.
but have you tryed useing

while (thing.hitTest(otherthing)) {
move one way or other
}
this will keep moveing it in one frame till it's nolonger tuching.

slideth3
August 23rd, 2006, 11:01 AM
yeah I got that concept lol but I need it to move to an exact position ie the tangent to the line. My hitTesting is distance defined ie if the distance is less than the radius hit=true. Heres a diagram to better describe my dilema... I literally just need the position in a tangential direction where a circle would appear on a line in order for its center to be its radial distance from the line. lol :)

http://img148.imageshack.us/img148/2349/4154um7.th.jpg (http://img148.imageshack.us/my.php?image=4154um7.jpg)

slideth3
August 23rd, 2006, 12:28 PM
woohoo I got it sorted :D

Nearly perfect, for not an ounce of vector stuff I think it works pretty well!

http://www.kitegallery.net/misc/Slideth/Sam/Engine+demo_001.swf.html

let me know what you think! :D

Sam

here is one with wider walls and spawning balls lol

http://www.kitegallery.net/misc/Slideth/Sam/Engine+demo_002.swf.html

saxplayer13
August 26th, 2006, 11:58 AM
I like your demos, great job!
The last one especially, if you tilt the floor up to like a 30degree angle, the ball seems to bounce a little too high- other than that, the collisions are really realistic.

slideth3
September 10th, 2006, 06:53 PM
hey just been working on some ik so here it is lol

http://www.kitegallery.net/misc/Slideth/Sam/halfpipe+3+wheels.swf.html

works very nicely!

and here is a bigger one with only one ball

http://www.kitegallery.net/misc/Slideth/Sam/physics+playground+2.swf.html

saxplayer13
September 11th, 2006, 09:52 PM
the bigger one I can't seem to drag the ball- you may not have planned this in it, but it is kind of boring if you can't move the ball.

Joppe
September 12th, 2006, 05:02 AM
^ you use the key arrows..

slideth3
September 12th, 2006, 05:20 PM
just got started on some verlet integration and its incredible lol.

ragdoll:

http://www.kitegallery.net/misc/Slideth/Sam/in+circle+with+drag.swf.html

skateboarding ragdoll:

http://www.kitegallery.net/misc/Slideth/Sam/skateboarder.swf.html

solid shapes:

http://www.kitegallery.net/misc/Slideth/Sam/triangle+solid+in+halfpipe.swf.html



so much more is possible, the ragdoll is done with solid constraints and spring constraints.

slideth3
September 13th, 2006, 07:31 AM
3d verlet is also possible with the engine!

http://www.kitegallery.net/misc/Slideth/Sam/3d+verlet+tetrahedron.swf.html

gonna try making a 3d car and ragdoll later

if anyone wants the source let me know

Cheers

Sam

Joppe
September 13th, 2006, 08:44 AM
Looks really nice Sam! :thumb:
Keep it up, and yes it would be interesting to see the source :)

slideth3
September 13th, 2006, 01:26 PM
you on msn?

add me

sam-halliday@hotmail.co.uk

cheers

Sam

here is annotated code!!! 2d version at the moment have fun :D



//verlet integration 2d

//how many points you have on the stage; points should have instances ball1, ball2, ball3 etc.
point_no = 10;

//this creates an array containing x, y, oldx, and oldy posiitions for each point
points = new Array();
for(i=1;i<point_no+1;i++){
points[i] = {x:_root["ball"+i]._x, y:_root["ball"+i]._y, x2:_root["ball"+i]._x, y2:_root["ball"+i]._y};
}

//iterations is how many times calculations are repeated per frame, should be >5 for best results
iterations = 10;
gravity = .5

//on enter frame call each function
function onEnterFrame(){
verlet();
constraints();
keys();
render();
}

//the verlet function pretty much moves the points by the difference of the previous two points
verlet = function(){
//for each particle
for(i=1;i<point_no+1;i++){
//store initial point
tempx = points[i].x
tempy = points[i].y
//verlet integration
points[i].x += points[i].x - points[i].x2
points[i].y += points[i].y - points[i].y2 + gravity
//store previous point
points[i].x2 = tempx;
points[i].y2 = tempy;
}
}

//this controls where each particle is allowed to go
constraints = function(){
//clear drawing after frame
_root.clear();
//for each particle carry out distance and boundary functions
for(it=0;it<iterations;it++){
distance();
boundaries();
}
}
//this function connects each particle/point with either a solid line (constrain) or a spring (muscle);
distance = function(){
/*/
for example typing

constrain(4, 5, 100)

would apply a constraint between ball4 and ball5 at a distance of 100 pixels

or

muscle(1, 12, 50)

would apply a springy constraint between the points at 50 pixels distance

you get the idea lol

do as many of these as you want for the particles you have on stage, make sure not to have conflicting distances unless you
want havoc lol the idea is to connect loads of straight lines.
/*/

}
//this keeps each point on the stage and moves the balls to the points
boundaries = function(){
for(i=1;i<ball_no+1;i++){
if(points[i].x > 540) (points[i].x = 540)
if(points[i].x < 0) (points[i].x = 0)
if(points[i].y > 400) (points[i].y = 400);
_root["ball"+i]._x = points[i].x
_root["ball"+i]._y = points[i].y
}
}
//this is the constrain function
constrain = function(point, target, distance){
r = distance;
//find distance;
dx = points[point].x-points[target].x
dy = points[point].y-points[target].y
d1 = Math.round(Math.sqrt(dx*dx + dy*dy))
//find the 'factor' of movement this is half the distance inbetween the particles
d2 = 0.5*(d1-r)/d1
dx = dx*d2;
dy = dy*d2;
//move the particles towards one another
points[target].x += dx; points[target].y += dy;
points[point].x -= dx; points[point].y -= dy;
//draw constrain line;
_root.lineStyle(2, 0xFF0000);
_root.moveTo(points[target].x, points[target].y);
_root.lineTo(points[point].x, points[point].y);
}
//this is the muscle function it is the same as the constrain except the factor changes
muscle = function(point, target, distance){
r = distance;
dx = points[point].x-points[target].x
dy = points[point].y-points[target].y
d1 = Math.round(Math.sqrt(dx*dx + dy*dy))
//change the 0.05 to anything between 0.01 and 0.5 for very soft spring to solid
d2 = 0.05*(d1-r)/d1
dx = dx*d2;
dy = dy*d2;
points[target].x += dx; points[target].y += dy;
points[point].x -= dx; points[point].y -= dy;
}
//move each particle with the arrow keys.
keys = function(){
for(i=1;i<point_no+1;i++){
if(Key.isDown(Key.RIGHT)) points[i].x += gravity*2
if(Key.isDown(Key.LEFT)) points[i].x -= gravity*2
if(Key.isDown(Key.UP)) points[i].y -= gravity*2
if(Key.isDown(Key.DOWN)) points[i].y += gravity*2
}
}
//any drawing api stuff could go in here if you want :)
render = function(){
}

//have fun, any questions email me./..... sam-halliday@hotmail.co.uk

slideth3
September 13th, 2006, 07:30 PM
here is a rendered one in 3d with a slope and random tetrahedral based shapes.

http://www.kitegallery.net/misc/Slideth/Sam/3d+verlet+rendered+random+tetrahedral.swf.html

Depth swapping isn't great, any tips on rendering it?

Nich
September 24th, 2006, 11:11 PM
slideth: I'm making my own physics engine, and this topic put me on to verlet integration. Before I was using Euler, because that's how it's supposed to work (:P), but now that I'm using verlet everything is a lot easier, and more dynamic. So first off, thanks.

Secondly, I've run into an annoying little bug, where any structure I make eventually collapses, either crushing it into a line, or crushing it into a line and then reforming it causing it to bounce around the screen. This is obviously undesirable.

To show you what I mean, I've attached an swf. Hopefully you have encountered this bug in your developement, and know how to fix it :)

Keep up the great work :).

slideth3
September 25th, 2006, 12:31 PM
hi,

to start with rectangles are not very strong structures, even with the diagonal constraints, when constructed how I presume you have done it. I would recommend 4 triangles which share a vertex and a constraint with one on each side. So you have four forming the rectangle.

The mass will still be evenly distributed as the shared vertex will be in the centre.

The reasons sometimes shapes collapse is that they might not be strong enough to conter the effects of gravity, or any additional forces like mouse dragging, wall collisions etc.

If you hit a wall really hard for example, and all particles align along the wall then they could all have the same x point. Now this becomes problematic in the way you and i have coded it, in that the points do not know which way to move away from the line, so they all stay in the same x position.

If you increase the strength of the structure enough you shouldn't have the problem of the flat line, or the bouncing.

cheers

Sam


edit: just noticed that for each constraint you've been calling it for both particles, with springyness of 0.5 you should only need to do it once, call the function for more iterations to improve stability as well, as opposed to back and forth as after the first the r should already be satisfied.

Nich
September 25th, 2006, 04:09 PM
I added the extra constraints as a last ditch effort to increase stability before I uploaded it, and forgot to change it back.

I tried drawing only one triangle, but it still breaks, regardless of the gravity/structureal strength of the triangle. I'll keep plugging at the code, I have a hunch it has less to do with my constraints as my interpretation of verlet. Thanks for your help.

Edit: SOLVED!

The problem was with my timer. I was adding 1 to it each time, and then resetting it to 0 when it hit 1000 to stop it from becoming too large. Unfortunately, I was using lastTime-time to get my DT for my verlet formula, and 1-1000 = -999. Needless to say, all my particles hit the bottom very fast, crushing of my shapes

slideth3
September 25th, 2006, 05:28 PM
I added the extra constraints as a last ditch effort to increase stability before I uploaded it, and forgot to change it back.

I tried drawing only one triangle, but it still breaks, regardless of the gravity/structureal strength of the triangle. I'll keep plugging at the code, I have a hunch it has less to do with my constraints as my interpretation of verlet. Thanks for your help.

Edit: SOLVED!

The problem was with my timer. I was adding 1 to it each time, and then resetting it to 0 when it hit 1000 to stop it from becoming too large. Unfortunately, I was using lastTime-time to get my DT for my verlet formula, and 1-1000 = -999. Needless to say, all my particles hit the bottom very fast, crushing of my shapes

lol good to hear! :D

silverneo188
September 25th, 2006, 07:01 PM
while (thing.hitTest(otherthing)) {
move one way or other
}
this will keep moveing it in one frame till it's nolonger tuching.

i have tried this but i get this error message

**Error** Scene=Scene 1, layer=actions, frame=2:Line 7: Syntax error.
move one way or other

whats wrong? ive replaced the code like this

while (sonic_mc.hitTest(circle_mc)) {
move one way or other
}

i need help making something like this

http://www.kitegallery.net/misc/Slideth/Sam/physics+playground+2.swf.html

Nich
September 25th, 2006, 10:20 PM
Your first problem is that "move one way or the other" is psudo code. While it would be nice if flash responded to that, it just doesn't work that way. I suggest finding a good beginner tutorial on actionscript, and learning the language.

as a quick and dirty fix, change "move one way or the other" to:

sonic_mc._x --

change -- to ++ if it's going the wrong way. Such code won't produce the results you see in the link, but it's a start to bigger things.

freeskier89
September 25th, 2006, 10:51 PM
Slideth, Some of the ragdoll stuff you have done has to be some of the coolest things I have seen done with flash lately :D. They are very impressive ;)

slideth3
September 26th, 2006, 12:15 PM
Slideth, Some of the ragdoll stuff you have done has to be some of the coolest things I have seen done with flash lately :D. They are very impressive ;)


lol they're not as cool as my latest ragdoll toy ;) :D

you know a few years back the game stair dismount came out. One of the first games to implement ragdoll physics as i recall. I remade it in flash

anyway thanks freeskier :)

heres a link

http://www.kitegallery.net/misc/Slideth/Sam/stair+dismount+flash.swf.html

select where to push it, and what force, try to maximise the pain! :D

I think that the next step is coming up with a really good 3d, or semi3d physics collision engine, and making ragdoll masters in 3d.

If anyone wants to help me with the 3d aspect of it add me to msn!

sam-halliday@hotmail.co.uk

cheers

silverneo188
September 28th, 2006, 03:04 AM
Your first problem is that "move one way or the other" is psudo code. While it would be nice if flash responded to that, it just doesn't work that way. I suggest finding a good beginner tutorial on actionscript, and learning the language.

as a quick and dirty fix, change "move one way or the other" to:

sonic_mc._x --

change -- to ++ if it's going the wrong way. Such code won't produce the results you see in the link, but it's a start to bigger things.
ummm i got a problem with Nich's help
When i do what he said i still get an error message.

Nich
September 28th, 2006, 07:18 AM
What's the error message?

And incidently, you DO have a MovieClip called "sonic_mc" initialised somewhere, correct? Otherwise you'll need to.

I myself have a question for slideth. Which methods are you using for collision detection? As you can see, I have the boundries set up fine, but I'm stumped at how to get two objects to collide properly.