Skip to main content
GameDev.net gamedev.net
🔒 Locked

basic gravity/friction simulation help

Started by PaulCesar Jan 20, 2008 at 1:56 AM 0 replies 1k views
Original Post
PaulCesar
PaulCesar
Hey all, Ok, I'm trying to do up an effect in one of my AS3 games here, its really not that complex I don't think, as there are a lot of "givens" I am going to use. Mind you AS3 experience is not at all necessary to help here, as I have general programming and math experience . Bear in mind the scene is only going to have 20-30 objects, so over optimizations are definitely not necessary. The effect im going for is simple but difficult to describe. Im trying to build an effect where you can drop an object randomly on the scene, and it pushes other objects out of the way (kinda bouncy-ish), which in turn pushes other objects out of the way. Collision detection is not a problem. This is simplified by the fact that I'm generalizing all objects to being a circle and getting the center of both circles and their radius's. This is further complicated by the fact that I don't want items to go out of bounds either, and to gravitate towards the center. So what I am trying to do is build a basic physics model representing "gravity" (towards the center of the scene), and repulsion (from the center of each object). The problem is my "idea" is missing a few things I need some help on. I have this so far. On each frame each object looks at all other objects on the scene. I do a distance check from our center to its center, if thats greater then our radius and their radius combined, then there needs to be some sort of pressure based on how much it overlaps. We can calculate how much it overlaps by finding the difference of the desired distance (our radius + their radius) and the actual distance between the two points. With that we get the amount the circle bounds overlap. The problem is that gives us a distance and not a vector for starters, its not normalized either. The issue comes into play because somehow I have to merge vectors (the pressure of all applicable objects around it, and gravity towards the center) I believe if I can do this, all the objects should try to fill up gaps naturaly (I could be wrong). Somehow the gravity needs to get stronger the distance from the center, and the inverse for the pressure from the objects, and this needs to give me a vector in which I can normalize and scoot it each frame. (Heres some current code, not that it helps much)
[Source]
		public function getPressureVector():Point{
			var force:Point = new Point();
			var pt1:Point = this.getCenter();
			// get each FlashMsg besides itself and calculate the pressure
			for(var index:Number = 0; index<this.parent.numChildren; index++) {
				// Grab the target message
				var target:FlashMsg = this.parent.getChildAt(index) as FlashMsg;
				if(target!=this){
					var pt2:Point = target.getCenter();
					var distance:Number = calcDistanceBetweenPoints(pt1, pt2);
					var expected_d = (target.width >= this.width) ? target.width : this.width;
					if(distance < expected_d){
						force.x = force.x + (pt1.x - pt2.x);
						force.y = force.y + (pt1.y - pt2.y);
					}
				}
			}
			// now we calculate the force from the sides
			if(this.x <= 0)
			{
				force.x = 0 - this.x;
			}
			if(this.x + this.width >= stage.stageWidth)
			{
				force.x = (stage.stageWidth - (this.x + this.width));
			}
			if(this.y <= 0)
			{
				force.y = 0 - this.y;
			}
			if(this.y + this.height >= stage.stageHeight)
			{
				force.y = (stage.stageHeight - (this.y + this.height));
			}
			return force;
		}
[/Source]
Thanks! RP.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.