Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
question on determining time spent in area
#1
My goal in this program I am attempting to write is to tally up how much time the ball spends inside of the square area I have set aside in the screen.  I thought I would be able to subtract the time it exited from the time it entered the square to get the time spent inside.  But it seems that the constant update of the variable being used to hold result of the 'TIMER' command into is constantly updating due to the loop, so I can't seem to figure out how to get a single value for the time the circle's center actually crossed into the green square.  I was thinking something like endTime! - beginTime! would give me the number of seconds that the circle spent in the square.  


 
Code: (Select All)
CONST BALLRADIUS = 30
DIM ballx! 'ball x coordinate
DIM bally! 'ball y coordinate
DIM ballvecx! 'ball x vector
DIM ballvecy! 'ball y vector

SCREEN _NEWIMAGE(400, 400, 32)
ballx! = 50
bally! = 50
ballvecx! = .25
ballvecy! = .50



DO
    _LIMIT 60
    CLS
    LINE (100, 300)-(300, 100), _RGB32(0, 300, 0), BF

    ballx! = ballx! + ballvecx!
    bally! = bally! + ballvecy!
    IF ballx! > 370 OR ballx! < 30 THEN
        ballvecx! = -ballvecx!
    END IF
    IF bally! > 370 OR bally! < 30 THEN
        ballvecy! = -ballvecy!
    END IF
    IF bally! > 100 AND bally! < 300 AND ballx! > 100 AND ballx! < 300 THEN
        PRINT TIMER
    END IF

    CIRCLE (ballx!, bally!), BALLRADIUS
    PAINT (ballx!, bally!), _RGB32(255, 255, 255)
    _DISPLAY
LOOP UNTIL _KEYDOWN(27)
SYSTEM
Reply
#2
Maybe this is what you are looking for?

Code: (Select All)
Const BALLRADIUS = 30
Dim ballx! 'ball x coordinate
Dim bally! 'ball y coordinate
Dim ballvecx! 'ball x vector
Dim ballvecy! 'ball y vector

Screen _NewImage(400, 400, 32)
ballx! = 50
bally! = 50
ballvecx! = .25
ballvecy! = .50



Do
    _Limit 60
    Cls
    Line (100, 300)-(300, 100), _RGB32(0, 300, 0), BF
    Locate 25, 1: Print "Timer"; Timer;: If z2 Then Print "Time spent in square"; z2;
    ballx! = ballx! + ballvecx!
    bally! = bally! + ballvecy!
    If ballx! > 370 Or ballx! < 30 Then
        ballvecx! = -ballvecx!
    End If
    If bally! > 370 Or bally! < 30 Then
        ballvecy! = -ballvecy!
    End If
    If bally! > 100 And bally! < 300 And ballx! > 100 And ballx! < 300 Then
        If z1 = 0 Then z1 = Timer
    Else
        If z1 Then z2 = Timer - z1: z1 = 0
    End If
    Circle (ballx!, bally!), BALLRADIUS
    Paint (ballx!, bally!), _RGB32(255, 255, 255)
    _Display
Loop Until _KeyDown(27)
System
Reply
#3
I did it this way:
Code: (Select All)
Const BR = 30 'BALLRADIUS = 30
Dim ballx! 'ball x coordinate
Dim bally! 'ball y coordinate
Dim ballvecx! 'ball x vector
Dim ballvecy! 'ball y vector

Screen _NewImage(400, 400, 32)
ballx! = 50
bally! = 50
ballvecx! = .25
ballvecy! = .50



Do
    Cls
    Print "Last Time:"; z2 / 60; "secs"
    Print "Curr Time:"; z1 / 60; "secs"
    Line (100, 300)-(300, 100), _RGB32(0, 300, 0), BF
    ballx! = ballx! + ballvecx!
    bally! = bally! + ballvecy!
    If ballx! > 370 Or ballx! < 30 Then
        ballvecx! = -ballvecx!
    End If
    If bally! > 370 Or bally! < 30 Then
        ballvecy! = -ballvecy!
    End If
    If bally! - BR > 100 And bally! + BR < 300 And ballx! - BR > 100 And ballx! + BR < 300 Then
        z1 = z1 + 1
    Else
        If z1 <> 0 Then z2 = z1: z1 = 0
    End If
    Circle (ballx!, bally!), BR
    Paint (ballx!, bally!), _RGB32(255, 255, 255)
    _Display
    _Limit 60
