Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, im doing an animation about the sun and moon etc. My problem occurs when 2 objects orbit eachother. Then i need the transparency to be different. One time object A passes in front of object B, so the color must be A, one time object A goes behind object b, then the color must be opposite. I am using the GD module by the way. Does anyone have a solution for this?Best regards

Replies are listed 'Best First'.
Re: color problem
by Corion (Patriarch) on Dec 29, 2013 at 18:31 UTC

    The simple approach is to draw the elements from back to front.

    The hard part is to find out which element is in the back and which is in the front, the "z-order" of the elements.

      Let's assume for a second i knew which object are in the back, even then, how do i need to draw this? I've added my code.

      use GD; use constant PI => 4 * atan2(1, 1); $im = new GD::Image(400,400); $black = $im->colorAllocate(0,0,0); $red=$im->colorAllocate(255,0,0); $green=$im->colorAllocate(0,255,0); $im->filledEllipse(200,200,50,50,$red); $im->filledEllipse(300,200,25,25,$green); for($xy=0;$xy<6.28;$xy+=0.01){ $x = 100*cos($xy); $y = (43.75/2)*sin($xy); push @punten,[200+$x,200+$y]; } $data =$im->gifanimbegin(1,0); $data.=$im->gifanimadd(1,0,0,1); for($z=0;$z<=$#punten;$z+=20){ $ima = new GD::Image(400,400); $black = $ima->colorAllocate(0,0,0); $red=$ima->colorAllocate(255,0,0); $green=$ima->colorAllocate(0,255,0); $ima->filledEllipse(200,200,50,50,$red); $xc = $punten[$z][0]; $yc = $punten[$z][1]; $ima->filledEllipse($xc,$yc,25,25,$green); $data.= $ima->gifanimadd(1,0,0,1); } $data.=$ima->gifanimend; binmode STDOUT; print $data;

        Instead of drawing $red and then drawing $green, you need to find out which of the two is in the back, and draw that one first.

        For example, do it like this:

        # Define the two planets my @large= ($red, 200,200,50,50 ); my @small= ($green, $xc, $yc, 25, 25 ); my( $front, $back ); if( $red_is_in_front_of_green ) { $front= \@large; $back= \@small; } else { $front= \@small; $back= \@large; }; # Just in case we ever go beyond two objects my @objects_to_draw= ($back, $front); for my $item (@objects_to_draw) { my( $color, @position )= @$item; $ima->filledEllipse( @position, $color ); };
Re: color problem
by locked_user sundialsvc4 (Abbot) on Dec 31, 2013 at 14:35 UTC
    There will be three points-of-interest in the geometry:
    • The position in space (and orientation) of the camera, and the dimensions of its viewport.
    • The position-in-space of the sun, the center of orbit.
    • The distance and angle (polar coordinates) of each planet, which may be converted to absolute (x,y,z).
    All of this math lets you determine, at any point in time, whether a given planet is visible to the camera, where it appears on the camera's film, and how far away from the camera it is. You then sort this display-list in descending order by z-order, and draw it.
Re: color problem
by Anonymous Monk on Jan 01, 2014 at 07:19 UTC

    Thank you for the explanations. I have allready made some progress with it.