Rolling sphere

BlitzMax Forums/MiniB3D Module/Rolling sphere

ima747(Posted 2007) [#1]
I'm sure there's an easy way to do this and I'm just too muddled to think of it right now. I have a sphere that needs to roll around. RotateMesh works perfectly for what I want, but it's way way way too slow to rotate all those points. Turn Entity looks like it works but the problem is if you roll forwards 90degrees now roll to the side, it spins instead of rolling because you've changed the orientation.

Do I have to keep track of orientation and apply my rotation depending on how it's already rotated? i.e. left may or may not be the objects roll, and may even be negative roll depending on how far it's pitched.

This seems overly complicated to me, maybe there's a way to reset the base orientation after I use turnentity... but that's basicly just rotatemesh and it's too slow.

Thoughts? sugestions?


simonh(Posted 2007) [#2]
In the latest version, unfortunately the unoptimised version of TransformMesh (used by RotateMesh) is used by default.

To fix this, in TMesh.bmx, change TransformMesh to TransformMesh0, and TransformMesh1 to TransformMesh. RotateMesh will be much faster now.


ima747(Posted 2007) [#3]
Aaah, very speedy rotate mesh now. Should I expect problems using this other transform mesh? (i.e. is there a reason this wasn't used in the first place?)

however I have to do updatenormals to get the lighting to work, which slows everything down agian. I assume the un-optomized transform updates the normals by itself since I didn't notice the lighting thing before.

Which brings me back to my real question, is there a way I can use turnentity for this without having to keep track of it's rotation to calculate new rotations from? (pitch forward 90 deg and now pitch left and right are spin and what was spin is now left and right, another 90 and left and right are backwards).

Update:
I suddenly remember the global parameter for turnentity which should do exactly what I want. however, it doesn't seem to have any effect. is this not implemented with turnentity?

Update2:
Just checked TEntity.bmx and it looks like there's a spot for the global paramater to work, but there's no code to actualy do it. Going to poke some more.


simonh(Posted 2007) [#4]
Here's a version which transforms the normals:

	Method TransformMesh(mat:TMatrix)

		For Local s=1 To no_surfs
	
			Local surf:TSurface=GetSurface(s)
				
			For Local v=0 To surf.no_verts-1
		
				Local vx#=surf.vert_coords[v*3]
				Local vy#=surf.vert_coords[v*3+1]
				Local vz#=surf.vert_coords[v*3+2]
	
				surf.vert_coords[v*3] = mat.grid#[0,0]*vx# + mat.grid#[1,0]*vy# + mat.grid#[2,0]*vz# + mat.grid#[3,0]
				surf.vert_coords[v*3+1] = mat.grid#[0,1]*vx# + mat.grid#[1,1]*vy# + mat.grid#[2,1]*vz# + mat.grid#[3,1]
				surf.vert_coords[v*3+2] = mat.grid#[0,2]*vx# + mat.grid#[1,2]*vy# + mat.grid#[2,2]*vz# + mat.grid#[3,2]

				Local nx#=surf.vert_norm[v*3]
				Local ny#=surf.vert_norm[v*3+1]
				Local nz#=surf.vert_norm[v*3+2]
	
				surf.vert_norm[v*3] = mat.grid#[0,0]*nx# + mat.grid#[1,0]*ny# + mat.grid#[2,0]*nz# + mat.grid#[3,0]
				surf.vert_norm[v*3+1] = mat.grid#[0,1]*nx# + mat.grid#[1,1]*ny# + mat.grid#[2,1]*nz# + mat.grid#[3,1]
				surf.vert_norm[v*3+2] = mat.grid#[0,2]*nx# + mat.grid#[1,2]*ny# + mat.grid#[2,2]*nz# + mat.grid#[3,2]

			Next
							
		Next

	End Method

There's no reason not to use it, it was just a mistake that the other function was left in use.

TurnEntity isn't implemented the same as in B3D - until MiniB3D switches to using quaternions, it won't be. Using RotateEntity with the global flag should do what you're hoping for though.


ima747(Posted 2007) [#5]
the new transformmesh is much better. fast and updates normals, but still not fast enough for what I need (though much much better for normal use).

RotateEntity tilts the object the amount specified, but I need the function of turnentity, where it remains rotated so the next rotation is applied to it. Might work if I keep track of my rotations, which is easier than tracking my tilts and calculating which rotation I need for turnentity based on that...

I'll keep poking, thanks for the help, as always it has been most enlightening. one of these days I'll start to wrap my head around the internals a bit more and won't have to bug you with these querys.

Any chance that change to quats is going to happen sooner rather than later? (I'm guessing not, as thinking about it scares the pants off me, so I imagine it's a little daunting)


simonh(Posted 2007) [#6]
It's something I definitely want to do, but probably not for another couple of updates.


z4g0(Posted 2007) [#7]
Hi, I'm developping a object placement editor with Minib3D, and I noticed rotation problems just now, too: If i parent a object to a pivot, and turn it, when I change a bit the "roll" value, the object start to turn randomly..


ima747(Posted 2007) [#8]
Alright I've been thinking about this a bit more and I think I have a solution but I can't quite get my head around the math.

I create a blank mesh, and in it I create a surface with 3 vertexes. One at 1,0,0 one at 0,1,0 and one at 0,0,1. now, I can use rotate mesh on this new mesh and since it's only moving 3 vertexes and has no normals it takes an infinitely small time regardless of how complex my real sphere is.

now I can the the x/y/z position of each of those vertexes which should give me a percentage of rotation in 180 degrees (from -1 to 1 being -180 degrees to 180 degrees) for each vertex for each x/y/z rotation.

I'd like to use rotateentity to spin the real object to match the rotation of the vertex mesh, but I can't get the math for sorting out the rotations and combining the vertex values together.

Maybe I'm going about this all wrong, but it's the best I could come up with.