Skip to main content

JAVA3D and RMI (Moving sprites with RMI not working)

No replies
jibbylala
Offline
Joined: 2010-11-16
Points: 0

Dear experts,
My intention was to create RMI version of Networked Tour3D
example in the 3d book here:http://fivedots.coe.psu.ac.th/~ad/jg/ch21/index.html.
which is about moving sprites on multiple client and navigate together through virtual world simultaneously but i m failing.
i refactor the class Sprite3d to get the Transform3d from remote object on server so when all clients get the same transform 3d all change their position hence "Network tour of sprites in virtual worlds' can be achieved .
i m beginner programmer and i dived into these advanced things and now i have no idea, why it is not working may be i m doing some silly mistake. I don't know what to do and now the code is much i can't paste all on some forum.
this is code in which the methods are calling remote but sprites are NOT moving simulataneously :
this is class which is getting the tranform3d from server other class on client site are calling this class for getting the objectgroup or transformgroup
public class Sprite3DClient
{
private static final long serialVersionUID = 1L;
private final static double OBS_FACTOR = 0.5;
// used to reduce radius of bounding sphere around Sprite
// when testing for intersection with obstacles

private BranchGroup objectBG; // for adding/removing from scene
private TransformGroup objectTG; // TG which the loaded object hangs off
private MySerTransform3D t3d, toMove, toRot; // for manipulating objectTG's transform
private Switch visSwitch; // to make object visible/invisible

private double radius;
private boolean isActive; // is the sprite active?
//private double currRotation;

private Obstacles obs; // obstacles in the way of an object
private DecimalFormat df; // for simpler output during debugging

private String userName, fnm;
private Sprite3D SSp ;
// private Sprite3D Tbob = null;

public Sprite3DClient(String userName, String fnm, Obstacles obs) throws RemoteException
{
try {
SSp = (Sprite3D) Naming.lookup("rmi://localhost/Sprite3D" );
} catch (Exception e) {
e.printStackTrace();
}
df = new DecimalFormat("0.###"); // 3 dp
this.obs = obs;

// load object and coords
PropManager propMan = new PropManager(fnm, true);
radius = propMan.getScale(); // assume radius == scale
// System.out.println("radius: " + df.format(radius));

// create switch for visibility
visSwitch = new Switch();
visSwitch.setCapability(Switch.ALLOW_SWITCH_WRITE);
visSwitch.addChild( propMan.getTG() ); // add object to switch
visSwitch.setWhichChild( Switch.CHILD_ALL ); // make visible

// create a new transform group for the object
objectTG = new TransformGroup();
objectTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objectTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objectTG.addChild( visSwitch );

// t3d = getT3d();
// toMove = getToMove();
// toRot = getToRot();

t3d = new MySerTransform3D();
toMove = new MySerTransform3D();
toRot = new MySerTransform3D();
isActive = true;
} // end of Sprite3D()

public String getUserName() throws RemoteException {
return userName;
}

public String getFnm() throws RemoteException {
return fnm;
}

public Obstacles getObs() throws RemoteException {
return obs;
}

public void setUserName(String username) throws RemoteException {
this.userName= username;
}

public void setFnm(String fnm1) throws RemoteException {
this.fnm= fnm1;
}

public void setObs(Obstacles obs) throws RemoteException {
this.obs= obs;
}

public BranchGroup getBG()
{ return objectBG; }

public void detach()
// remove this sprite from the scene
{ objectBG.detach(); }

public TransformGroup getTG()
{ return objectTG; }

public void setPosition(double xPos, double zPos)throws RemoteException
{
Point3d currLoc = getCurrLoc();
double xMove = xPos - currLoc.x;
double zMove = zPos - currLoc.z;
moveBy(xMove, zMove);
} // end of setPosition()

public boolean moveBy(double x, double z) throws RemoteException
/* Move the sprite by offsets x and z, but only if within the floor
and there is no obstacle nearby. */
{
if (isActive()) {
Point3d nextLoc = tryMove(new Vector3d(x, 0, z));
if (obs.nearObstacle(nextLoc, radius*OBS_FACTOR))
return false;
else {
doMove( new Vector3d(x, 0, z) ); // inefficient recalc
return true;
}
}
else { // not active
System.out.println("In Sprite3DClient moveBy");
return false;}

} // end of moveBy()

private void doMove(Vector3d theMove) throws RemoteException
// Move the sprite by the amount in theMove
{
objectTG.getTransform( t3d);
SSp.doMove( theMove);
objectTG.setTransform(t3d);
System.out.println("In Sprite3DClient doMove");
} // end of doMove()

private Point3d tryMove(Vector3d theMove)throws RemoteException
/* Calculate the effect of the given translation but
do not update the sprite's position until it's been
tested.
*/
{
objectTG.getTransform( t3d );
return SSp.tryMove( theMove);
} // end of tryMove()

public void doRotateY(double radians)throws RemoteException
// Rotate the sprite by radians amount around its y-axis
{
objectTG.getTransform( t3d );
SSp.doRotateY( radians);

objectTG.setTransform(t3d);
SSp.setCR(radians);
} // end of doRotateY()

public Point3d getCurrLoc()throws RemoteException
{
Point3d p3 = null;
if (objectTG != null) {
objectTG.getTransform(t3d);
p3 =SSp.getCurrLoc();
} else
System.err.println("Sprite3D is null");
return p3;
} // end of getCurrLoc()

public MySerTransform3D getT3d() throws RemoteException {
if (SSp != null) {
return (MySerTransform3D) SSp.getT3d();
}
else
System.err.println("SSp is null");
return null;
}

public boolean isActive() throws RemoteException
{ return isActive; }

public void setActive(boolean b)throws RemoteException
// Activity changes affect the sprite's visibility
{ isActive = b;
if (!isActive)
visSwitch.setWhichChild( Switch.CHILD_NONE ); // make invisible
else if (isActive)
visSwitch.setWhichChild( Switch.CHILD_ALL ); // make visible
} // end of setActive()

private void printTuple(Tuple3d t, String id)throws RemoteException
// used for debugging
{
System.out.println(id + " x: " + df.format(t.x) +
", " + id + " y: " + df.format(t.y) +
", " + id + " z: " + df.format(t.z));
} // end of printTuple()
} // end of Sprite3D class

