www.jpct.net

jPCT-AE - a 3d engine for Android => Support => Topic started by: aerosolswe on October 19, 2012, 02:42:42 am

Title: Object3D rotation around another object
Post by: aerosolswe on October 19, 2012, 02:42:42 am
Hello.

So I am really new with jpct-ae (and only been programming for 1yr). I am currently working on my first app with it, so far loving it. But I've struggled with this task all night and I cant seem to get it work.

The game plays out in the solar system and atm I am making the planets orbit around the sun but I cant find a good way to do this. Tried some with sin and cos but nothing really worked.

So I am wondering if anyone else got some tips for me how I am suppose to do this.

Appreciate all the help and tips. Thanks.
Title: Re: Object3D rotation around another object
Post by: EgonOlsen on October 19, 2012, 07:17:17 am
You can set a dummy object (http://www.jpct.net/doc/com/threed/jpct/Object3D.html#createDummyObj() (http://www.jpct.net/doc/com/threed/jpct/Object3D.html#createDummyObj())) in the sun's center (or one for each planet), make it a child of the planet and rotate the dummy around Y to make the planet orbit the sun.
Title: Re: Object3D rotation around another object
Post by: aerosolswe on October 19, 2012, 02:26:24 pm
Thank you egon, I will have a go with that
Title: Re: Object3D rotation around another object
Post by: phmenard on September 08, 2013, 11:19:09 pm
I took your advice and its not working ... I do this in
onSurfaceChanged()

Code: [Select]
center = Object3D.createDummyObj();
center.translate(sol[0].getTransformedCenter());
center.build();

earth[0].translate(0, 50, 0);
earth[0].setTexture("texture");
earth[0].addChild(center);

and in onDrawFrame()

Code: [Select]
center.rotateY(1f);

but nothing happends ... whats am I doing wrong?? the sol and earth objects are loaded obj models.
Title: Re: Object3D rotation around another object
Post by: EgonOlsen on September 08, 2013, 11:23:30 pm
The earth should be the child of the center to make this work, not the other way.
Title: Re: Object3D rotation around another object
Post by: phmenard on September 09, 2013, 12:31:40 am
ok did that ... now the earth is just sitting there spinning rather that rotating around the center ... hmm ... any advice??
Title: Re: Object3D rotation around another object
Post by: EgonOlsen on September 09, 2013, 06:53:19 am
Sounds like as if you are rotating the earth now....create the center, maek earth a child of it and then rotate the center, not the earth.
Title: Re: Object3D rotation around another object
Post by: phmenard on September 10, 2013, 12:07:50 am
silly me ... works perfect ... just need to rotate on the z axis not the y. What's up with these dummy objects any way??? are they costly to have around?? ... example ... a model of our solar system would require 9 dummy objects to control the orbits of each planet given that they orbit and rotate at different speeds.0 ... would it be better to use some old school math???

kinda off topic but while I'm here ... is there a way to make an abject glow .. like a sun ... texture effect maybe??

thanks a bunch for your help EgonOlsen
Title: Re: Object3D rotation around another object
Post by: EgonOlsen on September 10, 2013, 08:44:59 am
... would it be better to use some old school math???
No. Dummy objects are cheap.

kinda off topic but while I'm here ... is there a way to make an abject glow .. like a sun ... texture effect maybe??
Maybe a glow texture with an alpha mask placed on a billboarded quad in the center of the sun will look good enough?
Title: Re: Object3D rotation around another object
Post by: phmenard on September 13, 2013, 06:29:41 pm
ok thanks ... before I end this thread what would be the best way to go about making an elliptical orbit rather than a circle given the fact most planets don't orbit in a perfect circle ... I playing around with code like this but I'm not able to get it right.

Code: [Select]

SimpleVector sv = new SimpleVector();
sv.x = (float) (myOrbit.getTransformedCenter().x + Math.sin(myOrbitAngle*Math.PI/180) * Math.cos(myOrbitAngle*Math.PI/180));
sv.y = (float) (myOrbit.getTransformedCenter().y  + Math.sin(myOrbitAngle*Math.PI/180));// * Math.sin(.5));
//sv.z = (float) (myOrbit.getTransformedCenter().z  + Math.cos(myOrbitAngle*Math.PI/180));


myOrbitAngle += myOrbitSpeed; 

myObject.translate(sv);

Title: Re: Object3D rotation around another object
Post by: EgonOlsen on September 13, 2013, 08:20:11 pm
Translations are cumulative. Try something like

Code: [Select]
myObject.clearTranslation();
myObject.translate(sv);

instead.
Title: Re: Object3D rotation around another object
Post by: phmenard on September 13, 2013, 08:42:56 pm
using the method myObject.clearTranslation(); results in the object not being drawn for some reason ... I have this ..

Code: [Select]
SimpleVector sv = new SimpleVector();
 
myOrbitAngle += myOrbitSpeed;
        if(myOrbitAngle > Math.PI * 2)
        myOrbitAngle %= Math.PI * 2;

sv.x = (float) ((Math.cos(myOrbitAngle)) + myOrbit.getTransformedCenter().x);
sv.y = (float) ((Math.sin(myOrbitAngle)) +  myOrbit.getTransformedCenter().y);
    sv.z =  myOrbit.getTransformedCenter().z;

    //myObject.clearTranslation();
    myObject.translate(sv);

the planet clearly orbits in an elliptical fashion ... but not around it orbit center(myOrbit) I'm missing something ...
Title: Re: Object3D rotation around another object
Post by: EgonOlsen on September 13, 2013, 09:15:16 pm
Shouldn't the sin and cos values being multiplied by some values? This way, your translation hovers around +-1 around the orbit's center!?
Title: Re: Object3D rotation around another object
Post by: phmenard on September 13, 2013, 09:56:19 pm
I thought and seen the same thing ... so now I have this ..

Code: [Select]
SimpleVector sv = new SimpleVector();
 
myOrbitAngle += myOrbitSpeed;
        if(myOrbitAngle > Math.PI * 2)
        myOrbitAngle %= Math.PI * 2;

sv.z = (float) ((Math.cos(myOrbitAngle * distanceFromParent)) + myOrbit.getTransformedCenter().z);
sv.x = (float) ((Math.sin(myOrbitAngle * distanceFromParent)) +  myOrbit.getTransformedCenter().x);
    sv.y =  myOrbit.getTransformedCenter().y;

    //myObject.clearTranslation();
    myObject.translate(sv);

but still off center ... I'll get it ... lol
Title: Re: Object3D rotation around another object
Post by: phmenard on September 15, 2013, 06:51:36 pm
why is it that the following code will not orbit the orbitCenter ... instead its starts off ok but then dives right into orbitCenter and continues to do so ...

Code: [Select]
SimpleVector sv = new SimpleVector();
 
myOrbitAngle += myOrbitSpeed;
        if(myOrbitAngle > Math.PI * 2)
        myOrbitAngle %= Math.PI * 2;

        //float rad = (float) (myOrbitAngle * (Math.PI / 180)); // Converting Degrees To Radians
        sv.x = (float) (orbitCenter.x + distanceFromParent * Math.sin(myOrbitAngle));
        sv.z = (float) (orbitCenter.z + distanceFromParent * Math.cos(myOrbitAngle));

   


Log.w("pos", sv.toString() );
myObject.translate(sv);

myObject.rotateY(myOrbitSpeed);
Title: Re: Object3D rotation around another object
Post by: EgonOlsen on September 15, 2013, 08:57:28 pm
You are still ignoring that translations are cumulative. Maybe your orbitCenter is so close to the origin that it doesn't matter, but it's not correct anyway. You are trying to calculate and absolute position in each iteration and you should treat it like one...but you don't...
Title: [Solved]
Post by: phmenard on September 15, 2013, 11:29:10 pm
I figured it out ... seems the + and - signs make a big difference when orbiting on the z axis ... so with that said here is the code if you want to make your own unique orbits ...

Code: [Select]

myOrbitAngle += myOrbitSpeed;
if(myOrbitAngle > Math.PI * 2)
   myOrbitAngle %= Math.PI * 2; 
   SimpleVector sv = new SimpleVector();

   //double rad = (myOrbitAngle * (Math.PI / 180)); // Converting Degrees To Radians
   double x = myOrbit.getTransformedCenter().x - distanceFromParent * (Math.cos(myOrbitAngle)* .5);
   double z = myOrbit.getTransformedCenter().z - distanceFromParent * (Math.sin(myOrbitAngle) * 1);
       
   sv.x = (float) x;
   sv.z = (float) z;
   sv.y = 0;

   myObject.clearTranslation();
   myObject.translate(sv);
   myObject.rotateY(myOrbitSpeed);

if you want to orbit on a different axis just change around the x,y,z cords as needed ... as I said this code orbits the z axis. The floating point and whole number after the cos and sin add the option of playing around with the orbit ... stretch it to elliptical ... widen it ect.

Have fun ... I know I am ... now if I could only get a blazing sun rather than a dull object giving off light :-)