Results 1 to 4 of 4

Thread: Anyone experienced with vector algorithms?

  1. #1
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    456
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default Anyone experienced with vector algorithms?

    Specifically 3 dimensional operations.

    I would be very grateful for some 1 on 1 support.

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by ReadySteadyGo View Post
    Specifically 3 dimensional operations.

    I would be very grateful for some 1 on 1 support.
    Do you mean like in math? Or vectors in programming? I took calculus and stuff so I can do vectors pretty well.. I'm also quite good at physics so if its that then I can help with Torque n stuff..

    Can Do:
    Torque
    Vector Addition and Subtraction
    Resolving into components for physics stuff
    Magnitude and Direction
    Dot Product
    Cross Product
    Matrices
    Projection

    I feel like I just filled out a math teacher's resume.. Iunno what you need though.. be a little more specific. Also I'm not sure if I can offer "one on one" as I have a *hell of a lot of homework* at the moment but I can give pointers and answer a couple questions or post a couple solutions to questions if I spot them or if I'm even online. Post a problem that way I can answer it or if I'm not on, someone else can pitch in and contribute I guess.
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Aug 2008
    Location
    London, UK
    Posts
    456
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Awesome, thank you for your reply, those are exactly the things I need help with. Sorry, I just didn't want to post the code and waste peoples time if they weren't going to understand it.

    I'm building a Ray Tracer and having some issues, I'm not sure what I'm doing wrong but the output seems very off. Let me know if you need more explanation.

    Simba Code:
    program new;

    type TVector = record // Also using this for 3D Points, maybe rename to avoid confusion
        x: Integer;
        y: Integer;
        z: Integer;
    end;

    type TTriangle = record
        p1: TVector;
        p2: TVector;
        p3: TVector;
    end;

    type TTriangleMesh = record
        nt: Integer; // number of triangles
        nv: Integer; // number of vertices
        vi: array of Integer; // array of vertex indices
        p: array of TVector; // array of nv vertex positions
    end;

    type TRay = record
        o: TVector; // origin
        d: TVector; // direction
    end;


    function Vector(x, y, z: Integer): TVector;
    begin
            Result.x := x;
            Result.y := y;
            Result.z := z;
    end;

    var
      Image_Width, Image_Height: Integer;
      ImagePlanePosition, CameraPosition: TVector;
      ATriangle: TTriangle;
      MainForm: TForm;
      Image1 : TImage;
      Btn1: TButton;


    procedure DeclareScene;
    begin

      // number of triangles

      ATriangle.p1 := Vector(0, 0, 0);
      ATriangle.p2 := Vector(50, 50, 0);
      ATriangle.p3 := Vector(0, 50, 0);


      Image_Width := 200;
        Image_Height := 115;

      ImagePlanePosition := Vector(0, 0, -150);
      CameraPosition := Vector(0, 0, -250);
    end;





    // Vector Functions


    function V_Add(vr1, vr2: TVector): TVector;
    begin
      Result.x := vr1.x + vr2.x;
      Result.y := vr1.y + vr2.y;
      Result.z := vr1.z + vr2.z;
    end;

    function V_Subtract(vr1, vr2: TVector): TVector;
    begin
      Result.x := vr1.x - vr2.x;
      Result.y := vr1.y - vr2.y;
      Result.z := vr1.z - vr2.z;
    end;

    function Dot(vr1, vr2: TVector): Extended;
    begin
        Result := vr1.x * vr2.x + vr1.y * vr2.y + vr1.z * vr1.z;
    end;

    function Cross(vr1, vr2: TVector): TVector;
    begin
        Result := Vector(   (vr1.y * vr2.z) - (vr1.z * vr2.y),
                                            (vr1.z * vr2.x) - (vr1.x * vr2.z),
                                            (vr1.x * vr2.y) - (vr1.y * vr2.x));
    end;

    function IntersectTriangle(Ray: TRay; Triangle: TTriangle): Boolean;
    var
        e1, e2, s1, s2, T: TVector;
        Divisor, invDivisor, b1, b2: Extended;
    begin

        e1 := V_Subtract(Triangle.p2, Triangle.p1);
        e2 := V_Subtract(Triangle.p3, Triangle.p1);
        s1 := Cross(Ray.d, e2);
        Divisor := Dot(s1, e1);
        if (Divisor = 0) then
        begin

            Result := False;
            Exit;
        end;
        invDivisor := 1.0 / Divisor;
      //WriteLn(Divisor);


        T := V_Subtract(Ray.o, Triangle.p1);
        b1 := Dot(T, s1) * invDivisor;
        if (b1 < 0) or (b1 > 1) then
        begin
            Result := False;
            Exit;
        end;

        s2 := Cross(T, e1);
        b2 := Dot(Ray.d, s2) * invDivisor;
        if (b2 < 0) or ((b1 + b2) > 1) then
        begin
            Result := False;
            Exit;
        end;

      Result := True;
    end;


    procedure Camera;
    var
      cx, cy: Integer;
      tmpRay: TRay;
    begin
      for cy := 0 to Image_Height do
        for cx := 0 to Image_Width do
        begin
          tmpRay.o := CameraPosition;
          tmpRay.d := Vector(cx - (Image_Width / 2), cy - (Image_Height / 2), ImagePlanePosition.z);

          if IntersectTriangle(tmpRay, aTriangle) then
          begin
            Image1.CANVAS.Rectangle(cx, cy, cx+2, cy+2);
            //writeln('hmm');
          end;


        end;

    end;

    procedure start(Sender:TObject);
    begin
      Camera;
    end;

    procedure InitForm;
    begin
      MainForm := TForm.Create(nil);
      with MainForm do
      begin
        Position := poScreenCenter;
        Width := Image_Width;
        Height := Image_Height+20;

        Caption := 'RayCaster';
      end;

      Image1 := TImage.Create(MainForm);
      Image1.Parent := MainForm;
      Image1.Left := 0;
      Image1.Top := 0;
      Image1.Enabled := True;
      Image1.Width := Image_Width;
      Image1.Height := Image_Height;

      Image1.Canvas.Pen.Color := clRed;
      Image1.Canvas.Brush.Color := clRed;
      Image1.Canvas.Pen.Width := 1;

      Btn1 := TButton.Create(MainForm);
      Btn1.Parent := MainForm;
      Btn1.Left := 0;
      Btn1.Top := Image_Height;
      Btn1.Enabled := True;
      Btn1.Width := Image_Width;
      Btn1.Height := 20;
      Btn1.Caption := 'Start';
      Btn1.ONCLICK := @Start;
    end;

    procedure SafeInitForm;
    var
      v: TVariantArray;
    begin
      setarraylength(V, 0);
      ThreadSafeCall('InitForm', v);
    end;

    procedure ShowFormModal;
    begin
      MainForm.ShowModal;
    end;

    procedure SafeShowFormModal;
    var
      v: TVariantArray;
    begin
      setarraylength(V, 0);
      ThreadSafeCall('ShowFormModal', v);
    end;

    begin
      DeclareScene;
      SafeInitForm;
      SafeShowFormModal;
    end.

  4. #4
    Join Date
    Jan 2011
    Posts
    121
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Fix this:

    Code:
    function Dot(vr1, vr2: TVector): Extended;
    begin
        Result := vr1.x * vr2.x + vr1.y * vr2.y + vr1.z * vr1.z;
    end;
    That's just one error I saw glancing through. I may have more time to look at the rest later.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •