We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › how to extend a java interface
Page Index Toggle Pages: 1
how to extend a java interface? (Read 606 times)
how to extend a java interface?
Feb 19th, 2010, 6:47am
 
Hi guys,

I'm trying to implement a work with Box2D. And in order to implement collision detection, as far as i could search on web, I have to create a class that extends ContactListener.

The problem is that Processing is complaining that ContactListener must be a class. And after exploring the Box2D's docs, I saw that ContactListener is, in fact, a 'interface' not a class.

Ok... that explains why Processign was complaining. But... do not solve my problem... Smiley

Well, I'm not a Java guru, but I tried do do some thing:

Code:

class MyCollisions implements ContactListener{

 ArrayList contacts;
 
 MyCollisions(){
 }
 
 void persist(ContactPoint point){
   println("Persist Contact");
 }
   
 void Add(ContactPoint point){
   println("COLLISION ----------------------------------");
   println("shape1: "+point.shape1);
   println("shape2: "+point.shape2);
   println("separation: "+point.separation);
   println("position: "+point.position);
 }
 

 void remove(ContactPoint point){
   println("Remove Contact");
 }
 
 void result(ContactPoint point){
   println("Result Contact");
 }
}


Ok... now Processing seems to be ok with the 'implements' task. Smiley
But now... a new problem!

Processing complains:
Code:
The type box_00.Colisoes must implement the inherited abstract method ContactListener.persist(ContactPoint) 



now... i am stuck! As you can see... I do implement all the methods from ContactListener interface. Unless there is some secret way to implement sucha methods, diferent from what i did.

Does anyone knows how can I implement a java interface?

thanks in advance,
Paulo
P.S.: down here is the ContactListener code:
Code:

/*
* JBox2D - A Java Port of Erin Catto's Box2D
[...] */

package org.jbox2d.dynamics;

import org.jbox2d.dynamics.contacts.ContactPoint;
import org.jbox2d.dynamics.contacts.ContactResult;

public interface ContactListener {

/**
* Called when a contact point is added. This includes the geometry
* and the forces.
*/
public void add(ContactPoint point);

/**
* Called when a contact point persists. This includes the geometry
* and the forces.
*/
public void persist(ContactPoint point);

/**
* Called when a contact point is removed. This includes the last
* computed geometry and forces.
*/
public void remove(ContactPoint point);

public void result(ContactResult point);

}

Re: how to extend a java interface?
Reply #1 - Feb 19th, 2010, 1:34pm
 
You are doing it almost right, except you should have an error on add(), not on persist()... because you wrote it Add().
See also thread: Jbox2d, ContactListener in processing

This motivated me to complete an unfinished ContactListener sample sketch, an initial test used:
Code:
public class HitHandler implements ContactListener
{
HitHandler()
{
}

/**
* Called when a contact point is added, ie. when colliding.
* This includes the geometry and the forces.
*/
public void add(ContactPoint cp)
{
println("Ouch!");
DumpContactPoint(cp);
}

/**
* Called when a contact point persists, ie. contact is maintained.
* This includes the geometry and the forces.
*/
public void persist(ContactPoint cp)
{
println("Sticky, eh?");
DumpContactPoint(cp);
}

/**
* Called when a contact point is removed, ie. contact is broken.
* This includes the last computed geometry and forces.
*/
public void remove(ContactPoint cp)
{
println("Good riddance!");
DumpContactPoint(cp);
}

/**
*
*/
public void result(ContactResult cr)
{
DumpContactResult(cr);
}
}

void DumpContactResult(ContactResult cr)
{
println("ContactResult: " + cr + " (" + cr.id.features.incidentEdge + ")");
println("- Shapes: " + cr.shape1 + ", " + cr.shape2);
println("- Position: " + cr.position);
println("- Normal: " + cr.normal + " -> " + cr.normalImpulse + " / " + cr.tangentImpulse);
}

void DumpContactPoint(ContactPoint cp)
{
println("ContactPoint: " + cp + " (" + cp.id.features.incidentEdge + ")");
println("- Shapes: " + cp.shape1 + ", " + cp.shape2);
println("- Position: " + cp.position);
println("- Velocity: " + cp.velocity);
println("- Normal: " + cp.normal);
println("- Separation: " + cp.separation);
println("- Friction / Restitution: " + cp.friction + " / " + cp.restitution);
}
Re: how to extend a java interface?
Reply #2 - Feb 19th, 2010, 1:55pm
 
You are implementing it the right way. There is a mistake, though :
result(ContactResult r)

Code:
class MyCollisions implements ContactListener {

ArrayList contacts;

MyCollisions() {}

void add(ContactPoint p) {}
void result(ContactResult r) {}
void remove(ContactPoint p) {}
void persist(ContactPoint p) {}

}
Page Index Toggle Pages: 1