Page 1 of 1

X,Y to XX,YY Location Countdown Problem [SOLVED]

Posted: Fri Nov 09, 2007 1:00 am
by Rook Zimbabwe
Trying to make a smooth calculator to figure points along a line so I can shoot what I want and make a 20 (or whatever FPS I want) point target line:

Code: Select all

;

targetx = 24     ; end location
targety = 14

shootx = 157    ; start location
shooty = 104

; this works great with the following values:

;targetx = 24     ; end location
;targety = 14

;shootx = 157    ; start location
;shooty = 104

; or any values where the target is smaller than the shooter
; but not the other way around...

;what If the values were reversed... I cannot figure out
;how To create an even path no matter what the values are


Steps = 20      ; number of steps required to go from shoot to target

Dim spotx(21)   ; array to hold x position
Dim spoty(21)   ; array to hold y position

xx = shootx / Steps
yy = shooty / Steps

Debug "Starting"

For number = 0 To Steps  ; stepladder was set to 10 initially... I wanted to be able to change this value later
hopx = number * xx ; variable to subtract from shooter
hopy = number * yy
  placex = shootx - hopx
    placey = shooty - hopy
    
      spotx(number) = placex 
        spoty(number) = placey 
        
         Debug placex 
         Debug placey
  
Next

End
;
It is a maths thing. Any help would be appreciated. 8)

Posted: Fri Nov 09, 2007 1:27 am
by citystate
your problem is here

Code: Select all

xx = shootx / Steps 
yy = shooty / Steps 
replace this with

Code: Select all

xx = (shootx-targetx) / Steps 
yy = (shooty-targety) / Steps 

Posted: Fri Nov 09, 2007 2:06 am
by case
i guess the points will go in the wrong direction

Code: Select all

xx = (shootx-targetx) / Steps
yy = (shooty-targety) / Steps

you may do that instead

Code: Select all

xx = (targetx-shootx) / Steps
yy = (targety-shooty) / Steps

also better use floats. integer will end in some errors if you use a lot of steps

here's a small demonstration the yellow line is integer the white one use floats the red point is the start the green one is the target

play with the "steps" value


Code: Select all


InitSprite  ()
InitKeyboard   ()
OpenScreen   (1024, 768, 32,"") 
;--------floats
Declare Plt(x,y,xx,yy,Stp,maxstp)
;--------integer 
Declare Plti(x,y,xx,yy,Stp,maxstp)
; change here the number of steps
steps=200

For a=1 To steps
	plt(100,100,800,600,a,steps)
	plti(100,100,800,600,a,steps)
Next a
FlipBuffers()	
; wait for escape to quit
Repeat
	ExamineKeyboard()	
Until KeyboardPushed(#PB_Key_Escape)
End
;------------------------------------------------
;            x,y   :starting coords
;            xx,yy :target coords
;            stp   :curent step
;            maxstp:total steps
;------------------------------------------------
Procedure Plt(x,y,xx,yy,Stp,maxstp)
    dx=xx-x
    dy=yy-y
    xmov.f=(dx/maxstp)					
    ymov.f=(dy/maxstp)	
    StartDrawing(ScreenOutput())
    ddx=x+xmov*Stp
    ddy=y+ymov*Stp
    	Box(x-1,y-1,3,3,RGB(255,0,0))
    	Box(xx-1,yy-1,3,3,RGB(0,255,0))   		  
        Plot(ddx,ddy,RGB(255,255,255))
   	StopDrawing()       
EndProcedure
; same with integers
Procedure Plti(x,y,xx,yy,Stp,maxstp)
    dx=xx-x
    dy=yy-y
    xmov=(dx/maxstp)					
    ymov=(dy/maxstp)	
    StartDrawing(ScreenOutput())
    ddx=x+xmov*Stp
    ddy=y+ymov*Stp
    Box(x-1,y-1,3,3,RGB(255,0,0))
    Box(xx-1,yy-1,3,3,RGB(0,255,0))
   	Plot(ddx,ddy,RGB(255,255,0))
   	StopDrawing()       
EndProcedure

Posted: Fri Nov 09, 2007 2:14 am
by citystate
case wrote:i guess the points will go in the wrong direction
I don't think so... ordinarily, I'd add the xx/yy values but as Rook is subtracting them from the shooter's location, I think they'll work fine as is (insert the usual 'talking out of my hat' disclaimer here)

meh suck it and see, one or the other should work for you Rook - don't have PB on this computer so I can't test it ATM (luckily it's a small change) :)

Posted: Fri Nov 09, 2007 2:22 am
by case
oups you're right he's substract them :) but it's not the way bullets normally go from shooter to target... ^^

Posted: Fri Nov 09, 2007 3:21 pm
by Rook Zimbabwe
Case that is essentially it, but I wanted to slow it down to look like the lines were drawn gracefully which is why I had the thought to divide it into maybe 30 steps (or 60) film speed...

but it does work, now to try it with bigger sprites... :)

Code: Select all


InitSprite  () 
InitKeyboard   () 
OpenScreen   (1024, 768, 32,"") 
;--------floats 
Declare Plt(x,y,xx,yy,Stp,maxstp) 
;--------integer 
Declare Plti(x,y,xx,yy,Stp,maxstp) 
; change here the number of steps 
steps=200 

a=1 ; setup read
Repeat 
  Delay(25) ; slow down execution a bit
    If a < 200 ; only commit loop if a is less than the steps we asked for
      plt(100,100,800,600,a,steps) 
        plti(800,600,200,400,a,steps) 
   a = a + 1
   EndIf
   
FlipBuffers() 
   
ExamineKeyboard()    

Until KeyboardPushed(#PB_Key_Escape) ; wait for escape to quit 
End 
;------------------------------------------------ 
;            x,y   :starting coords 
;            xx,yy :target coords 
;            stp   :curent step 
;            maxstp:total steps 
;------------------------------------------------ 
Procedure Plt(x,y,xx,yy,Stp,maxstp) 
    dx=xx-x 
    dy=yy-y 
    xmov.f=(dx/maxstp)                
    ymov.f=(dy/maxstp)    
    StartDrawing(ScreenOutput()) 
    ddx=x+xmov*Stp 
    ddy=y+ymov*Stp 
       Box(x-1,y-1,3,3,RGB(255,0,0)) 
       Box(xx-1,yy-1,3,3,RGB(0,255,0))           
        Plot(ddx,ddy,RGB(255,255,255)) 
      StopDrawing()        
EndProcedure 
; same with integers 
Procedure Plti(x,y,xx,yy,Stp,maxstp) 
    dx=xx-x 
    dy=yy-y 
    xmov=(dx/maxstp)                
    ymov=(dy/maxstp)    
    StartDrawing(ScreenOutput()) 
    ddx=x+xmov*Stp 
    ddy=y+ymov*Stp 
    Box(x-1,y-1,3,3,RGB(255,0,0)) 
    Box(xx-1,yy-1,3,3,RGB(0,255,0)) 
      Plot(ddx,ddy,RGB(255,255,0)) 
      StopDrawing()        
EndProcedure 

nicee work!