Hi,
Ich habe eine Real Time Strategy Kamera Klasse erstellt welche einen eigenen Eventreceiver enthält. So wie die FPS Kamera zum Beispiel auch. Jetzt will ich aber für andere Events z.B. Anwählen von Objekten den MastEventReceiver verwenden. Nur scheinen sich die Kamera und der MastEventReceiver irgendwie in die Quere zu kommen, denn weder auswahl noch kamera funktionieren wenn beide aktiv sind.
hier Camera.h:
Code:
#ifndef __RTSCAMERA_H__
#define __RTSCAMERA_H__
#include <irrlicht.h>
#include <ICameraSceneNode.h>
#include <math.h>
class RTSCamera : public irr::scene::ICameraSceneNode
{
public:
RTSCamera(irr::IrrlichtDevice* devicepointer,irr::scene::ISceneNode* parent,irr::scene::ISceneManager* smgr,irr::s32 id,
irr::f32 rotateSpeed = -1000.0f,irr::f32 zoomSpeed = 1000.0f,irr::f32 translationSpeed = 1000.0f);
virtual ~RTSCamera();
//Hauptfunktionen um Kamera zu bewegen
void Movement();
void ChangeView();
void checkBound();
//Events
virtual bool OnEvent(const irr::SEvent& event);
virtual void OnRegisterSceneNode();
virtual void render();
virtual void OnPostRender(irr::u32 timeMs);
//Setup
virtual void setInputReceiverEnabled(bool enabled);
virtual bool isInputReceiverEnabled() const;
//Gets
virtual const irr::core::aabbox3d<irr::f32>& getBoundingBox() const;
virtual const irr::core::matrix4& getProjectionMatrix() const;
virtual const irr::scene::SViewFrustum* getViewFrustum() const;
virtual const irr::core::vector3df& getTarget() const ;
virtual const irr::core::matrix4& getViewMatrix() const;
virtual const irr::core::vector3df& getUpVector() const;
virtual irr::f32 getNearValue() const;
virtual irr::f32 getFarValue() const;
virtual irr::f32 getAspectRatio() const;
virtual irr::f32 getFOV() const;
//Sets
virtual void setRotation(const irr::core::vector3df& rotation);
virtual void setProjectionMatrix(const irr::core::matrix4& projection, bool isOrthogonal = false);
virtual void bindTargetAndRotation(bool bound);
virtual bool getTargetAndRotationBinding(void) const;
virtual void setNearValue(irr::f32 zn);
virtual void setFarValue(irr::f32 zf);
virtual void setAspectRatio(irr::f32 aspect);
virtual void setFOV(irr::f32 fovy);
virtual void setUpVector(const irr::core::vector3df& pos);
virtual void setProjectionMatrix(const irr::core::matrix4& projection);
virtual void setPosition(const irr::core::vector3df& newpos);
virtual void setTarget(const irr::core::vector3df& newpos);
//Helper Functions
void pointCameraAtNode(ISceneNode* selectednode);
void setMinZoom(irr::f32 amount);
void setMaxZoom(irr::f32 amount);
//Type Return
virtual irr::scene::ESCENE_NODE_TYPE getType() { return irr::scene::ESNT_CAMERA; }
protected:
//Properties
irr::core::vector3df Target;
irr::core::vector3df UpVector;
irr::core::matrix4 Projection;
irr::core::matrix4 View;
irr::scene::SViewFrustum ViewArea;
irr::core::aabbox3d<irr::f32> BBox;
bool InputReceiverEnabled;
irr::core::dimension2d<irr::f32> screenDim;
irr::f32 Fovy; //Field of view, in radians.
irr::f32 Aspect; //Aspect ratio.
irr::f32 ZNear; //Value of the near view-plane.
irr::f32 ZFar; //Z-value of the far view-plane.
void recalculateProjectionMatrix();
void recalculateViewArea();
private:
irr::IrrlichtDevice* device;
irr::core::vector3df Pos;
bool zooming,rotating,moving,translating;
irr::f32 zoomSpeed;
irr::f32 translateSpeed;
irr::f32 rotateSpeed;
irr::f32 rotateStartX, rotateStartY;
irr::f32 zoomStartX, zoomStartY;
irr::f32 translateStartX, translateStartY;
irr::f32 currentZoom;
irr::f32 oldZoom;
irr::f32 rotX, rotY;
irr::core::vector3df oldTarget;
irr::core::vector2df MousePos;
bool Keys[irr::KEY_KEY_CODES_COUNT];
bool MouseKeys[3];
irr::f32 targetMinDistance;
irr::f32 targetMaxDistance;
irr::f32 frameDeltaTime;
irr::video::IVideoDriver* driver;
irr::u32 then;
irr::u32 now;
int klick;
bool Bound;
//bool Break;
enum MOUSE_BUTTON
{
MOUSE_BUTTON_LEFT,
MOUSE_BUTTON_MIDDLE,
MOUSE_BUTTON_RIGHT
};
void allKeysUp();
void allMouseKeysUp();
bool isKeyDown(irr::s32 key);
bool isMouseKeyDown(irr::s32 key);
void updateAnimationState();
};
#endif
Camera.cpp:
Code:
#include "Camera.h"
#include "ICursorControl.h"
#include "ICameraSceneNode.h"
#include <iostream>
using namespace std;
using namespace irr;
RTSCamera::RTSCamera(IrrlichtDevice* devicepointer,scene::ISceneNode* parent,scene::ISceneManager* smgr,s32 id,
f32 rs,f32 zs,f32 ts)
: ICameraSceneNode(parent,smgr,id,core::vector3df(1.0f,1.0f,1.0f),core::vector3df(0.0f,0.0f,0.0f),
core::vector3df(1.0f,1.0f,1.0f)),InputReceiverEnabled(true)
{
device = devicepointer;
BBox.reset(0,0,0);
UpVector.set(0.0f,1.0f,0.0f);
Fovy = core::PI / 2.5f;
Aspect = 4.0f / 3.0f;
ZNear = 1.0f;
ZFar = 3000.0f;
video::IVideoDriver* d = device->getVideoDriver();
if (d)
{
screenDim.Width = (f32)d->getCurrentRenderTargetSize().Width;
screenDim.Height = (f32)d->getCurrentRenderTargetSize().Height;
Aspect = screenDim.Width / screenDim.Height;
driver = d;
}
zooming = false;
rotating = false;
moving = false;
translating = false;
zoomSpeed = zs;
rotateSpeed = rs;
translateSpeed = ts;
currentZoom = 100.0f;
targetMinDistance = 1.0f;
targetMaxDistance = 2000.0f;
Target.set(0.0f,0.0f,0.0f);
rotX = 0;
rotY = 0;
oldTarget = Target;
oldZoom = currentZoom;
klick=0;
then = device->getTimer()->getTime();
allKeysUp();
allMouseKeysUp();
recalculateProjectionMatrix();
recalculateViewArea();
smgr->setActiveCamera(this);
}
RTSCamera::~RTSCamera()
{
}
bool TargetAndRotationAreBound = false;
bool RTSCamera::OnEvent(const SEvent& event)
{
if (!InputReceiverEnabled)
return false;
ISceneNode* selectednode;
core::dimension2d<s32> ssize = SceneManager->getVideoDriver()->getScreenSize();
if(event.EventType == EET_MOUSE_INPUT_EVENT)
{
switch(event.MouseInput.Event)
{
case EMIE_LMOUSE_PRESSED_DOWN:
/* selectednode = SceneManager->
getSceneCollisionManager()->
getSceneNodeFromScreenCoordinatesBB(
device->getCursorControl()->getPosition(),0xFF,false);
if(selectednode){
getPosition();
pointCameraAtNode(selectednode);
}
else*/
MouseKeys[0] = true;
break;
case EMIE_RMOUSE_PRESSED_DOWN:
MouseKeys[2] = true;
//Zoom();
break;
case EMIE_MMOUSE_PRESSED_DOWN:
MouseKeys[1] = true;
ChangeView();
break;
case EMIE_LMOUSE_LEFT_UP:
MouseKeys[0] = false;
break;
case EMIE_RMOUSE_LEFT_UP:
MouseKeys[2] = false;
break;
case EMIE_MMOUSE_LEFT_UP:
MouseKeys[1] = false;
break;
case EMIE_MOUSE_MOVED:
MousePos.X = event.MouseInput.X / (f32)ssize.Width;
MousePos.Y = event.MouseInput.Y / (f32)ssize.Height;
Movement();
return true;
break;
case EMIE_MOUSE_WHEEL:
currentZoom -= event.MouseInput.Wheel * zoomSpeed;
Movement();
break;
}
return true;
}
if(event.EventType == EET_KEY_INPUT_EVENT)
{
Keys[event.KeyInput.Key] = event.KeyInput.PressedDown;
Movement();
return true;
}
return false;
}
//Bewegung der Kamera:
void RTSCamera::Movement(){
now = device->getTimer()->getTime();
frameDeltaTime = (f32)(now-then)/1000.f;
then=now;
const f32 MOVEMENT_SPEED = 400.f;
const f32 ZOOMING_SPEED = 400.f;
core::vector3df pos = getPosition();
core::vector3df target = getTarget();
//frameDelataTime muss noch so konfiguriert werden um eine von der Geschwindigkeit des PCs unabhängige Bewewgung zuerzeugen.
switch(klick){
case 0:
if ((MousePos.X < 0.1) || isKeyDown(KEY_LEFT)){
pos.X += MOVEMENT_SPEED*frameDeltaTime;
target.X += MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
else if (MousePos.X >0.90 || isKeyDown(KEY_RIGHT)){
pos.X -= MOVEMENT_SPEED*frameDeltaTime;
target.X -= MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
if (MousePos.Y < 0.10 || isKeyDown(KEY_UP)) {
pos.Z -= MOVEMENT_SPEED*frameDeltaTime;
target.Z -= MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
else if (MousePos.Y >0.90 || isKeyDown(KEY_DOWN)) {
pos.Z += MOVEMENT_SPEED*frameDeltaTime;
target.Z += MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
break;
case 1:
if ((MousePos.X < 0.1) || isKeyDown(KEY_LEFT)){
pos.Z -= MOVEMENT_SPEED*frameDeltaTime;
target.Z -= MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
else if (MousePos.X >0.90 || isKeyDown(KEY_RIGHT)){
pos.Z += MOVEMENT_SPEED*frameDeltaTime;
target.Z += MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
if (MousePos.Y < 0.10 || isKeyDown(KEY_UP)) {
pos.X -= MOVEMENT_SPEED*frameDeltaTime;
target.X -= MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
else if (MousePos.Y >0.90 || isKeyDown(KEY_DOWN)) {
pos.X += MOVEMENT_SPEED*frameDeltaTime;
target.X += MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
break;
case 2:
if ((MousePos.X < 0.1) || isKeyDown(KEY_LEFT)){
pos.X -= MOVEMENT_SPEED*frameDeltaTime;
target.X -= MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
else if (MousePos.X >0.90 || isKeyDown(KEY_RIGHT)){
pos.X += MOVEMENT_SPEED*frameDeltaTime;
target.X += MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
if (MousePos.Y < 0.10 || isKeyDown(KEY_UP)) {
pos.Z += MOVEMENT_SPEED*frameDeltaTime;
target.Z += MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
else if (MousePos.Y >0.90 || isKeyDown(KEY_DOWN)) {
pos.Z -= MOVEMENT_SPEED*frameDeltaTime;
target.Z -= MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
break;
case 3:
if ((MousePos.X < 0.1) || isKeyDown(KEY_LEFT)){
pos.Z += MOVEMENT_SPEED*frameDeltaTime;
target.Z += MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
else if (MousePos.X >0.90 || isKeyDown(KEY_RIGHT)){
pos.Z -= MOVEMENT_SPEED*frameDeltaTime;
target.Z -= MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
if (MousePos.Y < 0.10 || isKeyDown(KEY_UP)) {
pos.X += MOVEMENT_SPEED*frameDeltaTime;
target.X += MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
else if (MousePos.Y >0.90 || isKeyDown(KEY_DOWN)) {
pos.X -= MOVEMENT_SPEED*frameDeltaTime;
target.X -= MOVEMENT_SPEED*frameDeltaTime;
//setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
//setTarget(core::vector3df(target.X,target.Y,target.Z));
}
break;
}
//Zoom
if(currentZoom != oldZoom
&& ((pos.Y + frameDeltaTime* (currentZoom - oldZoom)* 0.1) < 1000)
&& ((pos.Y + frameDeltaTime* (currentZoom - oldZoom)* 0.1) >200) ){
pos.Y += frameDeltaTime* (currentZoom - oldZoom)* 0.1;
oldZoom = currentZoom;
}
if( isKeyDown(KEY_ADD)&& pos.Y> 200){
pos.Y -= ZOOMING_SPEED*frameDeltaTime;
}
if( isKeyDown(KEY_SUBTRACT) && pos.Y < 1000){
pos.Y += ZOOMING_SPEED*frameDeltaTime;
}
setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
setTarget(core::vector3df(target.X,target.Y,target.Z));
updateAnimationState();
}
void RTSCamera::ChangeView(){
core::vector3df pos = getPosition();
core::vector3df target = getTarget();
klick+=1;
if (klick > 3){
klick = 0;
}
if (klick == 0){
pos.Z = 900;
pos.X =0;
}
else if(klick ==1) {
pos.Z = 0;
pos.X = 900;
}
else if(klick==2) {
pos.Z= -900;
pos.X= 0;
}
else if(klick==3) {
pos.Z= 0;
pos.X= -900;
}
setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
setTarget(core::vector3df(0,0,0));
}
void RTSCamera::checkBound()
{
core::vector3df pos = getPosition();
core::vector3df target = getTarget();
Bound = false;
if(pos.X >=10000){
pos.X -= 5;
target.X-=5;
MousePos.X = 0.85;
Bound = true;
}
if(pos.X <= -10000){
pos.X +=5;
target.X +=5;
MousePos.X = 0.15;
Bound = true;
}
if(pos.Z >= 10000){
pos.Z -=5;
target.Z -=5;
MousePos.Y = 0.85;
Bound = true;
}
if(pos.Z <=-10000){
pos.Z +=5;
target.Z +=5;
MousePos.Y = 0.15;
Bound = true;
}
if(!Bound){
if((MousePos.X > 0.9) || ( MousePos.X < 0.1) || (MousePos.Y > 0.9) || (MousePos.Y < 0.1)){
updateAnimationState();
setPosition(core::vector3df(pos.X,pos.Y,pos.Z));
setTarget(core::vector3df(target.X,target.Y,target.Z));
Movement();
}
}
}
void RTSCamera::OnRegisterSceneNode()
{
video::IVideoDriver* driver = SceneManager->getVideoDriver();
if (!driver)
return;
if (SceneManager->getActiveCamera() == this)
{
screenDim.Width = (f32)driver->getCurrentRenderTargetSize().Width;
screenDim.Height = (f32)driver->getCurrentRenderTargetSize().Height;
driver->setTransform(video::ETS_PROJECTION,Projection);
//If UpVector and Vector to Target are the same, we have a problem.
//Correct it.
core::vector3df pos = getAbsolutePosition();
core::vector3df tgtv = Target - pos;
tgtv.normalize();
core::vector3df up = UpVector;
up.normalize();
f32 dp = tgtv.dotProduct(up);
if ((dp > -1.0001f && dp < -0.9999f) || (dp < 1.0001f && dp > 0.9999f))
up.X += 1.0f;
View.buildCameraLookAtMatrixLH(pos,Target,up);
recalculateViewArea();
SceneManager->registerNodeForRendering(this,scene::ESNRP_CAMERA);
}
if (IsVisible)
scene::ISceneNode::OnRegisterSceneNode();
}
void RTSCamera::render()
{
video::IVideoDriver* driver = SceneManager->getVideoDriver();
if (!driver)
return;
driver->setTransform(video::ETS_VIEW,View);
}
void RTSCamera::OnPostRender(u32 timeMs)
{
// animate();
ISceneNode::setPosition(Pos);
updateAbsolutePosition();
//TODO Add Animators
}
void RTSCamera::setInputReceiverEnabled(bool enabled)
{
InputReceiverEnabled = enabled;
}
bool RTSCamera::isInputReceiverEnabled() const
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return InputReceiverEnabled;
}
const core::aabbox3d<f32>& RTSCamera::getBoundingBox() const
{
return BBox;
}
const core::matrix4& RTSCamera::getProjectionMatrix() const
{
return Projection;
}
const scene::SViewFrustum* RTSCamera::getViewFrustum() const
{
return &ViewArea;
}
const core::vector3df& RTSCamera::getTarget() const
{
return Target;
}
const core::matrix4& RTSCamera::getViewMatrix() const
{
return View;
}
const core::vector3df& RTSCamera::getUpVector() const
{
return UpVector;
}
f32 RTSCamera::getNearValue() const
{
return ZNear;
}
f32 RTSCamera::getFarValue() const
{
return ZFar;
}
f32 RTSCamera::getAspectRatio() const
{
return Aspect;
}
f32 RTSCamera::getFOV() const
{
return Fovy;
}
void RTSCamera::setRotation(const core::vector3df& rotation)
{
if(TargetAndRotationAreBound)
Target = getAbsolutePosition() + rotation.rotationToDirection();
ISceneNode::setRotation(rotation);
}
void RTSCamera::setProjectionMatrix(const irr::core::matrix4& projection, bool isOrthogonal)
{
IsOrthogonal = isOrthogonal;
ViewArea.Matrices [ video::ETS_PROJECTION ] = projection;
ViewArea.setTransformState ( video::ETS_PROJECTION );
}
void RTSCamera::bindTargetAndRotation(bool bound)
{
TargetAndRotationAreBound = bound;
}
bool RTSCamera::getTargetAndRotationBinding(void) const
{
return TargetAndRotationAreBound;
}
void RTSCamera::setNearValue(f32 f)
{
ZNear = f;
recalculateProjectionMatrix();
}
void RTSCamera::setFarValue(f32 f)
{
ZFar = f;
recalculateProjectionMatrix();
}
void RTSCamera::setAspectRatio(f32 f)
{
Aspect = f;
recalculateProjectionMatrix();
}
void RTSCamera::setFOV(f32 f)
{
Fovy = f;
recalculateProjectionMatrix();
}
void RTSCamera::setUpVector(const core::vector3df& pos)
{
UpVector = pos;
}
void RTSCamera::setProjectionMatrix(const core::matrix4& projection)
{
Projection = projection;
}
void RTSCamera::setPosition(const core::vector3df& pos)
{
Pos = pos;
updateAnimationState();
ISceneNode::setPosition(pos);
}
void RTSCamera::setTarget(const core::vector3df& pos)
{
Target = oldTarget = pos;
updateAnimationState();
}
void RTSCamera::pointCameraAtNode(ISceneNode* selectednode)
{
core::vector3df totarget = getPosition() - getTarget();
setPosition(selectednode->getPosition() + (totarget.normalize() * 300));
setTarget(selectednode->getPosition());
updateAnimationState();
}
void RTSCamera::setMinZoom(f32 amount)
{
targetMinDistance = amount;
}
void RTSCamera::setMaxZoom(f32 amount)
{
targetMaxDistance = amount;
}
void RTSCamera::recalculateProjectionMatrix()
{
Projection.buildProjectionMatrixPerspectiveFovLH(Fovy,Aspect,ZNear,ZFar);
}
void RTSCamera::recalculateViewArea()
{
core::matrix4 mat = Projection * View;
ViewArea = scene::SViewFrustum(mat);
ViewArea.cameraPosition = getAbsolutePosition();
ViewArea.recalculateBoundingBox();
}
void RTSCamera::allKeysUp()
{
for(int i = 0;i < KEY_KEY_CODES_COUNT;i++)
Keys[i] = false;
}
void RTSCamera::allMouseKeysUp()
{
for (s32 i=0; i<3; ++i)
MouseKeys[i] = false;
}
bool RTSCamera::isKeyDown(s32 key)
{
return Keys[key];
}
bool RTSCamera::isMouseKeyDown(s32 key)
{
return MouseKeys[key];
}
void RTSCamera::updateAnimationState()
{
core::vector3df pos(Pos - Target);
// X rotation
core::vector2df vec2d(pos.X,pos.Z);
rotX = (f32)vec2d.getAngle();
// Y rotation
pos.rotateXZBy(rotX,core::vector3df());
vec2d.set(pos.X, pos.Y);
rotY = -(f32)vec2d.getAngle();
// Zoom
//currentZoom = (f32)Pos.getDistanceFrom(Target);
}
Vielen dank Schonmal für die Hilfe,
JFT