A RigidBody 3D class

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

A RigidBody 3D class

Postby D.J.Peters » Nov 18, 2006 15:37

A RigidBody class based on this paper SingleRigidBodies.pdf


Download: rigidbody.bi

Joshy
Last edited by D.J.Peters on Sep 25, 2017 17:01, edited 17 times in total.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Postby 1000101 » Nov 18, 2006 16:10

Dick Chaney programs computers? God help us all.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Postby MichaelW » Nov 18, 2006 20:16

Who?
coderJeff
Site Admin
Posts: 2825
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Postby coderJeff » Nov 19, 2006 1:11

I like it, but I will never understand your economy of characters. ;)

Before:

Code: Select all

  _DF eigenvectors   (_B m _AM, _
                      _B v _AM, _
                      _R d _AV) _AI


After:

Code: Select all

  _DF eigenvectors   (_B m _AM, _
                      _R v _AM, _
                      _R d _AV) _AI


(byval/byref issue)

Otherwise I think it works OK. I did a few tests by limiting movement to one axis or one direction and it seems about right. Although a visualization would help to confirm that.
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Postby 1000101 » Nov 19, 2006 4:21

MichaelW wrote:Who?


Dick Chaney, the US Vice President.
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Postby D.J.Peters » Nov 19, 2006 19:23

Thanx coderJeff without your help i have never found this error.

Joshy
coderJeff wrote:I like it, but I will never understand your economy of characters. ;)
Long and readable:

Code: Select all

declare function eigenvectors(byval m as MATRIX3D, byref v as MATRIX3D, byref d as VECTOR3D) as integer
Short and for me readable too:

Code: Select all

  _DF eigenvectors   (_B m _AM, _
                      _R v _AM, _
                      _R d _AV) _AI
My day/night has only 24 hours and i like to type less charaters as posible.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Postby KristopherWindsor » Nov 19, 2006 23:41

D.J.Peters wrote:My day/night has only 24 hours and i like to type less charaters as posible.


Me too. :-)
D.J.Peters
Posts: 7667
Joined: May 28, 2005 3:28

Postby D.J.Peters » Oct 23, 2007 16:15

test01.bas

Code: Select all

#include "rigidbody.bi"
#include "GL/GLUT.bi"
#include "crt/math.bi"

' callback must be CDECL
declare sub ReshapeFunc cdecl (win_width as integer,win_height as integer)
declare sub KeyFunc     cdecl (key as ubyte,x as integer,y as integer)
declare sub TimerFunc   cdecl (v as integer)
declare sub DisplayFunc cdecl

declare sub InitGL
declare sub SimStep

#define USEEULER

randomize timer

Const Rad2Deg as float = 180.0/M_PI
Const g       as float = 9.81   ' gravity
Const fps     as float = 60     ' 60 steps per sec.
Const linKD   as float = 0.01   ' linear damping
Const rotKD   as float = 0.01   ' rotational damping

type BOX3D
  declare constructor(byval w    as float=1, _
                      byval h    as float=1, _
                      byval l    as float=1, _
                      byval mass as float=1)
  declare sub    EulerStep
  declare sub    RKStep
  declare sub    Draw
  as RIGIDBODY3D Body
end type

constructor BOX3D(byval w    as float=1, _)
                  byval h    as float=1, _
                  byval l    as float=1, _
                  byval mass as float=1)
  dim as QUATERNION qRotPos
  dim as VECTOR3D   linPos
  dim as VECTOR3D   linVel
  dim as VECTOR3D   rotAxis
  dim as VECTOR3D   rotVel

  Body.SetGravity(9.81)
  Body.SetBox(w,h,l,mass)
  Body.Diagonalise()
  rotAxis.z=1
  qRotPos.rotation(rotAxis, 1)
  linVel.y=-30
  linVel.z= 10
  rotVel.x= rnd*20-10
  rotVel.y= rnd*20-10
  rotVel.z= rnd*20-10
  Body.Initialise(linPos,linVel,qRotPos,rotVel,linKD,rotKD)
  Body.SetStepSize(1/fps)
end constructor

sub BOX3D.EulerStep
  body.RKInit()
  body.EulerStep()
end sub
sub BOX3D.RKStep
  body.RKInit()
  body.RKStep1
  body.RKStep2
  body.RKStep3
  body.RKStep4
end sub
sub BOX3D.Draw
  dim as float t=any
  dim as float s=any
  glLoadIdentity()
  glTranslated(0,0,-3)
  glColor3d(1,1,1)
  with Body
    t =acos(.qRotPos.w)
    s =1/sin(t)
    t*=Rad2Deg
    ' translate to position
    glTranslated(.linPos.x,.linPos.z,.linPos.y)
    ' rotate according to quaternion
    glRotated(2*t,s*.qRotPos.x,s*.qRotPos.z,s*.qRotPos.y)
  end with
  glutWireCube(1)
end sub

'
' main
'
dim shared as BOX3D box

glutInit               (__FB_ARGC__,cptr(ubyte ptr,__FB_ARGV__))
glutInitWindowPosition (0, 0)
glutInitWindowSize     (640,480)
glutInitDisplayMode    (GLUT_DOUBLE or GLUT_RGBA)
glutCreateWindow       ("test for rigid body [esc]=exit")
glutDisplayFunc        (@DisplayFunc)
glutReshapeFunc        (@ReshapeFunc)
glutTimerFunc          (1000/fps,@TimerFunc,0)
glutKeyboardFunc       (@KeyFunc)
InitGL
glutMainLoop


sub SimStep
#ifdef USEEULER
  box.EulerStep()
#else
  box.RKStep
#endif
  with Box.Body
  if .linPos.z<-50 or .linPos.y<-500 then
    .linPos.x=0
    .linPos.y=0
    .linPos.z=0

    .linVel.y= rnd*-100
    .linVel.z= rnd*15+1
    .linVel.x= rnd*10-5

    .rotVel.x= rnd*20-10
    .rotVel.y= rnd*20-10
    .rotVel.z= rnd*20-10
  end if
  end with
end sub 

sub ReshapeFunc cdecl (win_width as integer,win_height as integer)
  if win_width=0 or win_height=0 then return
  glViewport   (0,0,win_width,win_height)
  glMatrixMode (GL_PROJECTION)
  glLoadIdentity

  gluPerspective(45.0, win_width/win_height, 0.1, 500.0)
  glMatrixMode  (GL_MODELVIEW)
  glLoadIdentity
end sub

sub KeyFunc cdecl (key as ubyte,x as integer,y as integer)
  if key=27 then end 0
end sub

sub TimerFunc cdecl (v as integer)
  SimStep
  glutTimerFunc(1000/fps,@TimerFunc,0)
  glutPostRedisplay()
end sub

sub DisplayFunc cdecl
  glClear(GL_COLOR_BUFFER_BIT)
  Box.Draw
  glutSwapBuffers()
end sub

sub InitGL
  glPointSize (2.0)
  glLineWidth (2.0)
  glEnable    (GL_BLEND)
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  glEnable    (GL_LINE_SMOOTH)
  glEnable    (GL_POINT_SMOOTH)
end sub
notthecheatr
Posts: 1759
Joined: May 23, 2007 21:52
Location: Cut Bank, MT
Contact:

Postby notthecheatr » Oct 23, 2007 17:24

And the Vice Prez is Cheney, not Chaney. Thank heavens.

Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 2 guests