I'm writing a simple solar system using Three.js
. At this time I have added 3 planets, every planet is rotating on its axis. At the middle I have added the sun.
- For every planet I create a different group (
mesh
); - All the planets are in a main group (
Object3D
);
I can:
- rotate every planet on its axis (using planet object/group);
- rotate every planets on theirs orbits (using main object/group).
The problem is that when I make a rotation in the main group of for example 1
, every planet is moving by 1. For example:
If I rotate a planet by 180 degrees, every planet is rotating by 180 degrees on theirs orbits. So how can I rotate every planet with a different speed?
I suppose that don't need the main group and I must write an algorithm and use it for every planet, but I don't know how algorithms like that works. Can somebody help me?
Important parts of code:
... main loop:
function loop() {
jQuery.each(planets, function( key, value ) {
value.rotation.y += 0.005;
value.rotation.x += 0.005;
});
group.rotation.z -= 0.005;
requestAnimationFrame( loop );
}
... adding a planet:
var data = {
0: {
texture: "images/telos.png",
radius: 10,
segments: 20
},
1: {
texture: "images/ako.png",
radius: 8,
segments: 20
},
2: {
texture: "images/alba.png",
radius: 21,
segments: 20
}
};
var planets = {}
jQuery.each(data, function( key, value ) {
var planet = creator.planet({
texture: value.texture,
radius: value.radius,
segments: value.segments
});
planet.position.set( key * 60, 0, 0 );
planets[ key ] = planet;
group.add( planets[ key ] );
});
Any tips?
data
variable – Vikram