February 27, 2021, 04:09:56 AM
Welcome,
Guest
. Please
login
or
register
.
Did you miss your
activation email
?
1 Hour
1 Day
1 Week
1 Month
Forever
Login with username, password and session length
Home
Forum
Help
Search
Gallery
Login
Register
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Algorithms
»
[bmx] TRadialVector, TLinearVector, TPos by ninjarat [ 1+ years ago ]
« previous
next »
Print
Pages: [
1
]
Go Down
Author
Topic: [bmx] TRadialVector, TLinearVector, TPos by ninjarat [ 1+ years ago ] (Read 419 times)
BlitzBot
Jr. Member
Posts: 1
[bmx] TRadialVector, TLinearVector, TPos by ninjarat [ 1+ years ago ]
«
on:
June 29, 2017, 12:28:42 AM »
Title :
TRadialVector, TLinearVector, TPos
Author :
ninjarat
Posted :
1+ years ago
Description :
TRadialVector is much more powerful if you want high accuracy, but TLinearVector is much faster to execute, seeing as the linear one just uses some basic algebra and the radial one uses some trig.
I origonally designed them for a particle system, the one in Diamonds. Diamonds was a warm up btw, my next game will be much better. Getting off topic. Sorry. Have fun!
Code :
Code: BlitzMax
'base class
Type
TPos
'would have extended it but it works better the way I did it
Field
x!,y!
Method
setTo:TPos
(
ex!,wy!
)
x=ex
y=wy
End
Method
Function
createPos:TPos
(
ex!,wy!
)
Local
temp:TPos
temp.x=ex
temp.y=wy
Return
temp
End
Function
End
Type
'children (sort of)
Type
TLinearVector
'less accurate but faster
Field
cur:TPos,nxt:TPos
Method
setTo
(
cr:TPos,nx:TPos
)
nxt=nx
cur=cr
End
Method
Method
setToZero
(
)
nxt=cur
End
Method
Method
increment
(
step
!=
1
)
xdif!=nxt.x-cur.x
ydif!=nxt.y-cur.y
cur=nxt
nxt.x:+xdif*
step
nxt.y:+ydif*
step
End
Method
Method
toRadialVector:TRadialVector
(
)
Local
temp:TRadialVector
temp.pos=cur
xdif!=nxt.x-cur.x
ydif!=nxt.y-cur.y
temp.vel=Sqr
(
xdif*xdif+ydif*ydif
)
If
xdif>
0
Then
temp.ang=ATan
(
yvel/xvel
)
Else
temp.ang=ATan
(
yvel/xvel
)
+
180
End
If
End
Method
Function
createVector:TLinearVector
(
cr:TPos,nx:TPos
)
Local
temp:TLinearVector
temp.nxt=nx
temp.cur=cr
Return
temp
End
Function
Function
createVectorFromCoords:TLinearVector
(
cx!,cy!,nx!,ny!
)
Local
temp:TLinearVector
temp.nxt=TPos.createPos
(
nx,ny
)
temp.cur=TPos.createPos
(
cx,cy
)
Return
temp
End
Function
End
Type
Type
TRadialVector
'more accurate but slower
Field
pos:TPos,ang!,vel!
Method
setTo
(
p:TPos,a!,v!
)
pos=p
ang=a
vel=v
End
Method
Method
setToZero
(
)
vel=
0
End
Method
Method
increment
(
step
!=
1
)
xvel!=Cos
(
ang
)
*vel*
step
yvel!=Sin
(
ang
)
*vel*
step
pos.x:+xvel
pos.y:+yvel
End
Method
Method
toLinearVector:TLinearVector
(
)
Local
temp:TLinearVector
temp.cur=pos
temp.nxt.x=pos.x+Cos
(
ang
)
*vel
temp.nxt.y=pos.y+Sin
(
ang
)
*vel
Return
temp
End
Method
Function
createVector:TRadialVector
(
p:TPos,a!,v!
)
Local
temp:TRadialVector
temp.pos=p
temp.ang=a
temp.vel=v
Return
temp
End
Function
Function
createVectorFromCoords:TRadialVector
(
px!,py!,a!,v!
)
Local
temp:TRadialVector
temp.pos=TPos.createPos
(
px,py
)
temp.ang=a
temp.vel=v
Return
temp
End
Function
End
Type
Comments :
ninjarat(Posted 1+ years ago)
Oops! The argument for increment() method is called step! WTF was I thinking!? Call it timestep. Makes more sense that way anyhow.
Logged
Print
Pages: [
1
]
Go Up
« previous
next »
SyntaxBomb - Indie Coders
»
Languages & Coding
»
Blitz Code Archives
»
Algorithms
»
[bmx] TRadialVector, TLinearVector, TPos by ninjarat [ 1+ years ago ]
SimplePortal 2.3.6 © 2008-2014, SimplePortal