Jump to content

3D Point rotation


Recommended Posts

Sorry... This is more of a math question, really.

I made a simple little 3d box that you can move around in, I intend on being able to create objects and stuff in it, but I put that off because I don't know how to do point rotations.

I want to transform/rotate a set of points, given the center and the rotation angle.

I did some googling, and I came up with lots of "3D Rotation Matrix" stuff but with a geometry-level education, I'm not understanding it.

Can anyone give me a tip?

Heres my code, incase you want to know what it's for.

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <Misc.au3>
Global $w = 1000
Global $h = 800
Global $d = 800
Global $x_center = Round($w/2,0)
Global $y_center = Round($h/2,0)
$dll = DllOpen("User32.dll")
$Form1 = GUICreate("Form1", $w+100, $h + 100, -1, -1)
$Stats_1 = GUICtrlCreateLabel("",0,100,200,200)
GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
GUISetState(@SW_SHOW)
Dim $ava[1][3]
$ava[0][0] = 0
$ava[0][1] = 0
$ava[0][2] = 0
$f_skip = 2 ;frame skip
$fc= 0
#cs
;Draw Grid
For $z_x = 0 to $w
    For $z_y = 2 to 800
        For $z_z = 0 to 150
            drawPoint($z_x,$z_y,$z_z)
            $z_z += 50
        Next
        $z_y += 50
    Next
    $z_x += 50
Next
#ce
;Draw Cube
drawPoint(0,0,0)
drawPoint($w,0,0)
drawPoint($w,$h,0)
drawPoint(0,$h,0)
drawPoint(0,0,$d)
drawPoint($w,0,$d)
drawPoint($w,$h,$d)
drawPoint(0,$h,$d)
$x_vel = 0
$y_vel = 0
$z_vel = 0
$lastpoint = drawPoint(0,0,0)
$posChange = False
While 1
    calcInput()
    calcPhys()
    $nMsg = GUIGetMsg()
    $Stats = "X: " & Round( $ava[0][0] , 2)
    $Stats &= @CRLF & "Y: " & Round( $ava[0][1] , 2)
    $Stats &= @CRLF & "Z: " & Round( $ava[0][2] , 2)
    $Stats &= @CRLF & "Xvel: " & Round( $x_vel , 2)
    $Stats &= @CRLF & "Yvel: " & Round( $y_vel , 2)
    $Stats &= @CRLF & "Zvel: " & Round( $z_vel , 2)
    GUICtrlSetData($Stats_1,$Stats)
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

GUISetState(@SW_SHOW)
Func drawObject($obj)
    For $i = 0 to UBound($obj) - 1
        $obj[$i][0] = drawPoint($obj[$i][1],$obj[$i][2],$obj[$i][3])
    Next
    Return $obj
EndFunc
Func drawPoint($x,$y,$z)
    $y = ($h-$y)
    $x_dist = ($x_center - $x) / 10
    $y_dist = ($y_center - $y) / 10
    $point = GUICtrlCreateLabel("+", $x + ($x_dist * ($z/100)), $y + ($y_dist * ($z/100)), 50, 50,$SS_CENTER)
    $size = 50 - ((32 / $d) * $z)
    If $size < 1 Then $size = 1
    GUICtrlSetFont(-1, $size)
    $color = Round( ($z / $d) * 99, 0 )
    If $color > 99 then $color = 99
    If $color < 0 Then $color = 0
    GUICtrlSetColor(-1, "0x00" & $color & "00")
    GUICtrlSetBkColor(-1, $GUI_BKCOLOR_TRANSPARENT)
    Return $point
EndFunc
Func calcPhys()
    $ava[0][0] += $x_vel
    $ava[0][1] += $y_vel
    $ava[0][2] += $z_vel
    If $x_vel > 1 Then
        $x_vel -= 0.5
    ElseIf $x_vel < -1 Then
        $x_vel += 0.5
    Else
        $x_vel = 0
    EndIf

    If $z_vel > 1 Then
        $z_vel -= 0.5
    ElseIf $z_vel < -1 Then
        $z_vel += 0.5
    Else
        $z_vel = 0
    EndIf
    If $ava[0][0] < 0 Then
        $ava[0][0] = 0
        $x_vel *= -2/3
    ElseIf $ava[0][0] > $w Then
        $ava[0][0] = $w
        $x_vel *= -2/3
    EndIf
    If $ava[0][1] < 0 Then
        $ava[0][1] = 0
        If Abs($y_vel) < 1 Then
        $y_vel = 0
        Else
        $y_vel *= -2/3
        EndIf
    ElseIf $ava[0][1] > $h Then
        $ava[0][1] = $h
        $y_vel *= -1
    EndIf
    If $ava[0][2] < 0 Then
        $ava[0][2] = 0
        $z_vel *= -2/3
    ElseIf $ava[0][2] > $d Then
        $ava[0][2] = $d
        $z_vel *= -2/3
    EndIf
    If $fc = $f_skip Then
    GUICtrlDelete($lastpoint)
    $lastpoint = drawPoint($ava[0][0],$ava[0][1],$ava[0][2])
    $fc = 0
    Else
    $fc +=1
    EndIf
$y_vel -= 2
EndFunc
Func calcInput()
    If _IsPressed(25,$dll) Then
        $x_vel -= 2
    EndIf
    If _IsPressed(27,$dll) Then
        $x_vel += 2
    EndIf
    If _IsPressed(26,$dll) Then
        $z_vel += 2
    EndIf
    If _IsPressed(28,$dll) Then
        $z_vel -= 2
    EndIf
    If _IsPressed(20,$dll) Then
        $y_vel += 6
    EndIf
EndFunc

[center]"When you look at old, classic games like Snake, you often put it off because it's such a simple game, but it's only when you actually try and create your own unique game from scratch, do you finally appreciate those games."[/center][center]Don't ask for answers if you haven't TRIED yet![/center][center]Most answers can be answered in the help file! Use it![/center]

Link to comment
Share on other sites

Or have a look to #09 Rotating Cube in Some Graphical Examples Using GDI+!

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Sorry... This is more of a math question, really.

No problem so far.

I want to transform/rotate a set of points, given the center and the rotation angle.

I did some googling, and I came up with lots of "3D Rotation Matrix" stuff but with a geometry-level education, I'm not understanding it.

In short, you need to know that rotations in 3-D space are not commutative. Then, there are two main common approaches to computing 3-D rotations: the [messy] so-called Euler angles and the _much_ simpler quaternions. You'll have to do you own searches for all these terms, concepts and possible implementations. Honestly, it isn't very difficult and if you have a ounce of motivation, you'll do it.

Euler angles: here (gimbal lock included, free of charge)

Quaternions used for computing rotations: here

I'd like to have more time to help you on this, but I'm currently too busy for doing so. Sorry for that.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...