PDA

View Full Version : [AS2] Making an object "solid"


sarka86
04-14-2009, 08:25 PM
Hi everybody!

I'm building a flash game with falling balls.
I have a box in which balls fall and bounce when they hit the edges or with each other. This works fine, but I'd like to avoid balls crossing each other. This happens when I have balls on the floor and other are falling on them. They will bounce a few times but at the end they will fall on the floor as well, even if the space is already "occupied" by other balls. The question is: how can I avoid this behavior? How can I make circles to be solid? Can someone suggest me good tutorials or explanation?

Thank you so much!

Fabio

pradvan
04-14-2009, 09:17 PM
Try this; http://www.metanetsoftware.com/technique/tutorialA.html

sarka86
04-15-2009, 09:16 AM
Wow! That's really interesting. I will spend the next weeks studying it :) (unfortunately the footnotes links are not available anymore)

Perhaps for my practical purposes I just need something easier. Considering I just use circles in the games and that frictions and bouncings are fixed, do you have some more specific suggestions? Or can you please show me the part of code which should work for my case (which projAABB to make circle don't overlap other circles?)?

Thank you so much!

scarce
04-15-2009, 04:11 PM
Wow! That's really interesting. I will spend the next weeks studying it :) (unfortunately the footnotes links are not available anymore)

Perhaps for my practical purposes I just need something easier. Considering I just use circles in the games and that frictions and bouncings are fixed, do you have some more specific suggestions? Or can you please show me the part of code which should work for my case (which projAABB to make circle don't overlap other circles?)?

Thank you so much!

Have you considered what is happening to the balls on the floor? How is it constantly touching a wall effecting it's variables? Ya know what I mean?

sarka86
04-15-2009, 06:21 PM
Hi scarce!

I guess the problem is right there, but I have no idea about the solution. I tried to force the ball speedX and speedY to 0 when it's hitting the floor and it works for one row of balls. But when more come, they hit the 2st row's ball and move them down overlapping the 1st row. This because hitTest is too primitive to consider more rows hits. I tried to build my own function with loops to check a further collision but got stucked on that... :o

And I have just elements of analytic geometry and point mechanics, but here solid physics is involved.

scarce
04-15-2009, 10:57 PM
Hi scarce!

I guess the problem is right there, but I have no idea about the solution. I tried to force the ball speedX and speedY to 0 when it's hitting the floor and it works for one row of balls. But when more come, they hit the 2st row's ball and move them down overlapping the 1st row. This because hitTest is too primitive to consider more rows hits. I tried to build my own function with loops to check a further collision but got stucked on that... :o

And I have just elements of analytic geometry and point mechanics, but here solid physics is involved.

You have the same code pretty much going on all, 4 walls right? Use the code you have for the walls and roof, as far as the ground goes, give it a do/while loop instead of an if/then loop. The balls that bounce a round, do you have a for/loop running 1 code for all the balls? If not that might help: I'm on my phone so I'll try to give an idea:


ballCode.onEnterFrame = function(){
Var ballz = 0
var ballCount = 0
Var theBalls:Array = new Array()
for(i = 0;i<=ballCount;i++){
theBalls = this["ballz"+i]
//with this setup, using theBalls as your mc to establish the x and y value and gravity blah blah blah because your putting all values into an array and one code applies to each variable in the array
}
}

newblack
04-16-2009, 04:58 AM
do a search for "constrained dynamics" or "multibody dynamics"

it's a super complicated subset of physics simulation and so i'd recommend using a physics engine like box2d.

you can probably get satisfactory results with the right integrator- how are you moving your balls now? (that's what she said)

sarka86
04-16-2009, 01:54 PM
Hi newblack!

This 2Dengine sounds really cool. I will need other months to learn how to use it with Flash (maybe a will ask about that in a further topic).

I have 3 walls (_root.box gives the boundaries). Balls fall from the top.
My movement code is this one (placed on the ball instance). I'm sure it's primitive and there are thousands better version... mod(a,b) function returns true if a < b or a>-b

onClipEvent (enterFrame) {
if (this != _root.ball) {
//friction on x
if(this._y == _root.box._y){
if(!_root.mod(this.speedX, 0.3)){
if(this.speedX > 0){
this.speedX -= 0.2;
}

if(this.speedX < 0){
this.speedX += 0.2;
}
} else {
this.speedX = 0;
}
}

//bounce and movement limitation on y
if (this._y<_root.box._y) {
//If the speed increment move the ball out of borders, I place it on the border.
if ((this._y+this.speedY)>_root.box._y) {
this._y = _root.box._y;
} else {
this._y += this.speedY;
this.speedY += 0.6;
}
} else {
if (this.speedY != 0) {
this.speedY *= -1;
this.speedY /= 2;
//Force speed to be 0 if less than 0.8
if (this.speedY<0.8 && this.speedY>-0.8) {
this.speedY = 0;
} else {
//altrimenti permetto il rimbalzo
this._y = _root.box._y-1;
}
}
}

//Speed limitation
if (this.speedX>limite) {
this.speedX = limite;
}
if (this.speedX<limite*-1) {
this.speedX = limite*-1;
}
if (this.speedY>limite) {
this.speedY = limite;
}
if (this.speedY<limite*-1) {
this.speedY = limite*-1;
}

if(this.speeX != 0){
dx = _root.box._x+_root.box._width-this._width;
//Bounce and movement limitation on x (Math.round(this._x*1000)/1000 is needed, I can't understand why)
if ((this._x>_root.box._x && this.speedX < 0) || (Math.round(this._x*1000)/1000< Math.round(dx*1000)/1000 && this.speedX > 0)) {
if ((this._x+this.speedX)<_root.box._x && this.speedX < 0) {
this._x = _root.box._x;
} else {
if (this._x+this.speedX>dx && this.speedX > 0) {
this._x = Math.round(dx*1000)/1000;
} else {
this._x += this.speedX;
}
}
} else {
if (this.speedX != 0) {
this.speedX *= -1;
this.speedX /= 1.5;
if (this.speedX<0.9 && this.speedX>-0.9) {
this.speedX = 0;
} else {
if (Math.round(this._x*1000)/1000 == Math.round(dx*1000)/1000) {
this._x = Math.round(dx*1000)/1000-1;
} else {
this._x = _root.box._x+1;
}
}
}
}
}
}
}