Hello all!
I've been doing a lot of reading on the API. I'm picking things up well but I wanted to make sure I understood some default settings when adding nodes.
Am I correct when I say that addAnimatedMeshSceneNode( mesh ) creates a node, unless specified, at a default position of vector3d(0,0,0) ?
Does the same go for addCameraSceneNode() and addCubeSceneNode()?
I just wanted to make sure I understood that basic idea.
I am fairly confident I am reading the documentation correctly.
irr::scene::ISceneManager::addCubeSceneNode (
32 size = 10.0f,
ISceneNode * parent = 0,
s32 id = -1,
const core::vector3df & position = core::vector3df(0, 0, 0),
const core::vector3df & rotation = core::vector3df(0, 0, 0),
const core::vector3df & scale = core::vector3df(1.0f, 1.0f, 1.0f)
)
Default node positions.
-
jimburnettva
- Posts: 13
- Joined: Fri Feb 19, 2010 5:07 pm
- Contact:
Re: Default node positions.
yes, you did !!!jimburnettva wrote:I am fairly confident I am reading the documentation correctly.
while(!asleep) sheep++;
IrrExtensions:
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
IrrExtensions:

http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
Seriously booe, what the hell? You know where under Beginners Help it says this?
Second, I've seen way worse questions from people, some who don't even seem like they know anything about programming.
Third, taking up 100+ lines to use a massive font and waste space is something I'd expect an elementary school kid to do.
Knock it off.
First of all, this is the Beginner's Help forum, so that kind of question is fine.You may also post general programming questions here.
Second, I've seen way worse questions from people, some who don't even seem like they know anything about programming.
Third, taking up 100+ lines to use a massive font and waste space is something I'd expect an elementary school kid to do.
Knock it off.
-
jimburnettva
- Posts: 13
- Joined: Fri Feb 19, 2010 5:07 pm
- Contact:
Not worried about booe. I'm used to idiots on the net. Anyway, I'm new to both C++ and 3D stuf and just wanted to make sure I understood the API.
On that note. I built a small class (using the Cockput code) based on code I found around here. I also added some logic to determine which direction you are facing and to fly in that direction. (See forward object).
I also dropped Receiver into a class too "AppEventReceiver".
irrHelloWorld.cpp
On that note. I built a small class (using the Cockput code) based on code I found around here. I also added some logic to determine which direction you are facing and to fly in that direction. (See forward object).
I also dropped Receiver into a class too "AppEventReceiver".
irrHelloWorld.cpp
Code: Select all
#include <irrlicht.h>
#include <CockpitCamera.h>
#include <AppEventReceiver.cpp>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#ifdef _IRR_WINDOWS_
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
int main()
{
AppEventReceiver receiver;
ChaseCamera cc;
CockpitCamera mtc;
IrrlichtDevice *device = createDevice( video::EDT_OPENGL, dimension2d<u32>(640, 480), 32, false, false, false, &receiver);
if (!device)
return 1;
device->setWindowCaption(L"Hello World!");
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
//Load the mesh (3d model)
IAnimatedMesh* shipmesh = smgr->getMesh("./media/spaceshuttle.3ds");
ICursorControl* MyCursor = device->getCursorControl();
MyCursor->setVisible(false); //hides our cursor
IGUIImage* MyImage = device->getGUIEnvironment()->addImage(driver->getTexture("./media/crosshair.tga"),core::position2d<s32>(0,0));
if (!shipmesh) { device->drop(); return 1; }
IAnimatedMeshSceneNode* ship = smgr->addAnimatedMeshSceneNode( shipmesh );
smgr->setAmbientLight(video::SColorf(0.3,0.3,0.3,1));
ILightSceneNode* light1 = smgr->addLightSceneNode( 0, core::vector3df(0,100,-100), video::SColorf(0.3f,0.3f,0.3f), 200.0f, 1 );
smgr->addSkyDomeSceneNode(driver->getTexture("./media/stars1.jpg"),64, 64, 1.0f, 2.0f);
IMeshSceneNode* sphere1 = smgr->addSphereSceneNode();
sphere1->setPosition(core::vector3df(850,-20,250));
sphere1->setScale(core::vector3df(500,500,500));
sphere1->setMaterialFlag(EMF_LIGHTING, false);
//sphere1->setMD2Animation(scene::EMAT_STAND);
sphere1->setMaterialTexture( 0, driver->getTexture("./media/jupitor.jpg") );
if (ship)
{
ship->setMaterialFlag(EMF_LIGHTING, true);
ship->setMD2Animation(scene::EMAT_STAND);
ship->setMaterialTexture( 0, driver->getTexture("./media/spaceorb.bmp") );
ship->getMaterial(0).Shininess = 20.0f;
}
scene::ICameraSceneNode * camera = smgr->addCameraSceneNode();
//camera->setRotation(vector3df(0,300,0));
// As in example 04, we'll use frame rate independent movement.
//This seems to have a smoothing effect.
u32 then = device->getTimer()->getTime();
const f32 MOVEMENT_SPEED = 50.f;
int oldX = receiver.GetMouseState().Position.X;
int oldY = receiver.GetMouseState().Position.Y;
const f32 ROTATION_SPEED = 30.0f; // how fast camera rotates
mtc.makeCockpit(camera,ship,vector3df(0,400,-800));
while(device->run())
{
core::stringw tmp(L"Mouse [");
tmp += receiver.MouseState.Position.X;
tmp += ",";
tmp += receiver.MouseState.Position.Y;
tmp += "]";
bool moving = 0;
bool rotating = 0;
// Work out a frame delta time.
const u32 now = device->getTimer()->getTime();
const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds
then = now;
tmp += "[time ";
tmp += now;
tmp += "]";
//makeCockpit(camera, ship,20);
/* Check if keys UP, DOWN, LEFT or RIGHT are being held down, and move the
camre around respectively. */
/* old stuff, saving for future use. */
then = now;
core::vector3df nodePosition = ship->getPosition();
int X = MyCursor->getPosition().X;
int Y = MyCursor->getPosition().Y;
MyImage->setRelativePosition(position2di(X,Y));
vector3df camTarget = camera->getTarget();
vector3df camPosition = camera->getPosition();
//vector3df camPosition = core::vector3df(camera->getPosition().X,camera->getPosition().Y+100,camera->getPosition().Z);
vector3df camDirection = camTarget - camPosition;
vector3df camMovement = camDirection.normalize();
camMovement = camMovement * MOVEMENT_SPEED * frameDeltaTime;
vector3df forward( sin( ship->getRotation().Y*PI/180.0f ), sin( ship->getRotation().X*PI/180.0f )*-1, cos( ship->getRotation().Y*PI/180.0f ) );
if(receiver.IsKeyDown(irr::KEY_KEY_D))
{
ship->setRotation(core::vector3df(ship->getRotation().X ,ship->getRotation().Y+ MOVEMENT_SPEED * frameDeltaTime ,ship->getRotation().Z));
}
if(receiver.IsKeyDown(irr::KEY_KEY_A))
{
ship->setRotation(core::vector3df(ship->getRotation().X ,ship->getRotation().Y - MOVEMENT_SPEED * frameDeltaTime,ship->getRotation().Z));
}
if(receiver.IsKeyDown(irr::KEY_KEY_E))
{
ship->setRotation(core::vector3df(ship->getRotation().X - MOVEMENT_SPEED * frameDeltaTime ,ship->getRotation().Y,ship->getRotation().Z));
}
if(receiver.IsKeyDown(irr::KEY_KEY_Q))
{
ship->setRotation(core::vector3df(ship->getRotation().X + MOVEMENT_SPEED * frameDeltaTime ,ship->getRotation().Y,ship->getRotation().Z));
}
//quit on escape
if(receiver.IsKeyDown(irr::KEY_ESCAPE))
{
device->drop();
return 0;
}
//node->setPosition( node->getPosition() + forward*10.0f );
if(receiver.IsKeyDown(irr::KEY_KEY_W))
nodePosition= ship->getPosition() + forward*10.0f;
else if(receiver.IsKeyDown(irr::KEY_KEY_S))
nodePosition= nodePosition= ship->getPosition() - forward*10.0f;
if (receiver.MouseState.RightButtonDown==true)
{
tmp += " Mouse Navgiation Activated.";
if (receiver.MouseState.Position.X > 220)
{
ship->setRotation(core::vector3df(ship->getRotation().X ,ship->getRotation().Y+ MOVEMENT_SPEED * frameDeltaTime ,ship->getRotation().Z));
}else if(receiver.MouseState.Position.X <200)
{
ship->setRotation(core::vector3df(ship->getRotation().X ,ship->getRotation().Y - MOVEMENT_SPEED * frameDeltaTime,ship->getRotation().Z));
}
if (receiver.MouseState.Position.Y > 220)
{
ship->setRotation(core::vector3df(ship->getRotation().X+ MOVEMENT_SPEED * frameDeltaTime ,ship->getRotation().Y ,ship->getRotation().Z));
}else if(receiver.MouseState.Position.Y <200)
{
ship->setRotation(core::vector3df(ship->getRotation().X - MOVEMENT_SPEED * frameDeltaTime ,ship->getRotation().Y,ship->getRotation().Z));
}
}
ship->setPosition(nodePosition);
mtc.makeCockpit(camera,ship,vector3df(0,400,-800));
// mtc.makeCockpit(camera,ship,vector3df(0,600,0));
// mtc.makeCockpit(camera,ship,core::vector3df(0,0,-2));
//core::vector3df shipPos = core::vector3df(ship->getPosition().X,ship->getPosition().Y + 600,ship->getPosition().Z);
// camera->setPosition(shipPos);
// camera->setRotation(core::vector3df(0,0,200));
// camera->setTarget(ship->getAbsolutePosition());
//ship->setRotation(nodePosition);
// core::vector3df mouseV2 = receiver.MouseState.Position.X;
/// core::vector3df camRot = core::vector3df(receiver.MouseState.Position.X , receiver.MouseState.Position.Y , 0 );
// core::vector3df camPos = core::vector3df(camera->getAbsolutePosition().X-23, camera->getAbsolutePosition().Y -10, camera->getAbsolutePosition().Z );
//core::vector3df shPos = core::vector3df(23,10,0);//default camera positions of ships was at 0,0,0.
//ship->setPosition(camPos);
// ship->setRotation(camRot);
// ship->setRotation(camPos);
// camera->setTarget(ship->getPosition());
device->setWindowCaption(tmp.c_str());
//Draw the scene/fame/etc...
driver->beginScene(true, true, SColor(255,0,0,0));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
device->drop();
return 0;
}
/*
* CockpitCamera.cpp
*
* Created on: Sep 3, 2010
* Author: jim
*/
#include "CockpitCamera.h"
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
const f32 MOVEMENT_SPEED = 50.f;
void CockpitCamera::makeCockpit(irr::scene::ICameraSceneNode *camera, //camera
irr::scene::IAnimatedMeshSceneNode *node, //scene node (ship)
irr::core::vector3df offset) //relative position of camera to node (ship)
{
// get transformation matrix of node
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
// transform forward vector of camera
irr::core::vector3df frv = irr::core::vector3df (0.0f, 00.f, 600.0f);
m.transformVect(frv);
// transform upvector of camera
irr::core::vector3df upv = irr::core::vector3df (0.0f, 1.0f, 0.0f);
m.transformVect(upv);
// transform camera offset (thanks to Zeuss for finding it was missing)
m.transformVect(offset);
// set camera
camera->setPosition(node->getPosition() + offset); //position camera in front of the ship
camera->setUpVector(upv); //set up vector of camera
camera->setTarget(node->getPosition() + frv); //set target of camera (look at point) (thx Zeuss for correcting it)
// update absolute position
camera->updateAbsolutePosition();
}
void CockpitCamera::rotate(irr::scene::ISceneNode *node, irr::core::vector3df rot)
{
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
irr::core::matrix4 n;
n.setRotationDegrees(rot);
m *= n;
node->setRotation( m.getRotationDegrees() );
node->updateAbsolutePosition();
}
//--- turn ship left or right ---
void CockpitCamera::turn(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(0.0f, rot, 0.0f) );
}
//--- pitch ship up or down ---
void CockpitCamera::pitch(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(rot, 0.0f, 0.0f) );
}
//--- roll ship left or right ---
void CockpitCamera::roll(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(0.0f, 0.0f, rot) );
}
void CockpitCamera::move(irr::scene::ISceneNode *node, //node to move
irr::core::vector3df vel) //velocity vector
// for example to move node 10 units forward use vector3df(0,0,10)
{
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
m.transformVect(vel);
node->setPosition(node->getPosition() + vel);
node->updateAbsolutePosition();
}
CockpitCamera::CockpitCamera() {
// TODO Auto-generated constructor stub
}
CockpitCamera::~CockpitCamera() {
// TODO Auto-generated destructor stub
}
And the header file:
/*
* CockpitCamera.h
*
* Created on: Sep 3, 2010
* Author: jim
*/
#include <irrlicht.h>
#ifndef COCKPITCAMERA_H_
#define COCKPITCAMERA_H_
class CockpitCamera {
public:
void makeCockpit(irr::scene::ICameraSceneNode *camera, irr::scene::IAnimatedMeshSceneNode *node, irr::core::vector3df offset);
void rotate(irr::scene::ISceneNode *node, irr::core::vector3df rot);
void turn(irr::scene::ISceneNode *node, irr::f32 rot);
void pitch(irr::scene::ISceneNode *node, irr::f32 rot);
void roll(irr::scene::ISceneNode *node, irr::f32 rot);
void move(irr::scene::ISceneNode *node, irr::core::vector3df vel);
CockpitCamera();
virtual ~CockpitCamera();
};
#endif /* COCKPITCAMERA_H_ */
/*
* AppEventReceiver.cpp
*
* Created on: Sep 3, 2010
* Author: jim
*/
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class AppEventReceiver : public IEventReceiver
{
public:
//This structure holds the state of the mouse.
struct SMouseState
{
core::position2di Position;
bool RightButtonDown;
SMouseState() : RightButtonDown(false) { }
} MouseState;
// This is the one method that we have to implement
virtual bool OnEvent(const SEvent& event)
{
if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
switch(event.MouseInput.Event)
{
case EMIE_RMOUSE_PRESSED_DOWN:
MouseState.RightButtonDown = true;
break;
case EMIE_RMOUSE_LEFT_UP:
MouseState.RightButtonDown = false;
break;
case EMIE_MOUSE_MOVED:
MouseState.Position.X = event.MouseInput.X;
MouseState.Position.Y = event.MouseInput.Y;
break;
default:
// We won't use the wheel
break;
}
}
// Remember whether each key is down or up
if (event.EventType == irr::EET_KEY_INPUT_EVENT)
{
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
}
return false;
}
// This is used to check whether a key is being held down
virtual bool IsKeyDown(EKEY_CODE keyCode) const
{
return KeyIsDown[keyCode];
}
const SMouseState & GetMouseState(void) const
{
return MouseState;
}
AppEventReceiver()
{
for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
KeyIsDown[i] = false;
}
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};