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

I'm currently working on a top down space shooter engine and was trying to compress my plane animation script At the moment I have:

glBegin(GL_POLYGON); @pos=($_[0],$_[1]); my $rot = $_[2]*0.0174; my @point1=($pos[0],$pos[1]); my @point2=($point1[0]+(cos($rot+90)*10),$point1[1]+(sin($rot+90)*10)) +; my @point3=($point2[0]+(cos($rot+45)*6),$point2[1]+(sin($rot+45)*6)); my @point4=($point3[0]+(cos($rot)*4),$point3[1]+(sin($rot)*4)); my @point5=($point4[0]-(cos($rot+225)*8),$point4[1]-(sin($rot+225)*8)) +; glVertex2f($point1[0],$point1[1]); glVertex2f($point2[0],$point2[1]); glVertex2f($point3[0],$point3[1]); glVertex2f($point4[0],$point4[1]); glVertex2f($point5[0],$point5[1]);

Which I replaced with :

my @ang=(0,90,45,0,225); my @dist=(0,10,6,4,8); my $rot = $_[2]*0.0174; my $points=[]; my @prev=($_[0],$_[1]); glBegin(GL_POLYGON); for ($posi =1,$posi<5,$posi++){ @prev=($prev[0]+(cos($rot+$ang[$posi])*$dist[$posi]),$prev[1]+(sin($ +rot+$ang[$posi])*$dist[$posi])); $points[$posi]=($prev[0],$prev[1]); glVertex2f($prev[0],$prev[1]); }
However I'm getting no display at all yet no errors. Could someone give me a bump in the right direction?

Replies are listed 'Best First'.
Re: Data not acting as expected.
by mbethke (Hermit) on Mar 23, 2012 at 03:17 UTC
    Will you be using $points later? If so, this is likely to be the error. It seems like you're not running under use strict so the fact that you expect $points to be an arrayref but actually you're writing into @points isn't caught. There is another error in the assignment, as each $points[$posi] element will have ($prev[0],$prev1) evaluated in scalar context, yielding the second element only. I'd try:
    for $posi (1 .. 4) { @prev = ($prev[0] + cos($rot+$ang[$posi]) * $dist[$posi], $prev[1] + sin($rot+$ang[$posi]) * $dist[$posi]); $points->[$posi] = [ @prev ]; glVertex2f(@prev); }
    Edit: copied the same mistake I explained just before m(
Re: Data not acting as expected.
by CountZero (Bishop) on Mar 23, 2012 at 07:01 UTC
    A more general comment. sin and cos take as their argument an angle in radians not degrees. You do make the correct degrees-to-radians conversion to the initial value of $rot but later adding 45, 90 or 225 seems somewhat strange.

    Also if you need to (re-)calculate these trigonometric values many times, it can speed up your program when you pre-calculate these values and save them in a array and then simply reference these arrays.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics