Simple Bouncing Ball Around Screen Help
Posted: Wed Jun 16, 2004 3:45 pm
I'm new to irrlicht, but learning. Could someone show me code to make a ball bounce around the screen? I know this can easily be done in 2D, but I want to try making this in 3D to take advantage of particle systems, rotation, and other 3d effects. I'm sure this can be done somehow and that 3D is not just for making Quake games.
I wrote my own which loads the ball from an ms3d file, applies a texture, but the ball just floats off into oblivion. I thought I was just changing its X and Y values, but maybe the camera affects its position too?
If you could show me a little example of the bouncing ball demo, I would appreciate it thanks.
Here is my code that I wrote, it compiles but if you run it, you'll see my problem of the ball floating into oblivion:
#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;
float BallX=0, BallY=0;
int main()
{
device = createDevice(video::EDT_DIRECTX8, core::dimension2d<s32>(640, 480), 16, false, false, NULL);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("ball.ms3d"));
node->setPosition(core::vector3df(BallX,BallY,10));
node->setMaterialTexture(0, driver->getTexture("fireball.bmp"));
node->setMaterialFlag(video::EMF_LIGHTING, false);
smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
device->getCursorControl()->setVisible(false);
bool bRight = true, bDown = true;
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,90,90,156));
smgr->drawAll();
driver->endScene();
if (bRight)
{
BallX = node->getPosition().X;
BallX+=0.1;
if (BallX > 300)
bRight = false;
}
else
{
BallX = node->getPosition().X;
BallX-=0.1;
if (BallX < 2)
bRight = true;
}
if (bDown)
{
BallY = node->getPosition().Y;
BallY+=0.1;
if (BallY > 300)
bDown = false;
}
else
{
BallY = node->getPosition().Y;
BallY-=0.1;
if (BallY < 2)
bDown = true;
}
node->setPosition(irr::core::vector3df(BallX, BallY, node->getPosition().Z));
node->setRotation(core::vector3df(BallX,BallY,30));
}
device->drop();
return 0;
}
I wrote my own which loads the ball from an ms3d file, applies a texture, but the ball just floats off into oblivion. I thought I was just changing its X and Y values, but maybe the camera affects its position too?
If you could show me a little example of the bouncing ball demo, I would appreciate it thanks.
Here is my code that I wrote, it compiles but if you run it, you'll see my problem of the ball floating into oblivion:
#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>
using namespace irr;
#pragma comment(lib, "Irrlicht.lib")
scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;
float BallX=0, BallY=0;
int main()
{
device = createDevice(video::EDT_DIRECTX8, core::dimension2d<s32>(640, 480), 16, false, false, NULL);
video::IVideoDriver* driver = device->getVideoDriver();
scene::ISceneManager* smgr = device->getSceneManager();
node = smgr->addAnimatedMeshSceneNode(smgr->getMesh("ball.ms3d"));
node->setPosition(core::vector3df(BallX,BallY,10));
node->setMaterialTexture(0, driver->getTexture("fireball.bmp"));
node->setMaterialFlag(video::EMF_LIGHTING, false);
smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
device->getCursorControl()->setVisible(false);
bool bRight = true, bDown = true;
while(device->run())
{
driver->beginScene(true, true, video::SColor(255,90,90,156));
smgr->drawAll();
driver->endScene();
if (bRight)
{
BallX = node->getPosition().X;
BallX+=0.1;
if (BallX > 300)
bRight = false;
}
else
{
BallX = node->getPosition().X;
BallX-=0.1;
if (BallX < 2)
bRight = true;
}
if (bDown)
{
BallY = node->getPosition().Y;
BallY+=0.1;
if (BallY > 300)
bDown = false;
}
else
{
BallY = node->getPosition().Y;
BallY-=0.1;
if (BallY < 2)
bDown = true;
}
node->setPosition(irr::core::vector3df(BallX, BallY, node->getPosition().Z));
node->setRotation(core::vector3df(BallX,BallY,30));
}
device->drop();
return 0;
}