Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
help with the arrays, or whatever
#1
you may remember my old post about how to fix a box from flickering or something, well now i want to redo that program where azz before, two dots randomly move about inside a box and if the dot reached the edge, it looped back around to the other side. well now with my new profound knowlge of arrays and the TYPE command thing i tryed to have 80 dots going at the same time. i think there's a problem, mainly 'cause it wont start (really?)
here, what did i do wrong so i can learn from my mistakes.
[syntax="QBASIC"]SCREEN 13
TYPE dottype
a AS INTEGER
b AS INTEGER
END TYPE
DIM dot(1 TO 80) AS dottype
FOR i = 1 TO 80
FOR ii = 1 TO 80
dottype(i).a = 40
dottype(ii).b = 40
NEXT: NEXT
RANDOMIZE TIMER
DO
LINE (19, 19)-(101, 101), 4, B

frld = 0
DO
frld = frld + 1
LOOP UNTIL frld = 2500
FOR i = 1 TO 80
FOR ii = 1 TO 80
RN = INT(RND * 4) + 1
IF RN = 1 THEN dottype(i).a = dottype(i).a - 1
IF RN = 3 THEN dottype(i).a = dottype(i).a + 1
IF RN = 2 THEN dottype(ii).b =dottype(ii).b + 1
IF RN = 4 THEN dottype(ii).b = dottype(ii).b - 1
NEXT: NEXT
FOR i = 1 TO 80
FOR ii = 1 TO 80
IF dottype(i).a < 20 THEN dottype(i).a = 100
IF dottype(i).a > 100 THEN dottype(i).a = 20
IF dottype(ii).b < 20 THEN dottype(ii).b = 100
IF dottype(ii).b > 100 THEN dottype(ii).b = 20
NEXT: NEXT
FOR i = 1 TO 80
FOR ii = 1 TO 80
PSET (dottype(i).a + 1, dottype(ii).b + 1), 9
PSET (dottype(i).a + 1, dottype(ii).b), 9
PSET (dottype(i).a, dottype(ii).b + 1), 9
PSET (dottype(i).a, dottype(ii).b), 9
NEXT: NEXT
LOOP UNTIL INKEY$ = "q"[/syntax]
quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Reply
#2
You could make this process a lot easier and clearer... I see you've tried to make a vector type, but why did you call the components 'a' and 'b'? Use 'x' and 'y' for clarity:

[syntax="QBASIC"]
TYPE Vec2D
x AS INTEGER
y AS INTEGER
END TYPE
[/syntax]

Also, it would be a lot simpler to make the dot locations relative; so to be in the top-left hand side of the box, a dot would have the location (0, 0). The advantage of this is that you can use the MOD operator to do automatic clipping (when a dot goes out of the box, it automatically returns to the beginning).

Rather than hardcoding "there is a box here that the particles are bound to", I think it would be better to do something like this:

[syntax="QBASIC"]
TYPE Box
p AS Vec2D ' top-left corner position vector
d AS Vec2D ' dimension vector
END TYPE
[/syntax]

By doing this, you will eventually be able to do some neat stuff... I'll show you a bit later on in this post.

Create an array of dots:

[syntax="QBASIC"]
DIM dots(1 TO 80) AS Vec2D
[/syntax]

Now, for each update, try something like this:

[syntax="QBASIC"]
DIM container AS Box

' set up information about the box.
' the box will be at (0, 0) and have the dimensions
' (200, 100).
box.p.x = 0
box.p.y = 0
box.d.x = 200
box.d.y = 100

DO
CLS ' or equivalent (read further in the post)

' draw the bounding box
LINE (container.p.x, container.p.y)-(container.p.x + container.d.x - 1, container.pos.y + container.d.y - 1), 4, B

' iterate through each particle and update/draw it
FOR i% = 1 TO 80

' calculate the random x increment
movX% = INT(RND * 2)
IF movX% = 0 THEN movX% = -1

' calculate the random y increment
movY% = INT(RND * 2)
IF movY% = 0 THEN movY% = -1

