Dark Bit Factory & Gravity

PROGRAMMING => General coding questions => Topic started by: KrazyK on March 15, 2011

Title: Plotting along the hypotenuse
Post by: KrazyK on March 15, 2011
Hi all,
I'm pretty new to democoding since I bought Purebasic recently and need help with the code in plotting a sprite along a hypotenuse path.
If I wanted to plot a path from x1,y1 to x2,y2 how would I go about it?  I know how to get the length of the hypotenuse but am a bit lost figuring out the path plotting when it comes to sin/cos/tan/deg functions etc, i should have paid more attention in school really! :-[
I'm sure it's a simple question for some of the talented people here and hope to learn from you.
Title: Re: Plotting along the hypotenuse
Post by: Shockwave on March 15, 2011
Hi Krazyk,

This is the bit you know (get the length of a line), just included here for people who need it;

Code: [Select]
DISTANCE = SQRT((X2-X1)^2 + (Y2-Y1)^2)

And the bit you need;

To plot a sprite along the path, store the top left pair in two variables
EG;

XPLOT = X1
YPLOT = Y1

divide X2-X1 by the amount of steps you want to take eg;

XDIV = (X2-X1) / STEPS

then divide y2-y1 by the amount of steps EG;

YDIV = (Y2-Y1) / STEPS

Then loop the amount of steps needed and add the result of the x division to the x you stored and do the same for the y and that should do the trick, so you just add xdiv to XPLOT and YDIV to YPLOT each step of the draw loop.

You may have to  floating point to integer before you draw the sprites, i dunno, I'm not really familiar with PB syntax :) If I'm wrong I'm sure someone else with more of a clue will come along soon.
Title: Re: Plotting along the hypotenuse
Post by: padman on March 15, 2011
Hi there!
I hope this isn't too complicated. But structures and linked lists are explained pretty good in the PB helpfile, so I hope you can understand what's going on even if you are new to PB. I'm not exactly a maths genius, so I can't explain in detail all the cos/sin/atan stuff involved, but it works:  ;) Source code and binary attached.

Pad

Code: [Select]
;*****************************************************************************************************
;*Based upon portions of a code snippet by PMV (if I remember correctly). Mutilated and hopefully    *
;*simplified by Padman on March 15th 2011. Tested With PB 4.41.                                      *
;*****************************************************************************************************


Structure Sprite
  x.f
  y.f
  Speed.f         
  Angle.f         
  State.l
EndStructure


Structure Dots
x.l
y.l
dot_number.l
EndStructure

Global Sprite.Sprite


Global NewList WayPoint.Dots()


Procedure.f Angle(cathetus_x.l, cathetus_y.l)
  If cathetus_x < 0 And cathetus_y <= 0
    ProcedureReturn ATan(-cathetus_y/-cathetus_x)*180/#PI+180
  ElseIf cathetus_x < 0 And cathetus_y >=0
    ProcedureReturn ATan(-cathetus_x/cathetus_y)*180/#PI+90
  ElseIf cathetus_x > 0 And cathetus_y > 0
    ProcedureReturn ATan(cathetus_y/cathetus_x)*180/#PI
  ElseIf cathetus_x >= 0 And cathetus_y<0
    ProcedureReturn ATan(-cathetus_x/cathetus_y)*180/#PI+270
  EndIf
EndProcedure




InitSprite()
OpenWindow(0, 0, 0, 640, 480, "Sprite movement between waypoints.", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 640, 480, 0, 0, 0)


ClearList(WayPoint())

AddElement(WayPoint())

 WayPoint()\x = 0                           ; first point
 WayPoint()\y = 0
 Waypoint()\dot_number = 1
 
 
AddElement(WayPoint())

 WayPoint()\x = 630                        ; second point... add more if you want, of course you can put
 WayPoint()\y = 460                        ; this all into a loop.
 Waypoint()\dot_number = 2
 
 
FirstElement(WayPoint())                   ; set the first point as starting point
Sprite\x = WayPoint()\x
Sprite\y = WayPoint()\y
Sprite\State = 0






Repeat
 
  Select Sprite\State
    Case 0 ; off to the next waypoint
      Sprite\Speed + 1
     
      Distance = Sqr(Pow(WayPoint()\x - Sprite\x, 2) + Pow(WayPoint()\y - Sprite\y, 2))
      If Distance <= 1
        Sprite\State = 1
      EndIf
     
    Case 1 ;we have arrived
       Sprite\Speed =0
       If Sprite\Speed = 0
         If Not NextElement(WayPoint())   ;if the end of the list is reached, go back to the first one
           FirstElement(WayPoint())
         EndIf
     
        Sprite\Angle = Angle(WayPoint()\x - Sprite\x, WayPoint()\y - Sprite\y)
        Sprite\State = 0
     EndIf
  EndSelect
 
 
  Sprite\x + Cos(Sprite\Angle * #PI / 180)
  Sprite\y + Sin(Sprite\Angle * #PI / 180)

  ClearScreen(0)
  StartDrawing(ScreenOutput())
  DrawingMode(#PB_2DDrawing_Transparent)

    *Current = @WayPoint()
    ForEach WayPoint()
      Circle(WayPoint()\x, WayPoint()\y, 2, $FFFFFF)
      DrawText(WayPoint()\x, WayPoint()\y,Str(Waypoint()\dot_number))
    Next
   
   
    ChangeCurrentElement(WayPoint(), *Current)
    Box(Sprite\x - 2, Sprite\y - 2, 5, 5, $0000FF)
  StopDrawing()
  FlipBuffers()
 
 

Until WaitWindowEvent(10) = #PB_Event_CloseWindow Or GetAsyncKeyState_(#VK_ESCAPE)

Title: Re: Plotting along the hypotenuse
Post by: KrazyK on March 15, 2011
Thanks for the input guys, i can see how this works now and will try to use it in my next project.
In the meantime here's a screenshot of my first PB oldskooler that i've almost finished as i've just figured out how to wibble my main logo thanks to messing about with sin/cos functions.
  :inspired:
Title: Re: Plotting along the hypotenuse
Post by: Shockwave on March 15, 2011
That looks  pretty sweet KrazyK :)

Karma+

Looking forward to seeing it finished!
Title: Re: Plotting along the hypotenuse
Post by: KrazyK on March 15, 2011
Ok, here it is then, as finished as I think I can make it - my first oldskooler in PB. 

Important: ** You need at least the latest DirectX 9c to run this demo or you will recieve an error warning **

Press F12/F11 to turn on/off frame rate display during the demo.  My main PC runs at 60fps while my laptop only runs at 29 and it looks crap slow.
Let me know if it runs ok or if there are any errors etc or suggestions on how to improve it. ;)

Title: Re: Plotting along the hypotenuse
Post by: Hotshot on March 15, 2011
I check the full screen and windows.......the demo is great when go FPS 59 but there is big slowdown when go down to FPS 29.......so it need to fixed that and I have to say for your First Attempt in purebasic is Very good!

Well Done  :kewl:

Keep coding more Demo eh? ;)
Title: Re: Plotting along the hypotenuse
Post by: KrazyK on March 15, 2011
Thanks for the info.  it's the 8 sine scrollers in the middle that kill it on slower pcs.  I can't get it to slow down on my main pc.  It's just my laptop that runs at 29FPS when the middle scrollers come on.
Just tried it with 2 and it's running at 58FPS but doesn't look as good.  I'll modify it to check frame rate and adjust the number of scrollers if below a certain FPS?
Title: Re: Plotting along the hypotenuse
Post by: Hotshot on March 15, 2011
ok...try 3 sine scrollers and that shouldnt be too much.....let me know what FPS is when having 3 sine scrollers or 4 sine scrollers :)

I do love the music bars that go up and down..... ;)
Title: Re: Plotting along the hypotenuse
Post by: KrazyK on March 15, 2011
with 3 sine scrollers: windowed 47-51 FPS     Fullscreen:45-48 FPS.
I'll re-compile it later in the week when i've made the improvements.  :cheers:

Title: Re: Plotting along the hypotenuse
Post by: va!n on March 16, 2011
Congratulation! Very nice work! Works very smooth here!
There is just a small things i noticed:

When running in Fullscreen mode, i see the window titlebar (seems like Window==Maximized)! When clicking on the window titlebar, it jumps over to the normal (windowed mode).

