Author Topic: A Variable Nightmare??  (Read 4198 times)

0 Members and 1 Guest are viewing this topic.

Offline Omnikam

  • Atari ST
  • ***
  • Posts: 121
  • Karma: 4
    • View Profile
A Variable Nightmare??
« on: November 15, 2015 »
Hey guys/Girls  I'm trying to figure out this seemingly basic problem
I`m only at the debug stage so please excuse the rudimentary of this code
The problem I'm having is that purebasic doesn't like my x=(200,200) format. The x is a positional marker, vextor x without the z coordinates
this is my code
Code: [Select]
srx=800
sry=600
x = (200,200) ; starting location
v =  (0,4) ; velocity
a =  2  ; acceleration
t = 0 ; time
xt = x+v*t  ; postion at time
vt = v+a*t ; velocity at time
Repeat
Debug xt
Debug vt
t=t+1
ForEver
i`d like to be able to do something like
Code: [Select]
displysprite(x) or displaysprite(xt)What am i doing wrong? and sorry if this question is obvious, Ive only being programing for around a month+

Offline Omnikam

  • Atari ST
  • ***
  • Posts: 121
  • Karma: 4
    • View Profile
Re: A Variable Nightmare??
« Reply #1 on: November 15, 2015 »
Okay Ive tried to work it out
This is what Ive come up with, I'm not sure if it`s right, but it does something  ;D
Not sure which of my displayed sprites reflect an object starting at 0,0 and having a velocity (v) and acceleration (a) moves with gravity (g)
Code: [Select]
InitSprite()
InitKeyboard()
srx=800
sry=600
xx=0
xy=0
v1=0
v2=8       ;velocity
x = xx+xy
v = 0+v2
a =  1
t=0
g=-9.81  ; gravity
OpenScreen(800,600,32,"PBWINDOW")
UsePNGImageDecoder()
UseJPEGImageDecoder()
ship= LoadSprite(#PB_Any,"Crystal.png")
line = LoadSprite(#PB_Any,"line.png")
;ElapsedMilliseconds()%1000
;Repeat
  t = t+1
  xt = x+v*t
vt = v+a*t
CreateImage(0, 800, 600, 32,black)

Delay(100)
;ForEver
TransparentSpriteColor(ship, #White)
Repeat
  Debug t 
  Debug xt
 Debug vt
  Debug g

  ClearScreen(0)
  ExamineKeyboard()   
  t = t+1
    xt = x+v*t
    vt = v+a*t
    A$=Str(xt)
    DisplayTransparentSprite(ship,xt,vt-g)
    DisplaySprite(line,0,30)
;DisplayTransparentSprite(ship,0,-g)
;DisplayTransparentSprite(ship,xt,vt)
;DisplayTransparentSprite(ship,a-g,0)
    StartDrawing(ScreenOutput())
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawText(xt-20,vt-g, A$, RGB(Random(255), Random(255), Random(255)))
     
 StopDrawing()
g=g-9.81
FlipBuffers()
Delay(40)




 

Until KeyboardPushed (#PB_Key_Escape)
End

« Last Edit: November 15, 2015 by Omnikam »

Offline Omnikam

  • Atari ST
  • ***
  • Posts: 121
  • Karma: 4
    • View Profile
Re: A Variable Nightmare??
« Reply #2 on: November 17, 2015 »
Alright after much trial and error Ive come up with this solution, I think the maths setup is better, i can control direction with pvx and pvy. I still dont know how to change  direction when it goes outside screen, I'm guessing a getposition test, but i don't know how to reverse my equation to go in the opposite, i suppose ill have to keep working on it. Does anyone know why my input screens dont always show up?
Code: [Select]
Global px,py,pvx,py,a,t,h,g,xtx,vtx,xty,vty,A$,B$,C$,T$,h$,ship,line,liney,srx,sry
Procedure init()
  InitSprite()
InitKeyboard()
  srx=800
sry=600
px=0
py=0
pvx=1;velocity
pvy= 0
tmax=500

a = 0
t=0
h=0.5
g=-9.81  ; gravity
;g=-1.00  ; gravity
OpenScreen(srx,sry,32,"PBWINDOW")
UsePNGImageDecoder()
UseJPEGImageDecoder()
ship= LoadSprite(#PB_Any,"Crystal.png")
line = LoadSprite(#PB_Any,"line.png")
liney = LoadSprite(#PB_Any,"liney.png")
EndProcedure
 init()
;ElapsedMilliseconds()%1000
;Repeat
Procedure movement()
   t = t+1
  moveflag = 0
      t = t+1
  xtx=px+pvx*t
  xty=py+pvy*t
  vtx=pvx+a*t
  vty=pvy+a*t
  x=xtv+vtx
  y=xty+vty
    A$=Str(xtx)
    B$=Str(vty)
    C$=Str(g)
    T$=Str(t)
    h$=Str(h)
        h=h-0.5
 g=g-9.81
 ;g=g-1.00
EndProcedure

Procedure draw()
  TransparentSpriteColor(ship, #White)
  DisplayTransparentSprite(ship,xtx-h,vty-g)
    DisplaySprite(line,0,30)
    DisplaySprite(liney,400,0)
;DisplayTransparentSprite(ship,0,-g)
;DisplayTransparentSprite(ship,xtx,xty-g)

    StartDrawing(ScreenOutput())
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawText(xtx-h+80,vty-g, "xtx  vtyy  -g          "+ A$+B$ ,#White); RGB(Random(255), Random(255), Random(255)))
    ;  DrawText(xtx,xty, "xtx  xty  -g          "+ A$ +A$ +C$,#Red)     ; RGB(Random(255), Random(255), Random(255)))
     
      DrawText(0,0,T$,#White)
      StopDrawing()
    EndProcedure
   
      Define velocity.s
      Define accel.s
 
     CreateImage(0, 800, 600, 32,black)

Delay(100)
;ForEver

Question:
  ClearScreen(0)
velocity = InputRequester("","enter velocity","")

accel=InputRequester("","enter acceleration","")
ac=Val(accel)
a=ac
      z=Val(velocity)
      pvx=z
     
Repeat
 ; Debug vty
 ; Debug xt
; Debug vt
; Debug g

  ClearScreen(0)
  ExamineKeyboard()   
 movement()
    draw()
      h=h-0.5
 ;g=g-9.81
; g=g-1.00
FlipBuffers()
Delay(40)

If xtx>800
px=0
py=0
pvx=1;velocity
pvy= 0
tmax=500

a = 0
t=0
h=0.5
g=-9.81  ; gravity


Goto Question
EndIf
  If g<-600
px=0
py=0
pvx=1;velocity
pvy= 0
tmax=500

a = 0
t=0
h=0.5
g=-9.81  ; gravity


Goto Question
EndIf
 
If KeyboardPushed(#PB_Key_Up)
 g=g+20
 
 
EndIf
If KeyboardReleased(#PB_Key_Up)
 
EndIf

If KeyboardPushed(#PB_Key_Left)
 
  pvx=-1
 
EndIf
If KeyboardPushed(#PB_Key_Right)
 
EndIf
Until KeyboardPushed (#PB_Key_Escape)
End

« Last Edit: November 18, 2015 by Omnikam »

Offline emook

  • C= 64
  • **
  • Posts: 94
  • Karma: 12
    • View Profile
Re: A Variable Nightmare??
« Reply #3 on: November 24, 2015 »
Hiya,

Not quite sure I understand what you're trying to do?

:)
----

R Tape loading error, 0:1

Offline Omnikam

  • Atari ST
  • ***
  • Posts: 121
  • Karma: 4
    • View Profile
Re: A Variable Nightmare??
« Reply #4 on: November 25, 2015 »
Hey Emook,  well what I was trying to do was figure out how to move things around the screen,  using vector equations.  I added gravity for fun,  but basically I was just trying to test the equation. I used velocity, acceleration and gravity. I  was trying to figure it out as I had a cool idea for a text display using the equation.  The equation is simple enough on paper, but getting it working in purebasic was harder.  I think I did a good job of moving the object in an initial direction, but had no idea how to alter the equation when the object say hit an object or the edge of the screen.  In my last example I put in user input just to show how different  numbers effects thing like direction and acceleration