News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Syncing up Pendulums?

Started by Draco9898, April 03, 2006, 10:20:04 PM

Previous topic - Next topic

Draco9898

Can anyone figure out an equation to get all these pendulums to sync up with each other despite they are swinging at different angles?

Angle 0 = Completely frozen up in the air, Angle 180= completely gravitated towards the ground.

Only thing I figured would work is if you swing two from completely opposite ends like one at a 90* angle and one at a 270* angle.



ScreenVsync 1
Global ScreenW#=640: Global ScreenH#=480
OpenScreen ScreenW#,ScreenH#,16,2

Dim SpikeBallImgs(2)
TempImage=GetFreeImage()
LoadImage "SpikeBallnew.png",TempImage
RenderToImage TempImage
X=1
SpikeBallImgs(X)=GetFreeImage()
GetImage SpikeBallImgs(X),0,0,20,20
PrepareFXImage SpikeBallImgs(X)
X=2
SpikeBallImgs(X)=GetFreeImage()
GetImage SpikeBallImgs(X),0,20,72,92
PrepareFXImage SpikeBallImgs(X)
RenderToScreen

Type tSballChn
X#,Y#
NumLinks
AngVelo#, CurAngle#
Sprite#
threeDTilt
versFlag
midVelo#
EndType
Dim SballChn(8) As tSballChn
For X=1 To 8
SballChn(X).Sprite#=GetFreeSprite()
CreateSprite SballChn(X).Sprite#
SballChn(X).X#=80+((X-1)*52)
SballChn(X).CurAngle#=(X*10)
SballChn(X).AngVelo#=0
SballChn(X).versFlag=1
SballChn(X).threeDTilt=8
Next X