Best regards and hope to see more nice stuff from you soon :)
Title: Re: Plotting along the hypotenuse
Post by: padman on March 16, 2011
Nice stuff you got there! And it's Atari Style <3  :clap:

But like va!n said you should get rid off the title bar in fullscreen mode.  ;) And I have about 37 FPS on my PC. I code in Purebasic as well and usually I have no probs that it runs at about 60FPS. Do you use a lot of StartDrawing()/StopDrawing() commands? If so, that is really a performance killer and you should put it all into one block. All in all you can say that all drawing commands are pretty slow in PB (except for Plot with an color argument) You should use sprites instead wherever you can.
Anyway cool work. Maybe you should take a look at www.retro-remakes.net (http://www.retro-remakes.net). Would be cool if you remade one or another Atari ST Cracktro. You sure got the skills for it. :) And I wouldn't feel so lonely as the only Atari guy on there anymore. ;)

K+
Title: Re: Plotting along the hypotenuse
Post by: Shockwave on March 16, 2011
Totally smooth here :)

Thanks for the nice mentions for DBF, you put a lot of effects in there and I really liked it!

Looking forward to seeing some more from you.
Title: Re: Plotting along the hypotenuse
Post by: KrazyK on March 17, 2011
Thanks for the feedback guys, I've modified it a bit now:
Only 3 sine scrollers but with different fonts.
Made it proper fullscreen now, no title bar.
Changed the copper efect on the top to nice gold one.
Added F9/F10 to toggle vsync to test maximum framerate - this really screws up the spheres though so you will have to run it again.
...i'm off to look at  www.retro-remakes.net (http://www.retro-remakes.net/) for some inspiration now.
Title: Re: Plotting along the hypotenuse
Post by: Hotshot on March 17, 2011
I have checked it.....the full screen does fook it up my system :( but on window....it ran so Smooth  FPS 59 all the way through!

Fantastic   :kewl:

 :cheers:
Title: Re: Plotting along the hypotenuse
Post by: Shockwave on March 17, 2011
It does look better like that :)
Title: Re: Plotting along the hypotenuse
Post by: hellfire on March 17, 2011
The wireframe-spheres probably look nicer if you don't draw the diagonal edge (just quads instead of triangles).
Especially at the bottom (where you can see the back-side, too) it looks a little bit "chaotic" now.
Just an idea...

Oh, and I love the music.
Title: Re: Plotting along the hypotenuse
Post by: Rbz on March 17, 2011
Runs smoothly here, nice work.

I think if you fade, around 50%, the reflection alpha color of the top scroller it would look much better and easy to read.
Title: Re: Plotting along the hypotenuse
Post by: KrazyK on March 22, 2011
Ok then here goes again, learning all the time guys.
You know when you've tested and tested a piece of code over and over again and got completely bored with seeing the same thing and hearing the same clip of music?
Well that's what's happended here so i've changed the demo again.

Changes:
Made the reflected top scroller more transparent.
Replaced wireframe spheres with mapped cubes.
Added oldieskoolie raster bars.
Moved the starfield from the centre to behind the bottom scroller.



You should hopefully still get the same framerate as before.
I reckon this is it now, no more changes.....he said looking at the glitch he's just spotted. ::)






Oh, and try F7/F8 to toggle a really cool effect.



updated exe attached now - no cursor  ;)
Title: Re: Plotting along the hypotenuse
Post by: padman on March 22, 2011
Well, still cool stuff. Love the projection of the screen on the cubes. One last thing though: please get rid off the cursor in fullscreen mode ;)
ShowCursor_(0)  should do the trick.
Title: Re: Plotting along the hypotenuse
Post by: KrazyK on March 22, 2011
Aargghhhh, there's always something.  Updated exe in previous post now. ;)

@padman, how do you use the OSME library within your exe? Do you use the Pure_Procs PB userlibrary that calls the dll from within memory as that's the one I use?
Title: Re: Plotting along the hypotenuse
Post by: padman on March 22, 2011
Hehe great! & Yep that's exactly how I do it, because I never managed to get the static lib to work with PB.
Title: Re: Plotting along the hypotenuse
Post by: Shockwave on March 22, 2011
I'm really liking the textured cubes :)

I wonder what you'll do for your next trick!
Title: Re: Plotting along the hypotenuse
Post by: Rbz on March 22, 2011
It's much better now, well done