' add these to the vector (i.e. move the particle)
dots(i%).x = dots(i%).x + movX%
dots(i%).y = dots(i%).y + movY%

' do bounds-checking; first check for negative positions
IF dots(i%).x < 0 THEN dots(i%).x = container.d.x
IF dots(i%).y < 0 THEN dots(i%).y = container.d.y

' next use MOD to clip the x/y positions.
dots(i%).x = dots(i%).x MOD container.d.x
dots(i%).y = dots(i%).y MOD container.d.y

' finally, draw the dot relative to the box position vector.
PSET (container.p.x + dots(i%).x, container.p.y + dots(i%).y), 15

NEXT i%
LOOP
[/syntax]


By making the positions of each of the dots relative to the position of the box, rather than storing absolute positions (i.e. screen co-ordinates), you can move the box around the screen and the dots will automatically be in the correct location. A pretty cool demonstration of this working would be to have the box bouncing around the screen. Big Grin

With your current code, if you wanted to move the box from (0, 0) to (5, 5), you would have had to update every single particle's position to the new location. With this code, you just have to set the box's ('container') position.

In that code (which is untested, by the way... I haven't coded in QB for years, so sorry if it doesn't quite work! the theory is correct though. Smile), I've used CLS. You say that you posted on these forums before a while ago about flickering... the best way to reduce flickering is to use double-buffering. If you don't know what that is, you should try and find a tutorial about it (there are plenty around). The basic idea is that you keep an area of system memory the same size as the video memory (e.g. in screen mode 13h, this is 320x200 = 64000 bytes). You then write to that buffer when you're plotting pixels. After you've drawn all that you want, the PUT command is used to quickly blit the surface to the screen. This totally eliminates flicker.

What's the purpose of this, by the way? At first I thought maybe you're trying to simulate Brownian motion, but that can't be the case if the dots move from one end of the box to another. Smile

Another point, which doesn't really matter much, is that your code will run differently on different-speed machines... so you should treat velocities as "pixels-per-second", really. In each update, you calculate the number of seconds elapsed since the last update (using TIMER), then multiply this by the velocity of the particles. You probably won't care about that though, if you're just doing this program for fun. Smile

Hope this helps out a bit. As I said before, this code is untested... sorry if it doesn't run straight-off (of course, I've missed out a lot of code, like initialising the screen and dot information Wink). The theory of the points that I'm trying to make are valid though.


-shiftLynx
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply
#3
I guess a good question to ask is: What does "vector" meen?
remember, im very new to this concept much less qbasic.
quote="whitetiger0990"]whitetiger is.. WHITE POWER!!! [/quote]
Here
Reply
#4
A vector is a mathematics concept that consists of an ordered list of numbers. In physics, this is often used to represent velocity or acceleration. A 2d vector consists of an x and a y component; with velocity, these mean "change in x" and "change in y".
Reply
#5
Quote:I guess a good question to ask is: What does "vector" meen?
remember, im very new to this concept much less qbasic.

A vector is a set of related numbers. For example, if you're describing the location of a dot on a piece of paper, you can't just use 1 number. You can use two numbers though - choose an origin (like the top-left corner of the page) and measure in centimetres horizontally to the dot and vertically to the dot. Now you can describe the "location" as two numbers: x and y.

Vectors don't necessarily have to be in two dimensions -- 3 dimensional vectors can be used to describe a position in 3D space, or velocity or acceleration in 3D space. You don't need to worry about that though. Smile

A vector representing velocity is particularly useful -- you can say that the velocity of a ball is (3, 5). That is, each second, the ball moves 3 metres horizontally and 5 metres vertically. Another way of saying that is that the ball moves with a speed of 5.83 metres per second (ms^-1) -- but in which direction? You could tell an angle that it's moving at as well, but then you're moving away from Cartesian co-ordinates and into polar co-ordinates. Seeing as the screen is easier described in Cartesian co-ordinates, you might as well stick with them (it's also a lot faster).

Erm... I think I've strayed from the point a little (excuse the pun).


-shiftLynx
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)