/** * Anchors and the bridge * * by Ricard Marxer * * This example shows the use of anchors and distance joints in order * to create a bridge. */ import fisica.*; int WND_WIDTH = 400; int WND_HEIGHT = 400; enum PhyObject_t { PHYOBJECT_NONE, PHYOBJECT_CIRCLE, PHYOBJECT_BOX, }; /* enum 縺御スソ縺医↑縺��縺ァ縲∽サ」逕ィ*/ final static int PHYOBJECT_NONE = 0; final static int PHYOBJECT_CIRCLE = 1; final static int PHYOBJECT_BOX = 2; final static int PHYOBJECT_TRIANGLE = 3; int curPhyObject = PHYOBJECT_NONE; void setup() { size(400,400); fw_init(); app_init(); } /* 繧「繝励Μ繧ア繝シ繧キ繝ァ繝ウ縺ョ蛻晄悄蛹�*/ void app_init() { gui_init(); phy_init(); } /* 繧「繝励Μ繧ア繝シ繧キ繝ァ繝ウ縺ョ謠冗判 */ void app_render() { background(255); phy_updateAndRender( true ); app_renderObjectCreating(); } /* 迴セ蝨ィ邱ィ髮�クュ縺ョ繧ェ繝悶ず繧ァ繧ッ繝医r謠冗判縺吶k */ void app_renderObjectCreating() { if( !isStartSizing ) { return ; } switch( curPhyObject ) { case PHYOBJECT_NONE: /* 迚ケ縺ォ菴輔b縺励↑縺�*/ break; case PHYOBJECT_CIRCLE:/**/ { float cx = (endPoint.x + startPoint.x)/2; float cy = (endPoint.y + startPoint.y)/2; float dx = (endPoint.x - startPoint.x); float dy = (endPoint.y - startPoint.y); float r = sqrt( dx*dx+dy*dy ); noFill(); stroke(204, 102, 0); ellipse(cx, cy, r, r); } break; case PHYOBJECT_BOX: { float sx = startPoint.x; float sy = startPoint.y; float w = (endPoint.x - startPoint.x); float h = (endPoint.y - startPoint.y); noFill(); stroke(204, 102, 0); rect(sx,sy,w,h); } break; case PHYOBJECT_TRIANGLE: { float sx = startPoint.x; float sy = startPoint.y; float w = (endPoint.x - startPoint.x); float h = (endPoint.y - startPoint.y); noFill(); stroke(204, 102, 0); triangle(sx, sy, sx, sy+h, sx+w, sy+h ); } break; default: print("UnKownObjectType"); break; } } /* 迴セ蝨ィ縺ョ繧ェ繝悶ず繧ァ繧ッ繝医Δ繝シ繝峨r繧オ繝シ繧ッ繝ォ縺ォ縺吶k */ void OnChangeCreateShape_Circle() { curPhyObject = PHYOBJECT_CIRCLE; phy_draggable( false ); print("Change Circle"); } /* 迴セ蝨ィ縺ョ繧ェ繝悶ず繧ァ繧ッ繝医Δ繝シ繝峨r繝懊ャ繧ッ繧ケ縺ォ縺吶k */ void OnChangeCreateShape_Box() { curPhyObject = PHYOBJECT_BOX; phy_draggable( false ); print("Change Box"); } void OnChangeCreateShape_Triangle() { curPhyObject = PHYOBJECT_TRIANGLE; phy_draggable( false ); print("Change Triangle"); } PVector startPoint = new PVector(); PVector endPoint = new PVector(); boolean isStartSizing = false; /* 繝槭え繧ケ縺梧款縺輔l縺溘i縲√◎縺薙r襍キ轤ケ縺ォ邱壹r蠑輔″蟋九a繧�*/ void mousePressed() { /* TODO 菴懈�繝「繝シ繝�*/ statrtCreateObjOnmousePressed_(); /* 繧ェ繝悶ず繧ァ繧ッ繝医′縺ゅk縺ェ繧峨ラ繝ゥ繝�げ繝「繝シ繝峨↓縺吶k */ if( phy_ishitobj(mouseX, mouseY ) ) { phy_draggable( true ); } else { phy_draggable( false ); } phy_switchStaticAt( mouseX, mouseY ); } void statrtCreateObjOnmousePressed_() { /* GUI荳翫↓縺ゅk縺九�繧ェ繝悶ず繧ァ繧ッ繝医′荳九↓縺ゅk縺ェ繧峨�繧ェ繝悶ず繧ァ繧ッ繝医�逕滓�縺ッ縺励↑縺�*/ if( gui_isOverGUI( mouseX, mouseY ) || phy_ishitobj(mouseX, mouseY ) ) { return; } isStartSizing = true; startPoint.x = mouseX; startPoint.y = mouseY; } /* 繝槭え繧ケ縺後�縺励°繧九∋縺榊�謇�〒謚シ縺輔l縲√�繧ヲ繧ケ縺悟虚縺�◆繧峨�縺昴�繧オ繧、繧コ縺ォ荳�凾逧�↑繧ェ繝悶ず繧ァ繧ッ繝医r陦ィ遉コ縺吶k */ void mouseDragged() { if( !isStartSizing ) { return ; } /* 蟷��縲�縺ョ邏ッ荵励↓縺ェ繧九h縺�↓隱ソ謨エ縺吶k */ int x = (int)startPoint.x - (int)mouseX; int y = (int)startPoint.y - (int)mouseY; endPoint.x = startPoint.x-x; endPoint.y = startPoint.y-y; } /* 縺昴�謨ー繧定カ�∴縺ェ縺�怙螟ァ縺ョ2縺ョ邏ッ荵励r霑斐☆ */ int powerof2nize_( int v ) { boolean isMinus = false; if( v < 0.0f ) { isMinus = true; v = -v; } for( int i=2;i<20;++i) { int p1 = (int)Math.pow( 2, i ); int p2 = (int)Math.pow( 2, i+1 ); if( p1 < v && v < p2 ) { if( isMinus ) { return -p1; } else { return p1; } } } return 4; } /* 繝槭え繧ケ縺後�縺励°繧九∋縺榊�謇�〒謚シ縺輔l縲√�繧ヲ繧ケ縺梧叛縺輔l縺溘i縺昴�繧オ繧、繧コ縺ョ繧ェ繝悶ず繧ァ繧ッ繝医r關ス荳九&縺帙k */ void mouseReleased() { if( !isStartSizing ) { return ; } isStartSizing = false; switch( curPhyObject ) { case PHYOBJECT_NONE: /* 迚ケ縺ォ菴輔b縺励↑縺�*/ break; case PHYOBJECT_CIRCLE: { int cx = (int)(endPoint.x + startPoint.x)/2; int cy = (int)(endPoint.y + startPoint.y)/2; float dx = (endPoint.x - startPoint.x); float dy = (endPoint.y - startPoint.y); float r = sqrt( dx*dx+dy*dy ); /* 蜊雁セ�′荳�ョ壻サ・荳翫□縺ィ逕滓�縺輔l繧�*/ if(r > 20.0f) { AddCircle(cx, cy, r ); } } break; case PHYOBJECT_BOX: { int sx = (int)startPoint.x; int sy = (int)startPoint.y; int w = (int)(endPoint.x - startPoint.x); int h = (int)(endPoint.y - startPoint.y); if(w<0) { w = -w; sx = sx - w; } if(h<0) { h = -h; sy = sy - h; } if( w>50.0f && h > 50.0f ) { AddBox(sx, sy, w, h, 0.0f ); } } break; case PHYOBJECT_TRIANGLE: { int sx = (int)startPoint.x; int sy = (int)startPoint.y; int w = (int)(endPoint.x - startPoint.x); int h = (int)(endPoint.y - startPoint.y); AddTriangle(sx, sy, w, h, 0.0f ); } break; default: print("UnKownObjectType"); break; } }