|
-
Jul 1st, 2010, 09:51 AM
#1
Thread Starter
Lively Member
Game Engine: Problem with bounding boxes
Here are excerpts from my code. I cannot figure out
"ComputeBoundingBoxes" or "GetMatrixRot". I also wanted to use rot with GetMeshPos and GetOrigMeshPos, GetOffMeshPos... .m41,m42,m43 are all wrong when you rotate a mesh...
having tried NUMEROUS PERMUTATIONS.
The DirectX book said:
x1=(x*m11)+(y*m21)+(z*m31)+m41
y1=(x*m12)+(y*m22)+(z*m32)+m42
z1=(x*m13)+(y*m23)+(z*m33)+m43
But this did not work, so I've played with it a whole lot -for weeks-
with different offset values and variables- and didn't figure it out. 
Vectors vs. Points maybe...???
Basically, I want the 2D bounding boxes (which we stand on) to follow the position and rotation of the parent mesh at its angle and position relative TO the parent mesh's position, at its distance FROM the parent mesh.
The polygon must stay flat on the x,z plane. I can't just make them meshes because I'm using createpolygonrgn to detect whether the player is standing in one or not.
For example, I have a merry-go-round with bounding rectangles on each of the horses for the player to sit on. These follow a single mesh's rotation. There's also a ferris wheel. Bear in mind that they also rotate as the merry-go-round rotates.
Code:
Public Sub Render()
.
.
.
ResetPositions
'ANIMATE ALL UNSKINNED MESHES AFTER THIS POINT*********
'Rotations first, Positions next
Select Case Scene(0).FileName
Case "CANDYLAND.X"
If Not NotFirstFrame Then 'Dunking horse problem
SetPos , "HorseEven", "", 0, 5
SetPos , "HorseOdd", "", 0, 5
SetWlkRelative "HorseEven"
SetWlkRelative "HorseOdd"
End If
'Establish original positions
SetRot , "GoRound", "", 0, Scene(0).Mesh(5).HRot.Y
SetRot , "HorseEven", "", 0, Scene(0).Mesh(5).HRot.Y
SetRot , "HorseOdd", "", 0, Scene(0).Mesh(5).HRot.Y
SetPos , "HorseEven", "", 0, -GetOffMeshPos(, "HorseEven").Y
SetPos , "HorseOdd", "", 0, -GetOffMeshPos(, "HorseOdd").Y
If RenderEnv0Index <> -1 Then
'Perform offset motions
If Wlk(RenderEnv0Index).Reference = "MerryGo.WAV" Then 'Slow Rotation
SetRot , "GoRound", "", 0, -0.05
SetRot , "HorseEven", "", 0, -0.05
SetRot , "HorseOdd", "", 0, 0 - 0.05
SetPos , "HorseEven", "", 0, 0.004 * RTim
SetPos , "HorseOdd", "", 0, 0.004 * -RTim
End If
'Perform offset motions
If Wlk(RenderEnv0Index).Reference = "MerryGoLoud.WAV" Then
SetRot , "GoRound", "", 0, -0.1
SetRot , "HorseEven", "", 0, -0.1
SetRot , "HorseOdd", "", 0, -0.1
SetPos , "HorseEven", "", 0, 0.004 * RTim
SetPos , "HorseOdd", "", 0, 0.004 * -RTim
End If
If RTimSwitch = False Then 'Special offset timer with equal integer values
RTim = RTim + 1: If RTim > 48 Then RTim = 48: RTimSwitch = True
Else
RTim = RTim - 1: If RTim < -48 Then RTim = -48: RTimSwitch = False
End If
End If
End Select
.
.
.
Public Sub ComputeBoundingBoxes()
Dim Mat0 As D3DMATRIX
Dim Mat1 As D3DMATRIX
Dim Mat2 As D3DMATRIX
Dim Mat3 As D3DMATRIX
Dim P0 As D3DVECTOR
Dim P1 As D3DVECTOR
Dim P2 As D3DVECTOR
Dim P3 As D3DVECTOR
Dim TM As D3DVECTOR
Dim A As D3DVECTOR
Dim x0 As Single
Dim Y0 As Single
Dim Z0 As Single
Dim f() As Integer
For T = 0 To BlkCount - 1
With Blk(T)
For U = 0 To UBound(.P)
.Pol(U) = .OPol(U)
.P(U) = .OP(U)
Next U
If .Follows <> "None" Then
'If Left$(.Follows, 6) = "ZDoorl" Then Stop
f = SeekBone("", .Follows, 0)
If f(0) <> -1 And f(1) <> -1 Then
P2 = .Pos
P3 = .OPos
GoSub Calc
.Rot = Scene(f(0)).Mesh(f(1)).Rot.Y 'Rotate ourself as we go around
.Pos = P2
'If .Relative = True Then .Pos.Y = -.Pos.Y
.Sca.X = .Sca.X * Scene(f(0)).Mesh(f(1)).Sca.X
.Sca.Y = .Sca.Y * Scene(f(0)).Mesh(f(1)).Sca.Y
.Sca.Z = .Sca.Z * Scene(f(0)).Mesh(f(1)).Sca.Z
For U = 0 To UBound(.P)
.P(U).X = .Pos.X - ((Sin((.Rot + .Pol(U).Ang) * (PI / 180)) * (.Pol(U).D * .Sca.X)))
.P(U).Y = .Pos.Z - ((Cos((.Rot + .Pol(U).Ang) * (PI / 180)) * (.Pol(U).D * .Sca.Z)))
Next U
End If 'Found parent object
End If 'Follower?
End With
Next T
For T = 0 To WlkCount - 1
With Wlk(T)
.Sca.X = 1: .Sca.Y = 1: .Sca.Z = 1
For U = 0 To UBound(.P)
.Pol(U) = .OPol(U)
.P(U) = .OP(U)
Next U
If .Follows <> "None" Then
f = SeekBone("", .Follows, 0)
If f(0) <> -1 And f(1) <> -1 Then
P2 = .Pos
P3 = .OPos
'If .Name = "Horse01" And .Follows = "HorseOdd" And Scene(f(0)).Mesh(f(1)).Rot.Y <> 0 Then Stop
GoSub Calc
.Rot = Scene(f(0)).Mesh(f(1)).Rot.Y 'Rotate ourself as we go around
.Pos = P2
'If .Name = "Horse01" Then Form1.Touching.Caption = Str(.Pos.Y)
'If .Relative = True Then .Pos.Y = -.Pos.Y
.Sca.X = .Sca.X * Scene(f(0)).Mesh(f(1)).Sca.X
.Sca.Y = .Sca.Y * Scene(f(0)).Mesh(f(1)).Sca.Y
.Sca.Z = .Sca.Z * Scene(f(0)).Mesh(f(1)).Sca.Z
For U = 0 To UBound(.P)
.P(U).X = .Pos.X - ((Sin((.Rot + .Pol(U).Ang) * (PI / 180)) * (.Pol(U).D * .Sca.X)))
.P(U).Y = .Pos.Z - ((Cos((.Rot + .Pol(U).Ang) * (PI / 180)) * (.Pol(U).D * .Sca.Z)))
Next U
End If 'Found parent object
End If 'Follower?
End With
Next T
Exit Sub
Calc:
With Scene(f(0)).Mesh(f(1))
P0 = GetMeshPos(f(0), f(1))
P1 = GetOrigMeshPos(f(0), f(1))
rx = .Rot.X 'Paren't rot
ry = .Rot.Y 'All we can DO for NOW... -=CW/CCW axis
rz = .Rot.Z
Mat0 = .m_MacroMat(0)
'Funny little calc #!@# to prepare p0 for rotation - maybe not...
'TM.X = -TM.X
'P1.X = -P1.X
'P1.Z = -P1.Z
'TM.X = (P1.X - TM.X) + P1.X
'TM.Y = (P1.Y - TM.Y) + P1.Y
'TM.Z = (P1.Z - TM.Z) + P1.Z
A.X = .m_FrameMat(0).m41
A.Y = .m_FrameMat(0).m42
A.Z = .m_FrameMat(0).m43
TM = GetMatrixRot(Mat0, P0, A) 'rotate a vector around a matrix and a vector
End With
'Now rotate us around OUR PARENTS center
'D3DXMatrixIdentity Mat1
'P1.X = -P1.X
'P1.Z = -P1.Z
'P0.X = -P0.X
'P0.Z = -P0.Z
'P0.X = (P1.X - P0.X) - P1.X
'P0.Y = (P1.Y - P0.Y) + P1.Y
'P0.Z = (P1.Z - P0.Z) - P1.Z
'P0.X = -P0.X
'P0.Z = -P0.Z
'P3.X = -P3.X
'TM = AddVec(P0, P1)
'TM = AddVec(TM, P1)
'TM = AddVec(TM, P1)
P2 = AddVec(P2, TM)
Return
End Sub
Public Function GetMatrixRot(M As D3DMATRIX, U As D3DVECTOR, V As D3DVECTOR) As D3DVECTOR
Dim T As D3DVECTOR
'T.X = (-(M.m41 * M.m11) - ((M.m42 * M.m21) - (M.m43 * M.m31)))
'T.X = (M.m41 * M.m11) - (M.m42 * M.m21) - (M.m43 * M.m31)
'T.Y = (M.m41 * M.m12) - (M.m42 * M.m22) - (M.m43 * M.m32)
'T.Z = (M.m41 * M.m13) - (M.m42 * M.m23) - (M.m43 * M.m33)
'T.X = (M.m43 * M.m31) + (M.m42 * M.m21) + (M.m41 * M.m11)
'T.Y = (M.m43 * M.m32) + (M.m42 * M.m22) + (M.m41 * M.m12)
'T.Z = (M.m43 * M.m33) + (M.m42 * M.m23) + (M.m41 * M.m13)
T.X = (U.X * M.m11) + (U.Y * M.m21) + (U.Z * M.m31) + V.X
T.Y = (U.X * M.m12) + (U.Y * M.m22) + (U.Z * M.m32) + V.Y
T.Z = (U.X * M.m13) + (U.Y * M.m23) + (U.Z * M.m33) + V.Z
GetMatrixRot = T
End Function
Public Function GetMeshPos(Optional SceneNum, Optional MeshName, Optional BoneName) As D3DVECTOR
Dim T As D3DVECTOR
Dim U As D3DVECTOR
Dim c() As Integer
If IsMissing(SceneNum) Then SceneNum = ""
If IsMissing(MeshName) Then MeshName = ""
If IsMissing(BoneName) Then BoneName = ""
c = SeekBone(SceneNum, MeshName, BoneName)
If c(0) <> -1 And c(1) <> -1 And c(2) <> -1 Then
T.X = Scene(c(0)).Mesh(c(1)).m_FrameMat(c(2)).m41
T.Y = Scene(c(0)).Mesh(c(1)).m_FrameMat(c(2)).m42
T.Z = Scene(c(0)).Mesh(c(1)).m_FrameMat(c(2)).m43
GetMeshPos = T
End If
End Function
Public Function GetOffMeshPos(Optional SceneNum, Optional MeshName, Optional BoneName) As D3DVECTOR
Dim c() As Integer
Dim T As D3DVECTOR
Dim U As D3DVECTOR
If IsMissing(SceneNum) Then SceneNum = ""
If IsMissing(MeshName) Then MeshName = ""
If IsMissing(BoneName) Then BoneName = ""
c = SeekBone(SceneNum, MeshName, BoneName)
If c(0) <> -1 And c(1) <> -1 And c(2) <> -1 Then
T.X = Scene(c(0)).Mesh(c(1)).m_MacroMat(c(2)).m41
T.Y = Scene(c(0)).Mesh(c(1)).m_MacroMat(c(2)).m42
T.Z = Scene(c(0)).Mesh(c(1)).m_MacroMat(c(2)).m43
T = InvVec(T)
'T.X = -T.X
'U = GetMatrixPos(Scene(c(0)).Mesh(c(1)).m_FrameRestore(c(2)))
'U.Y = -U.Y
'U.Z = -U.Z
'Funny little calc #!@#
'T.X = (U.X - T.X) + U.X
'T.Y = (U.Y - T.Y) + U.Y
'T.Z = (U.Z - T.Z) + U.Z
GetOffMeshPos = T
End If
End Function
Public Function GetOrigMeshPos(Optional SceneNum, Optional MeshName, Optional BoneName) As D3DVECTOR
Dim c() As Integer
Dim T As D3DVECTOR
If IsMissing(SceneNum) Then SceneNum = ""
If IsMissing(MeshName) Then MeshName = ""
If IsMissing(BoneName) Then BoneName = ""
c = SeekBone(SceneNum, MeshName, BoneName)
If c(0) <> -1 And c(1) <> -1 And c(2) <> -1 Then
T.X = Scene(c(0)).Mesh(c(1)).m_FrameRestore(c(2)).m41
T.Y = Scene(c(0)).Mesh(c(1)).m_FrameRestore(c(2)).m42
T.Z = Scene(c(0)).Mesh(c(1)).m_FrameRestore(c(2)).m43
GetOrigMeshPos = T
End If
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|