-
-
Notifications
You must be signed in to change notification settings - Fork 83
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from constants to accessible variables (AbstractPhysicsBody & AbstractCollisionBody) #292
Conversation
@JNightRide would you be able to provide a few concrete scenarios where this proposal would help you? For example, "I want to be able to assign these final members so I can perform a copy" or "I want to do this so that I can serialize/deserialize the object" etc. Knowing the use-case(s) for this change would help me play the pros/cons game. That said, at a high level, marking things final provide the following benefits:
|
Hi @wnbittle. Thanks for reviewing this PR. What I'm trying to do is clone physical bodies, make an exact copy of them; However, currently properties cannot be cloned correctly (cloning is only partially done), which is bad. I'm trying to serialize dyn4j objects for the JME engine (it's a personal project). Releasing these properties can give access to object cloning support ( import java.util.ArrayList;
import java.util.List;
public class TestBody extends AbstractPhysicsBody implements Cloneable {
public TestBody() {
}
@Override
public TestBody clone() {
try {
TestBody clon = (TestBody) super.clone();
clon.fixtures = (List<BodyFixture>) ((ArrayList<BodyFixture>) fixtures).clone();
clon.forces = (List<Force>) ((ArrayList<Force>) forces).clone();
clon.transform = transform.copy();
// For all attributes to make a new copy by cloning...
return clon;
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
} This results in a new object identical to the original; Of course, the BodyFixture should be cloned anyway, but as far as I know, those objects are not a problem. If you want, you can visit this repository to see what I intend to do (repo) |
Imagine dyn4j had a For the cloning use-case, I'm proposing every (within reason) class implement the custom interface |
I think in this case the question would be; the properties would be
Another way (possibly) is serialization, since we have to rebuild the object in its initial (original) state.
The |
I just realized that there is an open issue about cloning support, it is possible that this PR could contribute to this issue since porting the |
Thank you for the additional detail. I was doing some testing with using the
The serialization/deserialization scenario is a tough one. There are some members you don't want serialized (or simply can't be) and some members that don't have anything to serialize except for what class is being used. I messed around with serialization/deserialization in the Sandbox application that was built long ago that could be used as a reference, but I wouldn't do it that way again. The point is, that you should be able to rebuild the simulation without access to the protected/final methods (I think there are some issues with this in 5.x though), but there isn't a native dyn4j way to do that. So to summarize, I don't think // don't do this
// clon.transform = transform.copy();
// do this
clon.transform.set(transform);
// don't do this
// clon.fixtures = (List<BodyFixture>) ((ArrayList<BodyFixture>) fixtures).clone();
// do something like this
for (T fixture : this.fixtures) {
BodyFixture bf = new ...;
bf.setXXX(...)
bf.setYYY(...)
clon.fixtures.add(bf);
} Sure, it's more code, but it solves some key issues with dyn4j supporting natively. I'd love to support what I call "Copy Constructors", but there's an issue there as well when copying fixtures. Next steps: I want to try a few more things, but at this point, I don't think dyn4j will be able to support a native Copy feature or |
For now I will leave this PR (possibly close it)...
This part is where 'Cloneable' does the magic, the JVM is responsible for making an exact copy of the object (it doesn't matter if it is abstract) you will have a copy of said object. What this code snippet does is add the same object back to the list; when you clone an object, you only clone the object itself; Its properties don't, so you have to be careful not to affect the original object. for (T fixture : this.fixtures) {
BodyFixture bf = new ...;
bf.setXXX(...)
bf.setYYY(...)
clon.fixtures.add(bf);
}
I am very interested in this topic, I would like to help; I'm trying things out on my end and hope to present a cloning possibility. |
Hello everyone.
This PR modifies the properties of type
final
so that those who inherit from classAbstractCollisionBody | AbstractPhysicsBody
can modify it (have full access to it).Having the lists as
final
greatly limits the classes that inherit from these objects since we cannot instantiate new objects for those properties... I think it is better to have them accessible so that users can modify them if they consider it necessary.What do you think of this proposal?