this is remote class from where i m getting the transform3d:

package ServerSite;

// Sprite3D.java
// Andrew Davison, April 2005, ad@fivedots.coe.psu.ac.th

/* Sprite3D loads a 3D image, placing it at (0,0,0).
We assume that the object's actual position is (0,0) in the XZ plane.
The Y position will vary but probably the base of the object is
touching the XZ plane.

Movements are restricted to the XZ plane and rotations
around the Y-axis.

An object cannot move off the floor, or travel through obstacles
(as defined in the Obstacles object).

Very sinmilar to Sprite3D in Tour3D.
New net-related code:
- a userName above the sprite that stays oriented along +z axis;
- return a detachable branchgroup for the scene;
- store the current rotation around the y-axis, and methods to return
and set it.
*/

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.text.DecimalFormat;
import java.util.Enumeration;
import java.util.Vector;
import javax.media.j3d.*;
import javax.vecmath.*;

public class Sprite3DImpl extends UnicastRemoteObject implements Sprite3D
{

private static final long serialVersionUID = 1L;
private MySerTransform3D t3d, toMove, toRot; // for manipulating objectTG's transform
private double currRotation;

private DecimalFormat df; // for simpler output during debugging

public Sprite3DImpl()throws RemoteException {
super();
t3d = new MySerTransform3D();
toMove = new MySerTransform3D();
toRot = new MySerTransform3D();

df = new DecimalFormat("0.###"); // 3 dp
currRotation = 0.0;
System.err.println("In remote constructor");
}

public void doMove(Vector3d theMove)throws RemoteException
// // Move the sprite by the amount in theMove
{
toMove.setTranslation(theMove); // overwrite previous trans
t3d.mul(toMove);
System.err.println("In remote doMove");
} // end of doMove()

public Point3d tryMove(Vector3d theMove)throws RemoteException
/* Calculate the effect of the given translation but
do not update the sprite's position until it's been
tested.
*/
{
toMove.setTranslation(theMove);
t3d.mul(toMove);
Vector3d trans = new Vector3d();
t3d.get( trans );
// printTuple(trans, "nextLoc");
System.err.println("In remote tryMove");
return new Point3d( trans.x, trans.y, trans.z);

} // end of tryMove()

public void doRotateY(double radians)throws RemoteException
// Rotate the sprite by radians amount around its y-axis
{
toRot.rotY(radians); // overwrite previous rotation
t3d.mul(toRot);
System.err.println("In remote doRotateY");
} // end of doRotateY()

public void setCR(double radians)throws RemoteException
{
currRotation += radians;
} // end of setCurrRotation()

public Point3d getCurrLoc()throws RemoteException
{
Vector3d trans = new Vector3d();
t3d.get( trans );
System.err.println("In remote getCurrLoc");
// printTuple(trans, "currLoc");
return new Point3d( trans.x, trans.y, trans.z);
}
public void setCurrRotation(double rot)throws RemoteException
{
double rotChange = rot - currRotation;
doRotateY(rotChange);
System.err.println("In remote setCurrRotation");
} // end of setCurrRotation()

public double getCurrRotation()throws RemoteException
{ System.err.println("In remote getCurrRotation");
return currRotation; }

private void setT3d(MySerTransform3D t3d)throws RemoteException {
this.t3d = t3d;
}
public MySerTransform3D getT3d() throws RemoteException{
if(t3d!=null){
return t3d;}
else
System.err.println("t3d is null");
return t3d;
}
private void setToMove(MySerTransform3D toMove) throws RemoteException{
this.toMove = toMove;
}
public MySerTransform3D getToMove()throws RemoteException {
return toMove;
}
private void setToRot(MySerTransform3D toRot)throws RemoteException {
this.toRot = toRot;
}

public MySerTransform3D getToRot()throws RemoteException {
return toRot;
}

private void printTuple(Tuple3d t, String id)throws RemoteException
// used for debugging
{
System.out.println(id + " x: " + df.format(t.x) +
", " + id + " y: " + df.format(t.y) +
", " + id + " z: " + df.format(t.z));
} // end of printTuple()
} // end of Sprite3D class

Other class are also using like behavior class and WrapNetTour3D according to changed design because of RMI may be there is some issue in those but first i would like to confirm and have some review on these main classes and why it is not working and how it can be implemented with RMI.
If somebody need i can send the whole project because i just wanted that working and i thought i did the major changes and spent lot of time on it.

please help i would be really thankful
jibby lala