Page 1 of 1

Simple Bouncing Ball Around Screen Help

Posted: Wed Jun 16, 2004 3:45 pm
by surfordie
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;
}

Posted: Wed Jun 16, 2004 11:57 pm
by dingo
If you are detecting collision with the edge of the screen then you will need to unproject the ball each frame to get the ball's screen coordinates because in 3D the coordiantes do not relate to the screen pixels (like 2D) but relate to an imaginary 3D world with x, y, z axes.

Thus you will need to "unproject" from the 3D world back to the screen to see which pixel is being used to draw the model.

Best solution is to forget about the screen coords form collision detection, and instead use a physics engine (see the tuts on tokamak, newton, ODE on this site) and just have a box within which the ball bounces

Posted: Sat Jun 26, 2004 6:29 am
by thesmileman
Sorry I don't have time to look but you should search for a couple of posts by myself back a few months. I was working on bouncing a ball when it hit a wall. I would explain how to do it but it was all explained there pretty well.