Of course it does that, you set the velocity so it won’t be anything else, I thought that was the point, having it rotate from collisions but stay on its orbit:
Course not ! I am just not following you as I would like to do.
I started to understand the code you sent, but then I get lost…
My understanding of rotating a vector is to mult it, so.
Here, my last offer, take it or leave it, I’m out.
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.FastMath;
import com.jme3.math.Quaternion;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import java.util.Random;
/**
* test
*
* @author normenhansen
*/
public class Main extends SimpleApplication implements PhysicsTickListener {
RigidBodyControl rigid;
float dist = 5f;
Vector3f rotVec = new Vector3f(0, dist, 0);
Vector3f planetPos = new Vector3f(0,-2,0);
Random rnd = new Random(System.currentTimeMillis());
float speed = 1;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
Box b = new Box(1, 1, 1);
Geometry geom = new Geometry("Box", b);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Blue);
geom.setMaterial(mat);
BulletAppState bullet = new BulletAppState();
stateManager.attach(bullet);
bullet.getPhysicsSpace().addTickListener(this);
rigid = new RigidBodyControl(1);
geom.addControl(rigid);
bullet.getPhysicsSpace().add(rigid);
// find actual start position
Vector3f startPos = planetPos.add(rotVec);
// put physics object to start position
rigid.setPhysicsLocation(startPos);
rootNode.attachChild(geom);
}
@Override
public void simpleUpdate(float tpf) {
//TODO: add update code
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
public void prePhysicsTick(PhysicsSpace space, float tpf) {
Quaternion rotQuat = new Quaternion().fromAngleAxis(speed * tpf, Vector3f.UNIT_Z);
// rotate vector a bit for new position
rotQuat.multLocal(rotVec);
// find direction to new position
Vector3f direction = planetPos.add(rotVec).subtract(rigid.getPhysicsLocation());
direction.normalizeLocal();
// needed speed depends on circle size (2*Pi*radius) and the speed you rotate the vector
direction.multLocal(speed * tpf * (2f * FastMath.PI * dist));
rigid.setLinearVelocity(direction);
}
public void physicsTick(PhysicsSpace space, float tpf) {
//simulate things bouncing into the object, causing it to rotate (but not go out of orbit)
if(rnd.nextFloat()>0.7f)
rigid.applyTorque(new Vector3f(rnd.nextFloat(),rnd.nextFloat(),rnd.nextFloat()));
}
}
Edit: updated code to include a distance (from planet) parameter
Thank you for your code Normen.
I tested and it is working.
But it is still not what I need…
I am looking more for a joint simulation like the Distance Joint point out by @RatKod.
I need the object to complete free move around the planet.
Its a pitty that Jm3 and bullet dont have this functionality, it improves a lot the realist of scenes where you use it.
I am going to try to do some solution myself using the angle aprouch : http://postimg.org/image/s4v0xpm67/
If I come along with a solution for this I will point out here.
Also if someone can help or has a solution I will be glad to hear !
So if you just want it to always keep the same distance to the planet (but not the same orbit) just add forces that push it back to that distance when its not (which is basically what a joint would do too). I won’t go step by step through that with you again though. Especially as we can still just guess what exactly you want to do.
I dont think its so easy. Every time I apply force to the object with out considering angles it just fix the object spinning at some point. It moves if you apply forces but dosent fell natural.
The maths to build such thing seems to be quite complex :
You are right, its not what I need, I dont have a ground to break the impulse, but it was good to look at your code thought.
I have read the bullet source code, its using this complex Jacobians equations for all joints and constraints.
With the forces I am using the objects never stop to bounce in / out of the orbit, I was thinking to reduce the force using the distance as parameter, but I am not sure it will work.
Do you have any idea or should I go straight to the Jacobians ?