Loop Until _KeyDown(27)
System

Note: Screw the Timer, _Limit 60 = loop cycling 60 times per sec so count loops in square and divide by 60 for secs in the square.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#4
OK, where are the z1 and z2 variables coming from?  Are they some sort of naming conventions?
Reply
#5
z1 is loop counter when the entire circle is inside box
z2 is z1 before z1 is switched back to 0 because circle is outside of box.
so z2 was the count/time the last time the circle was in the box.
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#6
(08-22-2025, 12:58 AM)bplus Wrote: z1 is loop counter when the entire circle is inside box
z2 is z1 before z1 is switched back to 0 because circle is outside of box.
so z2 was the count/time the last time the circle was in the box.

I was slapping myself in the face earlier because I had thought to myself why I didn't just change the code to display z1 and z2 and perhaps figured it out myself.  I'm going to let this marinate on my brain for a bit because what the two of you did with the z variables has brought me back to an issue I've always had trouble with.  Before I ask about it though, I'm going to try and make sense of it myself this time using my tool here in front of me.  I appreciate what y'all have showed me.
Reply
#7
Quit the slapping, give youself more time to marinate. Smile
  724  855  599  923  575  468  400  206  147  564  878  823  652  556 bxor cross forever
Reply
#8
z1 and z2 are universal variables restricted to timing functions only... that's just because Mark and I use them that way, of course! Why that lazy ASCII Rho hasn't documented them, by now, to the wiki is beyond me. Big Grin

Mark and I are just using the same names for variables here, but using two different approaches. Mine is based on z1 starting the time measurement when the ball is enters the box, while z2 gives us the time spent in the box once z1 has been activated (becomes non-zero). Mark (bplus) is basing his method on the qb64 page display cycle function by setting it to _limit 60. Clever!

Pete
Reply
#9
Here's a really simple example of this, that you can test just using your mouse so that you can control things to your satisfaction:

Code: (Select All)
$Color:32
Screen _NewImage(640, 480, 32)
Line (120, 100)-(480, 380), White, B
PCopy 0, 1
Do
PCopy 1, 0
Locate 1, 1: Print Using "Time in box: ###.####### "; timein
While _MouseInput: Wend
If _MouseX > 120 And _MouseX < 480 And _MouseY > 100 And _MouseY < 380 Then
'we're inside the box
If timestart = 0 Then timestart = Timer(0.001) 'start the timer
Else
timestart = 0 'stop and reset the timer
End If
timein = 0 'default to no time in the box
If timestart > 0 Then
timein = Timer(0.001) - timestart 'and calculate how long if we're in the box
End If
_Limit 30
_Display
Loop Until _MouseButton(1) Or _KeyHit

When it first moves into the area, set a variable to the time.
If that variable is non-zero, then you know to calculate the time in that area from then on out.
Once it moves out of the area, reset that variable back to 0. If you want to keep that time inside the box; just store it in a variable and don't reset it as I've done here.

That's all there is to it. Wink
Reply
#10
Thanks for the clarifications everybody.  I believe the marinated brain has now been spared from the frying pan.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  _Putimage Question. Pete 11 671 01-04-2026, 09:33 PM
Last Post: Pete
  Nice simple, I hope, question Mad Axeman 4 335 12-20-2025, 09:28 PM
Last Post: SMcNeill
  NewBie Question niteflyer 2 325 11-06-2025, 07:11 PM
Last Post: Petr
  A Question About _MAPTRIANGLE Magdha 2 373 11-02-2025, 11:37 AM
Last Post: Magdha
  '$include: 'file.tmr' Timing Question pmackay 2 427 10-16-2025, 12:20 PM
Last Post: a740g

Forum Jump:


Users browsing this thread: