Loading...
Processing Forum
Recent Topics
All Forums
Screen name:
aevol
aevol's Profile
1
Posts
1
Responses
0
Followers
Activity Trend
Last 30 days
Last 30 days
Date Interval
From Date :
To Date :
Go
Loading Chart...
Posts
Responses
PM
Show:
All
Discussions
Questions
Expanded view
List view
Private Message
Issue while extending Fisica Fjoint class
[3 Replies]
25-Aug-2013 04:24 PM
Forum:
Contributed Library Questions
Hi there,
I would like to extend the Fjoint class of the Fisica library to make a new object I would call Flink.
Actually I just want it to draw a line when the distance between the two Fbody involved is less than the value of the m_vision field.
My problem is that nothing happens. It looks like the draw() method and the debugDraw method are not called.
I've done a lot of exploration of the code of the library and based my class extension to the FDistanceJoint class but I still cannot have any result.
The sketch work and there is no error or stack trace but I simply don't have my links !
Here is the class, ( nearly a copy of FDistanceJoint ) :
import processing.core.*;
import java.util.ArrayList;
import org.jbox2d.common.*;
import org.jbox2d.collision.*;
import org.jbox2d.collision.shapes.*;
import org.jbox2d.dynamics.*;
import org.jbox2d.dynamics.joints.*;
/////////////////////////////////////////////////////
public class aeFlink extends FJoint {
protected FBody m_body1;
protected FBody m_body2;
protected Vec2 m_anchor1;
protected Vec2 m_anchor2;
protected float m_vision;
/////////////////////////////////////////////////////
public aeFlink (FBody body1, FBody body2) {
super() ;
m_body1 = body1;
m_body2 = body2;
m_anchor1 = new Vec2(0.0f, 0.0f);
m_anchor2 = new Vec2(0.0f, 0.0f);
m_vision = 150;
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
public void draw(PGraphics applet) {
preDraw(applet);
println("test"); //->nothing print...
if (getDistance() < m_vision) {
applet.line(getAnchor1X(), getAnchor1Y(), getAnchor2X(), getAnchor2Y());
} //nothing draw...
postDraw(applet);
}
////////////////////////////////////////////////////////////////
public void drawDebug(PGraphics applet) {
preDrawDebug(applet);
applet.line(getAnchor1X(), getAnchor1Y(), getAnchor2X(), getAnchor2Y());
postDrawDebug(applet);
}
////////////////////////////////////////////////////////////////
public float getDistance() {
float lengthX = ((m_body1.getX() + getAnchor1X()) - (m_body2.getX() + getAnchor2X()));
float lengthY = ((m_body1.getY() + getAnchor1Y()) - (m_body2.getY() + getAnchor2Y()));
float distance = (float)Math.sqrt(lengthX*lengthX + lengthY*lengthY);
return distance;
}
////////////////////////////////////////////////////////////////
public float getAnchor1X() {
if (m_joint != null) {
return Fisica.worldToScreen(m_joint.getAnchor1()).x;
}
return Fisica.worldToScreen(m_anchor1.x);
}
////////////////////////////////////////////////////////////////
public float getAnchor1Y() {
if (m_joint != null) {
return Fisica.worldToScreen(m_joint.getAnchor1()).y;
}
return Fisica.worldToScreen(m_anchor1.y);
}
////////////////////////////////////////////////////////////////
public float getAnchor2X() {
if (m_joint != null) {
return Fisica.worldToScreen(m_joint.getAnchor2()).x;
}
return Fisica.worldToScreen(m_anchor2.x);
}
////////////////////////////////////////////////////////////////
public float getAnchor2Y() {
if (m_joint != null) {
return Fisica.worldToScreen(m_joint.getAnchor2()).y;
}
return Fisica.worldToScreen(m_anchor2.y);
}
////////////////////////////////////////////////////////////////
public void setAnchor2(float x, float y) {
if (m_joint != null) {
((DistanceJoint)m_joint).getAnchor2().set(Fisica.screenToWorld(x), Fisica.screenToWorld(y));
}
m_anchor2 = Fisica.screenToWorld(x, y);
}
////////////////////////////////////////////////////////////////
public void setAnchor1(float x, float y) {
if (m_joint != null) {
((DistanceJoint)m_joint).getAnchor1().set(Fisica.screenToWorld(x), Fisica.screenToWorld(y));
}
m_anchor1 = Fisica.screenToWorld(x, y);
}
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//end class aeFlink
}
And this is the sketch to see how it is instantiated :
import fisica.*;
FWorld world;
FCircle[] balls;
aeFlink[] links;
int nombreDeBalles = 10;
float boundXL = 5;
float boundXR = 700-5;
float boundYT = 5;
float boundYB = 700-5;
color backColor = 40;
int radius = 22;
//////////////////////////////////////////////////////////
void setup() {
size(1000, 700);
smooth();
frameRate(60);
Fisica.init(this);
world = new FWorld();
world.setGravity(0, 0);
world.setEdges(boundXL, boundYT, boundXR, boundYB );
world.setEdgesRestitution(1);
world.setEdges(int(backColor));
balls = new FCircle[nombreDeBalles];
for (int i = 0; i<nombreDeBalles; i++) {
//int d = int(random (100, 200));
int d = 20;
balls[i] = new FCircle(radius);
balls[i].setPosition(random(100, 500), random(100, 500));
//balls[i].setVelocity(random(-200, 200), random(-200, 200));
balls[i].setDensity(1+d);
balls[i].setRestitution(1.0);
balls[i].setDamping(0.0001);
balls[i].setStroke(0);
balls[i].setStrokeWeight(1);
balls[i].setFill(235 - d);
//balls[i].setSensor(true);
balls[i].setFriction(0);
world.add(balls[i]);
}
links = new aeFlink[nombreDeBalles];
for (int i = 0; i<nombreDeBalles; i++) {
links[i] = new aeFlink(balls[i], balls[(i+1)%nombreDeBalles]);
links[i].setStroke(255);
links[i].setStrokeWeight(1);
links[i].setCollideConnected(true);
links[i].setDrawable(true);
world.add(links[i]);
}
//end setup
}
//////////////////////////////////////////////////////////
void draw() {
background(backColor);
world.draw();
world.step();
noFill();
stroke(255);
rect(boundXL+4, boundYT+4, boundXR-boundXL-8, boundXR-boundXL-8, 5);
//end draw
}
So.... any idea ?
«Prev
Next »
Moderate user : aevol
Forum