Author Topic: sin (need to use in a loop for speed adjustment  (Read 2182 times)

0 Members and 1 Guest are viewing this topic.

Offline PMACKAY

  • Forum Regular
  • Posts: 188
  • LIFE is Temporary
    • View Profile
sin (need to use in a loop for speed adjustment
« on: September 05, 2018, 03:36:38 am »
for i=1 to 55

???? sin(i) = 0.01, 0.02, 0.03 up to 0.08

next i

then i need a reverse of above

for i=55 to 1 step -1

???? sin(i) = 0.08, 0.07, 0.06 back to 0.01

next i

at various stages it needs to chang (the further from 55 then faster it gets  .eg _delay sin(i)  which is in range of 0 = 0.01 to 55 = 0.08    (gravity thing or stretch rubber band

pretty much needs to look like this

  <----------------------------------------------------------------------------------------------------------/   then reverse back to 0.01 so center of loop is slowest and furthest fastest
  0.01                      0.02                  0.03             0.04         0.05         0.06      0.07    0.08


thanks guys
MackyWhite

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: sin (need to use in a loop for speed adjustment
« Reply #1 on: September 05, 2018, 09:51:34 am »
Hi.
 
Maybe this?

i = .01

j! = _SINH(i)

PRINT SIN(j!)

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: sin (need to use in a loop for speed adjustment
« Reply #2 on: September 05, 2018, 10:14:30 am »
OK the sin(x) function is at max when x = pi/2 here you want the max value to be .08.

Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. 'when i = 1 to 55
  4. 'want a = 0 to Pi/2
  5. 'k*55 = pi/2
  6. k = _PI(1 / 2) * (1 / 55) '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< here is k for speed equation
  7. 'at i = 55 want .08 speed
  8. 'so
  9. 'speed = .08 * SIN(k * i) '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< here is equation
  10.  
  11. 'demo  and test plot
  12. LINE (0, 100)-STEP(800, 0)
  13. FOR i = 55 * 3 TO 4 * 55 * 3 STEP 55 * 3
  14.     LINE (i, 0)-STEP(0, 600)
  15.     _PRINTSTRING (i, 10), STR$(INT(i / 3))
  16.  
  17. FOR i = 1 TO 220 'full cycle
  18.     PSET (i * 3, 100 - .08 * SIN(k * i) * 500) 'test equation by plot of values magnified for human edification
  19.     IF i MOD 5 = 0 THEN
  20.         _PRINTSTRING (i * 3 - 12, 600 - .08 * SIN(k * i) * 6000), STR$(INT(.08 * SIN(k * i) * 100) / 100)
  21.     END IF
  22.  


« Last Edit: September 05, 2018, 10:15:58 am by bplus »

Offline PMACKAY

  • Forum Regular
  • Posts: 188
  • LIFE is Temporary
    • View Profile
Re: sin (need to use in a loop for speed adjustment
« Reply #3 on: September 06, 2018, 02:46:57 am »
Im not at home, it looks easy butsome unknown reason i cannot understand how to get it  into my code.... it keeps return 0 or goes into an error

Speed = _pi(1/2)*1/110
I have for k=1 to 110
Print 0.8 * sin (speed*k)
Next k
But only prints 0

It must be something im missing or a bug. I think more something im missing

So if i change 0.8 to 0.08 would this also work

Its a speed routine

Thank you b+   
MackyWhite

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: sin (need to use in a loop for speed adjustment
« Reply #4 on: September 06, 2018, 05:36:20 am »
You've probably using DEFINT A-Z or DEFLNG A-Z in your code.  Make certain to DIM the variables involved As SINGLE/DOUBLE/FLOAT.
« Last Edit: September 06, 2018, 10:35:18 am by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: sin (need to use in a loop for speed adjustment
« Reply #5 on: September 06, 2018, 09:40:13 am »
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. 'when i = 1 to 55
  4. 'want a = 0 to Pi/2
  5. 'k*55 = pi/2
  6. k! = _PI(1 / 2) * (1 / 55) '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< here is k for speed equation
  7. 'at i = 55 want .08 speed
  8. 'so
  9. 'speed = .08 * SIN(k! * i%) '<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< here is equation
  10.  
  11. WHILE _KEYDOWN(27) = 0 'press escape to quite
  12.     i% = i% + 1
  13.     i% = i% MOD 110 'this keeps i between 0 and 110
  14.  
  15.     speed! = INT(.08 * SIN(k! * i%) * 100) / 100 '<<< this rounds to 100th's so can follow numbers
  16.  
  17.     'in code for speed use this
  18.     'speed! = .08 * SIN(k! * i%)
  19.  
  20.     PRINT speed!; ", ";
  21.     _LIMIT 5

I am wondering if you are working on a gravity like thing. Where there is change of speed there are forces at play and illustrating those could be effected by time but also by lengths or distance traveled over a set period of time like a loop with a limit which might be less jerky.

« Last Edit: September 06, 2018, 10:40:41 am by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: sin (need to use in a loop for speed adjustment
« Reply #6 on: September 06, 2018, 11:53:16 am »
speed up, speed down: Hey! no fancy sin function ;)
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(1200, 600, 32)
  2.  
  3. 'using single precsion default type
  4. ox = 0: oy = 100: ow = 100: oh = 400: direction = 1: force = .1: dx = 0
  5. WHILE _KEYDOWN(27) = 0
  6.     CLS
  7.     IF ox < 550 THEN dx = dx + force
  8.     IF ox >= 550 THEN dx = dx - force
  9.     IF ox < 0 THEN ox = 0: dx = 0
  10.     IF ox > 1100 THEN ox = 1100: dx = 0
  11.     ox = ox + dx
  12.     LINE (ox, oy)-STEP(ow, oh), , BF
  13.     LOCATE 1, 1: PRINT INT(dx)
  14.     _DISPLAY
  15.     _LIMIT 60
  16.  

Hey this is cool accident trying to make above code work right:
Code: QB64: [Select]
  1. SCREEN _NEWIMAGE(800, 600, 32)
  2.  
  3. 'using single precsion default type
  4. ox = 0: oy = 100: ow = 100: oh = 400: direction = 1: force = .1: dx = 1
  5. WHILE _KEYDOWN(27) = 0
  6.     CLS
  7.     dx = dx + direction * force 'the change of the change of x is constantly changing
  8.     ox = ox + dx
  9.     IF ox > 700 THEN ox = 699: direction = direction * -1: dx = -dx
  10.     IF ox < 0 THEN ox = 1: direction = 1: dx = -dx
  11.     LINE (ox, oy)-STEP(ow, oh), , BF
  12.     LOCATE 1, 1: PRINT INT(dx)
  13.     _DISPLAY
  14.     _LIMIT 60
  15.  

Watch what happens to bars, they never quite settle into any pattern.

                                             Warning: Lots of flashing lights going on here, epileptics BEWARE!

« Last Edit: September 06, 2018, 05:10:45 pm by bplus »

Offline Omerta7486

  • Newbie
  • Posts: 33
  • √𝜋²
    • View Profile
Re: sin (need to use in a loop for speed adjustment
« Reply #7 on: September 06, 2018, 02:40:09 pm »
First off, B+... Put a seizure warning on your second example! Please. I thought it looked cool, but an epileptic might feel otherwise! LOL

Second: Mackay, are you needing a parabolic jump arc, like gravity, or an elliptical loop like B+'s floating platform style example?
The knowledge that's seeking the favor of another means the murder of self.

Latest version of my game, here  Omertris: Invasion

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: sin (need to use in a loop for speed adjustment
« Reply #8 on: September 06, 2018, 05:14:01 pm »
So that's where my morning went! ;-))