Do
Cls 0
For X=1 To GetArrayElements(SballChn().tSballChn,1)
 `Velocity based on Angle-
 If SballChn(X).CurAngle#>180
  SballChn(X).AngVelo#=SballChn(X).AngVelo#-0.02
 Else
  SballChn(X).AngVelo#=SballChn(X).AngVelo#+0.02
 EndIf
 SballChn(X).CurAngle# =SballChn(X).CurAngle# +SballChn(X).AngVelo#
 `WrapAngles-
 If SballChn(X).CurAngle#>360
  SballChn(X).CurAngle#=0
 EndIf
 If SballChn(X).CurAngle#<0
  SballChn(X).CurAngle#=360
 EndIf
 `Get Positions for chain using sin/cos-
 SinVal#=(Sin(SballChn(X).CurAngle#)*SballChn(X).threeDTilt)
 CosVal#=(Cos(SballChn(X).CurAngle#)*16)
 If SballChn(X).versFlag=0
  X#=SballChn(X).X#
  Y#=200+SballChn(X).Y#
  DrawImage SpikeBallImgs(1),X#-10,Y#-10,1
  For L=1 To 8
   X#=SballChn(X).X#+(SinVal#*L)
   Y#=200+SballChn(X).Y#-(CosVal#*L)
   If L=8
    DrawImage SpikeBallImgs(2),X#-36,Y#-36,1
   Else
    DrawImage SpikeBallImgs(1),X#-10,Y#-10,1
   EndIf
  Next L
 Else
  If SballChn(X).CurAngle#>180 Or SballChn(X).CurAngle#>360
   X#=SballChn(X).X#
   Y#=200+SballChn(X).Y#
   DrawImage SpikeBallImgs(1),X#-10,Y#-10,1
   For L=1 To 8
    X#=SballChn(X).X#+(SinVal#*L)
    Y#=200+SballChn(X).Y#-(CosVal#*L)
    If L=8
     DrawImage SpikeBallImgs(2),X#-36,Y#-36,1
    Else
     DrawImage SpikeBallImgs(1),X#-10,Y#-10,1
    EndIf
   Next L
  Else
   For L=8 To 1 Step-1
    X#=SballChn(X).X#+(SinVal#*L)
    Y#=200+SballChn(X).Y#-(CosVal#*L)
    If L=8
     DrawImage SpikeBallImgs(2),X#-36,Y#-36,1
    Else
     DrawImage SpikeBallImgs(1),X#-10,Y#-10,1
    EndIf
   Next L
   X#=SballChn(X).X#
   Y#=200+SballChn(X).Y#
   DrawImage SpikeBallImgs(1),X#-10,Y#-10,1
  EndIf
 EndIf
 X#=SballChn(X).X#
 Y#=200+SballChn(X).Y#
 Text X#-10,Y#-40,SballChn(X).CurAngle#
 Text X#-10,Y#-60,SballChn(X).AngVelo#
Next X
Sync
Loop
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

#1
I was looking at it before, but decided to just write one and here it is bellow.

 Anyway, In this case, save yourself some effort, and just split the movement code into a sub. This sub should be called to step the object to it's next position (in both your init and update code).   So when you init their starting positions.  Set each ball to it's a known fixed point. Then simply pre-step each ball to whatever offset you want.  This way, regardless of how the movement is handled, you can linearly force it be in a relativity position.  Effort required.  None

Draco9898

#2
real good kevin, thanks, I guess you could consider my brain busted :P I'm not sure what your doing with all these maths...Lol I'm a pretty crummy programmer...

lol cherries...

so your stepping thru the physics for each angle "missing" of the previous pendulum so they're matched up in terms of velocity?

Silly me, I was trying to make an algorithim to get them synced up..



I like the RGBmask, very nice...Is there any way to write over certian 'ranges' of colours? like reds -> Blues


I also wanted to previously have all the links give way to 'slack' in the chain any idea how to do this?
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

#3
In the example, each object is offset by X number of movement steps from the original starting position.

  So the object #1, is offset 0 steps,  Object #2 is stepped say 10.. Object #3 would be stepped 20..etc etc   To step them, we just call there movement routine.   So there's no extra work involved.

 You could make slack illusion a number of ways.  One old trick is to add a sin wave to a line. Which give gives a sort of arc.  

 A better way might be to use some control points and plot a bezier curve..  -> Bezier curves Example

  RgbMaskImage performs a logical AND upon the images viewport.  So you can remove certain bit patterns, but you can't screen out particular ranges of R,G,B.

To mask off GREEN and keep all RED + Blue bits..
  RgbMaskImage imagenumber, Rgb(255,0,255)

  RgbMaskImage imagenumber, Rgb(255,0,0) ; keep Red
  RgbMaskImage imagenumber, Rgb(0,255,0) ; keep Green only
  RgbMaskImage imagenumber, Rgb(0,0,255) ; keep Blue only

etc

thaaks

Why was this posted in Beginners at all???  :D
Isn't there some expert forum or some "Danger - extreme math inside" forum?

This is again the time where I wished there would be a cool "Math and Physics for game programmers" book with handsome tutorials on how to add good physics to your game with understandable examples that explain the math inside...
Any recommendations?

I am so happy to work for some telco where the most complicated thing to deal with are phone numbers  :rolleyes:
...no, not really  ;)

Cheers,
Tommy who still stares at Kevin's code cluelessly  :blink:

Draco9898

#5
oh cool, I'm still scratching my head at these 3 lines though

For offset=0 To Angle-1
 Step_Pendulum(lp)
Next


Where does 'Angle' come from? It wasn't called before this, some whats it equal?

Oh Nevermind, I found it, you adding onto the angle at every ball at the end for the for next loop


Yes, there should be an experts help area :) Although I'm not sure this is that complicated or not

QuoteWhy was this posted in Beginners at all??? biggrin.gif
Isn't there some expert forum or some "Danger - extreme math inside" forum?

This is again the time where I wished there would be a cool "Math and Physics for game programmers" book with handsome tutorials on how to add good physics to your game with understandable examples that explain the math inside...
Any recommendations?

I am so happy to work for some telco where the most complicated thing to deal with are phone numbers rolleyes.gif
...no, not really wink.gif

Cheers,
Tommy who still stares at Kevin's code cluelessly

I think he's basically taking my math and De-Draco-fying it :), untangling my mess, if you will :)
He's simplififing stuff with Cosradius / sinradius which really cleans it up
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin

kevin

Quotehis is again the time where I wished there would be a cool "Math and Physics for game programmers" book with handsome tutorials on how to add good physics to your game with understandable examples that explain the math inside...

 This is just trig.  So the object moves on circular path.   But, Rather than change it's angle by a fixed amount (circle/ellipse).  Which would make it move at a uniform rate (so it'd appear to not be effective by gravity).   The trick is, (if you could call it that)  it's using cos() to calc the movement speed.   So the speed rises and falls, since its a wave.  

This means the movement speed ranges from 0 to max speed (wave height).  Where it will slowly accelerate (0 to 90), to it's max (90 degrees),  then decelerate (90 to 180).

 The accel angle is stepping a constant amount each update (which is the accel direction).    When Accel angle hits it's limit of 180 (on the right), it flips the direction to a negative.  It also reduces the accel rate.  Which is the size of the accelation wave (the amount to move the object).  This is what makes it slowly appear to loose energy.  

The same occurs when it's moving left (180 to 0) degrees

Draco9898

#7
oh I see kevin, you just took out my physics and slapped in the CosnewValue to make it appear to be swinging faster and slower as it goes up and down...neat trick..

But the thing I liked about my version is you could start my pendulums swinging from any point and they swing with more realism...

But I do like yours, you can set them to swing however you wish..

I think I'll try using the stepping method with my code and see if I can get it to work, I get  whats happening now...You basically step how many ever steps you want your pendulums to be ahead in time  :D
DualCore Intel Core 2 processor @ 2.3 ghz, Geforce 8600 GT (latest forceware drivers), 2 gigs of ram, WIN XP home edition sp2, FireFox 2.

"You'll no doubt be horrified to discover that PlayBasic is a Programming Language." -Kevin