Quirky draw method activity
-
I have implemented the draw method in a script so that it draws a grid. As I orbit around, the vertical lines redraw quite nicely. But the horizontal lines sort of crawl off the screen. I tried to set it up so that each line gets drawn from the pervious line's location, but add 20 units to the y value. But everytime the draw method is called again, it doesn't reset the original position. It seems to pick up where it left off, even though I have tried to reset the y values. I've cut the code down significantly so it still works incorrectly, but I took out the unneccesary bits if anyone cares to look at it. Hopefully it will just be something ebarrasingly simple wrong with my code.... Thanks in advance,
@line_positions
is an array of 3dpositions on the x-axisdef draw( view ) if !@line_positions.empty? @line_positions.each do |e| p1 = e p2 = p1.clone p2[1] += 100 view.draw GL_LINES, p1, p2 end np1 = @line_positions[0] np2 = @line_positions.last puts "from draw " + @line_positions[0].y.to_f.to_s 5.times do np1[1] = np1[1] + 20 np2[1] = np2[1] + 20 view.draw GL_LINES, np1, np2 end end end
Chris
-
and a quick video to show better how th horizontal lines crawl away....
[flash=696,601:25d1gl6d]http://www.chrisfullmer.com/chrisfullmer/forums/grid_crawling_away.swf[/flash:25d1gl6d]
Chris
-
p2[1] += 100
shouldn't it be
p2[1] = p2[1] + 100
as you do with...
np2[1] = np2[1] + 20
???
-
@tig said:
p2[1] += 100
shouldn't it be
p2[1] = p2[1] + 100
as you do with...
np2[1] = np2[1] + 20
???
p2[1] += 100
andp2[1] = p2[1] + 100
doe the exact same thing. -
Chris, I can't quite work out what your code do. Which draw method draws the horizontal, and which draws the vertical?
And where in the code have you tried to reset the Y position?
-
Duh !
@unknownuser said:
p2[1] += 100
andp2[1] = p2[1] + 100
do the exact same thing.Very true... Sorry I misread the first version as "
p2[1] + 100
"... but copied it correctly - I have poorer eyesight that I thought...
. -
Does
view.draw_line(point1,point2)
give the same result ?
Is
@line_positions
changing dynamically as you view ? How is it set ? -
@unknownuser said:
np1 = @line_positions.clone[0]
np2 = @line_positions.last.cloneDoes this cloning help ?
-
ok, so writing all that tou, and thinking about the line:
@chris said:
All I can think is that is it possible that it thinks that since np1 points to @line_positions[0] when I change np1 it thinks I want the original @line_positions[0] value changed too and somehow sends the change back to that array?
And sure enough, here's what is happening I believe.
The @line_positions is holding an array of 3dpositions. So np1 was not holding an identical array of integers, but it was holding the same 3dpoint object. So inchanging that object in the draw method was changing it everywhere it exists - @line_positions for example.
Si I changed the lines:
np1 = @line_positions[0] np2 = @line_positions.last
to
np1 = @line_positions[0].clone np2 = @line_positions.last.clone
And that made it all better! Thanks again,
Chris
EDIT: TIG beat me to it! Yes, that is exactly what it needed. Thanks!
-
EDIT: Issue resolved in the 2 posts below this. Skip this post unless you're really interested in boring yourself to death.
OK, its morning and I'm back!
@line_positions is an array of points on the x axis. Each point acts as the base starting point for each vertical grid line. So first the method checks to see if @line_positions has been populated yet. If it has, then it takes each value from that array and calls it p1 and then adds an amount to the y of 100 and calls that p1, then draws the line from p1 (the base) to p2, the top of the vertical line.
The 5.times do portion is drawing the horizontal lines by taking the first and last points on the horizontal boundaries. Then adds 20 to each and draws a line. Does that 5 times, and those are the horizontal grid lines.
To explain better,
@line_positions
is defined once when the user hits enter. Its values do not change at all normally. To test this, I added aputs @line_positions[0]
statement in my onKeyUp method. So everytime I press a key, it puts the first @line_positions position. And sure enough, when I disable the draw method, that value stays the same.But once I enable the draw method, that value changes everytime the screen is redrawn. So it appears that somehow something that I have in the draw method is changing the @line_positions, not just getting values from it.
More specifically, when I comment out the 5.times portion (which is the part that draws the horizontal grids), then the method works as expected and does not change my @line_positions values. So I think it is something with how I have that written, but I can't see anything in there that should affect the @line_positions values.
def draw( view ) if !@line_positions.empty? @line_positions.each do |e| p1 = e p2 = p1.clone p2[1] += 100 view.draw GL_LINES, p1, p2 end np1 = @line_positions[0] np2 = @line_positions.last puts "from draw " + @line_positions[0].y.to_f.to_s #5.times do # np1[1] = np1[1] + 20 # np2[1] = np2[1] + 20 # view.draw GL_LINES, np1, np2 #end end end
All I can think is that is it possible that it thinks that since np1 points to @line_positions[0] when I change np1 it thinks I want the original @line_positions[0] value changed too and somehow sends the change back to that array? I'm a bit stumped with this one....
Chris
Advertisement