-
Any FOAM experts?
I've got a handful of Circles (stationary) and a handful of triangles - and one 'gravity well' - basically a circle with a ton of mass that I'm re-positioning every step (so it doesnt 'drift').
The problem is that when my triangles bump into a circle, it kind of flips out - the triangles jump in and out of the circle until it figures out how to right itself...anyone have any ideas?
-
if you post the code i'll probably be able to help
-
This is the first time I've used it so I'm still figuring out how to do some things (like making things stationary when they are picking up force from collisions?)...any help would be greatly appreciated.
PHP Code:
package com {
/*
* Flash 9.0 :: ActionScript 3.0
*/
//--------------------------------------
// PACKAGES
//--------------------------------------
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.events.Event;
import flash.utils.Dictionary;
import org.generalrelativity.foam.Foam;
import org.generalrelativity.foam.dynamics.element.body.RigidBody;
import org.generalrelativity.foam.dynamics.enum.Simplification;
import org.generalrelativity.foam.dynamics.element.body.Circle;
import org.generalrelativity.foam.dynamics.element.particle.*;
import org.generalrelativity.foam.dynamics.force.*;
import org.generalrelativity.foam.dynamics.element.ISimulatable;
import org.generalrelativity.foam.dynamics.element.body.RigidBody;
import org.generalrelativity.foam.dynamics.enum.Simplification;
import org.generalrelativity.foam.util.ShapeUtil;
import org.generalrelativity.foam.dynamics.force.Gravity;
import org.generalrelativity.foam.math.Vector;
import org.generalrelativity.foam.dynamics.force.Friction;
import org.generalrelativity.foam.dynamics.force.spring.RigidBodyBungee;
import org.generalrelativity.foam.view.Renderable;
import org.generalrelativity.foam.dynamics.element.body.Circle;
import example.orbit.force.GravitationalForceGenerator;
import example.rocket.view.DisplayObjectFoamRenderer;
import example.rocket.view.DisplayObjectData;
public final class GravTest extends Sprite {
//--------------------------------------
// CLASS CONSTANTS
//--------------------------------------
private const SHIPS:Array = new Array();
private const LEVELS:Array = new Array();
private const MASS_BY_LEVEL:Dictionary = new Dictionary();
private const FOAM:Foam = new Foam();
//--------------------------------------
// CONSTRUCTOR
//--------------------------------------
public function GravTest(){
super();
addEventListener(Event.ADDED_TO_STAGE, function(e:Event):void{
e.target.removeEventListener(e.type, arguments.callee);
init();
});
}
//--------------------------------------
// PRIVATE VARIABLES
//--------------------------------------
private var m:Circle;
private var _currentPlanet:Sprite;
private var resFlag:Boolean = true;
//--------------------------------------
// GETTER/SETTERS
//--------------------------------------
//--------------------------------------
// EVENT HANDLERS
//--------------------------------------
private function newShip(i:int):void{
SHIPS[i] = addShip(0,0);
}
private function onFoamStep(e:Event):void{
var i:int = SHIPS.length;
var tempFlag:Boolean = false;
// loop through all ships
while(i){
i--;
var r:RigidBody = SHIPS[i];
// if first spawned, clear inertia
if(resFlag) r.vx = r.vy = r.av = 0;
// always point toward grav well (this is still kind of broken)
r.q = Math.atan2(_currentPlanet.y - r.y, _currentPlanet.x - r.x);
// if close to grav well, delete and spawn new ship
if(r.x - m.x <= 3 && r.y - m.y <= 3){
// replace this with some kind of collision event handler???
FOAM.removeElement(r);
SHIPS[i] = addShip(0,0);
}
}
// clear spawn flag
resFlag = false;
// keep grav well from moving around
// is there a way to do this automatically ???
m.x = _currentPlanet.x;
m.y = _currentPlanet.y;
m.clearTorque();
}
//--------------------------------------
// PRIVATE & PROTECTED INSTANCE METHODS
//--------------------------------------
private function init(e:Event = null):void{
LEVELS.push(part1);
LEVELS.push(part2);
LEVELS.push(part3);
LEVELS.push(part4);
LEVELS.push(part5);
LEVELS.push(part6);
LEVELS.push(part7);
LEVELS.push(part8);
LEVELS.push(part9);
LEVELS.push(part10);
LEVELS.push(part11);
LEVELS.push(part12);
LEVELS.push(part13);
LEVELS.push(part14);
LEVELS.push(part15);
LEVELS.push(part16);
LEVELS.push(part17);
LEVELS.push(part18);
_currentPlanet = part9;
FOAM.setRenderer( new DisplayObjectFoamRenderer() );
initPhysics();
}
private function initPhysics():void{
addChild(FOAM);
swapChildren(FOAM, planets);
FOAM.solverIterations = 2;
// build masses underneath the 'part' graphics on stage
for each(var p:Sprite in LEVELS){
var mass:Circle = new Circle(p.x, p.y, (p.width * .55), Simplification.INFINITE_MASS, 0, 0, .5, .15, 0, 0);
MASS_BY_LEVEL[p] = mass;
p.alpha = 1;
p.visible = false;
FOAM.addElement(mass, true, false);
}
// build gravity well
m = new Circle(20, 20, 10, 1000);
m.x = 100;
m.y = 300;
FOAM.addElement(m, false, false);
// apply grav to objects after this point in time...ie. ships only
FOAM.addGlobalForceGenerator(new GravitationalForceGenerator(m), false);
// fill SHIPS array with new ships
for(var i:int = 0; i < 20; i++){
SHIPS.push(addShip(21 * i, 40));
}
FOAM.addEventListener( Foam.STEP, onFoamStep);
FOAM.simulate();
}
private function addShip(xx:Number, yy:Number):RigidBody{
var ship:DisplayObject = new Greenie();
var shape:RigidBody = new RigidBody(xx, yy, 100, [new Vector(-1, -5), new Vector(-1,5), new Vector(10,0)]);
// start off from one of the planets
var departure:Sprite = LEVELS[4];
shape.x = departure.x + Math.random() * 10 - 5;
shape.y = departure.y + Math.random() * 10 - 5;
shape.q = Math.atan2(_currentPlanet.y - shape.y, _currentPlanet.x - shape.x);
FOAM.addElement(shape, true, true, new DisplayObjectData(ship));
return(shape);
}
}
}
-
sorry this took me a while to get back to you. the problem (i think) comes from you performing a positional update (setting r.q in your FOAM step handler):
PHP Code:
// always point toward grav well (this is still kind of broken) r.q = Math.atan2(_currentPlanet.y - r.y, _currentPlanet.x - r.x);
when there are forces inducing torsion, this will counteract them and create a jittering effect when not complementary. basically, you're breaking newton's laws! soooo, i'd suggest instead, creating an IForceGenerator that applies a force to turn your ship toward the current planet, instead of setting the orientation explicitly. this way, a collision will respond to these forces instead of "fighting" with rotation update.
if you comment out that line of code does it fix the jittering?
Last edited by newblack; 07-20-2008 at 02:37 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|