Jump to content

Rotating A Model Via Vertexes


gamecreator
 Share

Recommended Posts

Could anyone let me know what's wrong with my code?

	for(int i=0; i<model->GetSurface(0)->CountVertices(); i++)
	{
		Vec3 pos=model->GetSurface(0)->GetVertexPosition(i);
		pos.x=pos.x*Math::Cos(angle)-pos.y*Math::Sin(angle);
		pos.y=pos.y*Math::Cos(angle)+pos.x*Math::Sin(angle);
		model->GetSurface(0)->SetVertexPosition(i, pos);
	}

It rotates the model but the more you do it, the smaller the model gets.  I suspect there's a better way to do this and I'm open to suggestions (but I need to do it at the vertex level).

Link to comment
Share on other sites

6 hours ago, macklebee said:

Just rotating the model rotates the vertices

Yes and no.  Visually, yes.  In actuality, no.  If you rotate a model and get a vertex position via

model->GetSurface(0)->GetVertexPosition(0)

for example, it will return the same vertex position after each turn.

I'm playing around with making a road snap to terrain and I want to be able to rotate the road and snap the moved vertices to the proper terrain height.

road.jpg

Link to comment
Share on other sites

Right now it takes any model you have (doesn't even have to be a road), snaps all of its vertexes to the terrain height below them and exports an OBJ.

Unfortunately I don't know anything about textured surfaces and UV coordinates and whatnot so the OBJ will be untextured so you need to retexture it in your modeling program.  If someone wants to add that functionality later, they're welcome to do it.  I kind of just brute-force the OBJ export.  It works fine at the moment but I would have loved to have people be able to rotate the road.

Link to comment
Share on other sites

5 hours ago, gamecreator said:

Yes and no.  Visually, yes.  In actuality, no.  If you rotate a model and get a vertex position via

model->GetSurface(0)->GetVertexPosition(0)

for example, it will return the same vertex position after each turn.

Actually, yes and yes. It is actually changing the position of the vertices if you just rotate the model. The problem is that GetVertexPosition() returns the local position of the vertex. You need to transform that to a global position.

window = Window:Create("Global/Local/Screen Example",0,0,800,600,Window.Center)
context = Context:Create(window)
world = World:Create()
camera = Camera:Create()
camera:Move(0,2,6)
light = DirectionalLight:Create()
light:SetRotation(35,35,0)

model = Model:Box()
model:SetColor(0.0,0.0,1.0)
model:SetPosition(0,2,9)
model:SetRotation(45,45,0)
surface = model:GetSurface(0)
marker = Model:Box()
marker:SetScale(.05,.05,.05)
marker:SetColor(1,0,0,1)

while window:KeyDown(Key.Escape)==false do
	if window:Closed() then break end
	
	model:Turn(0,-0.2,0.5)
	localpos = surface:GetVertexPosition(0)
	globalpos = Transform:Point(localpos,model,nil)
	screenpos = camera:Project(globalpos)
	marker:SetPosition(globalpos)

	Time:Update()
	world:Update()
	world:Render()

	context:SetBlendMode(Blend.Alpha)
	context:DrawText("Local Vertex Position: "..localpos:ToString(),2,2)
	context:DrawText("Global Vertex Position: "..globalpos:ToString(),2,22)
	context:DrawText("Vert0",screenpos.x,screenpos.y)
	context:SetBlendMode(Blend.Solid)
	context:Sync(true)
end

 

GlobalLocalScreen.jpg

  • Upvote 1

Win7 64bit / Intel i7-2600 CPU @ 3.9 GHz / 16 GB DDR3 / NVIDIA GeForce GTX 590

LE / 3DWS / BMX / Hexagon

macklebee's channel

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...