tgb: rotating a 3d model [fixed]
by Jeff Yaskus · in Torque Game Builder · 09/18/2011 (5:07 pm) · 2 replies
My goal:
1) Display a 3d model of earth that can be rotated and zoomed in/out.
2) Determine when/where player clicks the mouse
3) Then load missions based on location of globe clicked.
What I have tried:
a) added a 3d model of earth under ./data/shapes
b) created behavior to rotate and zoom in/out the model
d) created behavior to enable mouse click and mouseUp/mouseDown statement to echo the world space clicked.
... so far it all works and looks great. But I'm stumped on the next step ...
1) HOW can I go about translating the mouse click or cross hair location -- into something usable.
2) Is it possible to add an overlay or such on top of the 3d model - so it can "show" mission locations?
Can a static model somehow be mounted on the 3d model, at the target location?
OR -- have I reached the limits of 3D models in TGB?
Any suggestions are welcome
[edit]
Solved (1) by using the current xyz rotation of the globe 3d model when the player presses the mouse button.
However, (2) was more difficult ... when mounting an object to a 3d model, the object doesn't rotate with the model. (as expected) I opted to use multiple models, each with a different texture image that included missions, etc.
1) Display a 3d model of earth that can be rotated and zoomed in/out.
2) Determine when/where player clicks the mouse
3) Then load missions based on location of globe clicked.
What I have tried:
a) added a 3d model of earth under ./data/shapes
b) created behavior to rotate and zoom in/out the model
//-----------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//-----------------------------------------------------------------------------
if (!isObject(planetControlsBehavior))
{
%template = new BehaviorTemplate(planetControlsBehavior);
%template.friendlyName = "planet Controls";
%template.behaviorType = "Input";
%template.description = "planet style movement control";
%template.addBehaviorField(upKey, "Key to bind to rotate up", keybind, "keyboard up");
%template.addBehaviorField(downKey, "Key to bind to rotate down", keybind, "keyboard down");
%template.addBehaviorField(leftKey, "Key to bind to rotate left", keybind, "keyboard left");
%template.addBehaviorField(rightKey, "Key to bind to rotate right", keybind, "keyboard right");
%template.addBehaviorField(zoomInKey, "Key to bind to zoom in", keybind, "keyboard A");
%template.addBehaviorField(zoomOutKey, "Key to bind to zoom in", keybind, "keyboard Z");
%template.addBehaviorField(rotateSpeed, "The speed at which the object will rotate (degrees per second)", float, 15.0);
}
function planetControlsBehavior::onBehaviorAdd(%this)
{
if (!isObject(moveMap))
return;
moveMap.bindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), "moveUp", %this);
moveMap.bindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), "moveDown", %this);
moveMap.bindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), "moveLeft", %this);
moveMap.bindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), "moveRight", %this);
moveMap.bindObj(getWord(%this.zoomInKey, 0), getWord(%this.zoomInKey, 1), "zoomIn", %this);
moveMap.bindObj(getWord(%this.zoomOutKey, 0), getWord(%this.zoomOutKey, 1), "zoomOut", %this);
%this.startRotation = %this.owner.getShapeRotation();
%this.up = 0;
%this.down = 0;
%this.left = 0;
%this.right = 0;
%this.maxWidth = %this.owner.getWidth();
%this.maxHeight = %this.owner.getHeight();
%this.zoom = 1; // 100%
// enable the MOUSE
%this.owner.setUseMouseEvents(true);
}
function planetControlsBehavior::onBehaviorRemove(%this)
{
if (!isObject(moveMap))
return;
%this.owner.disableUpdateCallback();
moveMap.unbindObj(getWord(%this.upKey, 0), getWord(%this.upKey, 1), %this);
moveMap.unbindObj(getWord(%this.downKey, 0), getWord(%this.downKey, 1), %this);
moveMap.unbindObj(getWord(%this.leftKey, 0), getWord(%this.leftKey, 1), %this);
moveMap.unbindObj(getWord(%this.rightKey, 0), getWord(%this.rightKey, 1), %this);
moveMap.unbindObj(getWord(%this.zoomInKey, 0), getWord(%this.zoomInKey, 1), %this);
moveMap.unbindObj(getWord(%this.zoomOutKey, 0), getWord(%this.zoomOutKey, 1), %this);
%this.up = 0;
%this.down = 0;
%this.left = 0;
%this.right = 0;
}
// bigger
function planetControlsBehavior::zoomIn(%this, %val)
{
%this.up = %val;
%this.zoom += 0.1;
if (%this.zoom > 2.5)
%this.zoom = 2.5;
// calc the new sizes
%newWidth = %this.maxWidth * %this.zoom;
%newHeight = %this.maxHeight * %this.zoom;
// update the size on the owner of this behavior
%this.owner.setSize(%newWidth, %newHeight);
}
// smaller
function planetControlsBehavior::zoomOut(%this, %val)
{
%this.down = %val;
%this.zoom -= 0.1;
if (%this.zoom < 0.5)
%this.zoom = 0.5; // min 10% size
// calc the new sizes
%newWidth = %this.maxWidth * %this.zoom;
%newHeight = %this.maxHeight * %this.zoom;
// update the size on the owner of this behavior
%this.owner.setSize(%newWidth, %newHeight);
}
function planetControlsBehavior::moveUp(%this, %val)
{
%this.left = %val;
if (%val)
%this.owner.setShapeAngularVelocity( %this.rotateSpeed SPC "0" SPC "0");
else
%this.owner.setShapeAngularVelocity("0 0 0");
}
function planetControlsBehavior::moveDown(%this, %val)
{
%this.left = %val;
if (%val)
%this.owner.setShapeAngularVelocity( (-1 * %this.rotateSpeed) SPC "0" SPC "0");
else
%this.owner.setShapeAngularVelocity("0 0 0");
}
function planetControlsBehavior::moveLeft(%this, %val)
{
%this.left = %val;
if (%val)
%this.owner.setShapeAngularVelocity("0" SPC %this.rotateSpeed SPC "0");
else
%this.owner.setShapeAngularVelocity("0 0 0");
}
function planetControlsBehavior::moveRight(%this, %val)
{
%this.right = %val;
if (%val)
%this.owner.setShapeAngularVelocity("0" SPC (-1 * %this.rotateSpeed) SPC "0");
else
%this.owner.setShapeAngularVelocity("0 0 0");
}c) added a cross hair (static sprite) - centered on globe, which doesn't rotated) created behavior to enable mouse click and mouseUp/mouseDown statement to echo the world space clicked.
function planetControlsBehavior::onMouseDown(%this, %modifier, %worldPos)
{
echo("planetControlsBehavior::onMouseDown =" SPC %worldPos );
}
function planetControlsBehavior::onMouseUp(%this,%modifier,%worldPos, %a1, %a2, %a3)
{
echo("planetControlsBehavior::onMouseUp =" SPC %worldPos SPC "," SPC %a1 SPC "," SPC %a2 SPC "," SPC %a3 );
}... so far it all works and looks great. But I'm stumped on the next step ...
1) HOW can I go about translating the mouse click or cross hair location -- into something usable.
2) Is it possible to add an overlay or such on top of the 3d model - so it can "show" mission locations?
Can a static model somehow be mounted on the 3d model, at the target location?
OR -- have I reached the limits of 3D models in TGB?
Any suggestions are welcome
[edit]
Solved (1) by using the current xyz rotation of the globe 3d model when the player presses the mouse button.
However, (2) was more difficult ... when mounting an object to a 3d model, the object doesn't rotate with the model. (as expected) I opted to use multiple models, each with a different texture image that included missions, etc.
About the author
Long time gamer, hacker and programmer. With dreams of making video games -
#2
Was hoping to find a way to interact with the globe a bit more though ... so they could choose "where" to place their base and such.
Maybe getRotation() could still be used for that ... keep track of xy location when mouse is clicked. That will allow me to recenter map back to that location - maybe even "mount" a static sprite on top of it at that location.
09/19/2011 (9:52 pm)
oh - I see what you are saying. Present a list of missions like t3d does or such and rotate the globe in the background to center on each mission location. I like that idea.Was hoping to find a way to interact with the globe a bit more though ... so they could choose "where" to place their base and such.
Maybe getRotation() could still be used for that ... keep track of xy location when mouse is clicked. That will allow me to recenter map back to that location - maybe even "mount" a static sprite on top of it at that location.
Torque Owner Rpahut
Or you can getRotation of the globe and calculate side which is facing player.