Hi,
I’m trying to write a crazy golf game and I’m not sure of the best approach to modelling the motion of the ball.
Has anyone got any suggestions/tips for this type of game ?
Hi,
I’m trying to write a crazy golf game and I’m not sure of the best approach to modelling the motion of the ball.
Has anyone got any suggestions/tips for this type of game ?
So the big question is how your gonna do animation. I am not an experianced J3D programmer so I don’t think I am the best person to answere that question. I will say, that in my current project, I have a control structure that will update anything that needs to be moved around the screen. (or specific control structures for each group of things that need to be moved).
The question is how does that control structure make its decisions. So maybe you should develop a physics class. The class will take current input, given that input, it will develope a path for the ball with specific characteristics, like is the ball bouncing, rolling, sliding, ect. The physics object will continually take input and continually modifies its plan.
I hope this helps - maybe you already concidered this and were looking for more specific help?
Thanks,
It sounds like I’m approaching it in a similer way to what you’re suggesting. The things that I’m not too sure about are :
What is the best way to drive the animation ? My current approach it to use a thread to update the underlying model object (ball motion etc) and then refresh the screen. I call this the pull method.
Another way I could get it to work is to have the model object running as a thread and every time the ball moves a significant amount, get the model object to update the screen. I call this the push method. What other apporoaches are there ?
What is the best way to cope with the ball movement when velocity is very nearly 0 ? I have read a few articles mentioning the need dampening to prevent the ball doing odd things.
Browsing http://www.flipcode.com/links/ I found a link to: http://www.d6.com/users/checker/dynamics.htm where the author has archived 4 articles he has written about rigid body dynamics for Game Developer Magazine. He also has a 2d and 3d demo with source which may prove useful.
Thanks those articles are a great help
I’m trying to get the ball movement (angular/velocity changes upon collision) in a breakout type game working. It would seem that this should be an easy problem not requiring any fancy physics but I can’t seem to get it right. Could someone either post a bit of code from their breakout game or read the following and help me get it right?
Specifically I am having problems with the ball not coming off of a collsion with a block in the correct manner so I end up with weird ball movement. This movement can cause a single horizontal row to be cleared by the ball becuase it never deflects enough to get outside of the row.
public void update(long currentTime) {
super.update(currentTime);
if(!lockedToPaddle)
{
if(posX<20)
{
posX=20;
setXVelocity(-getXVelocity());
}
else if((posX+width)>(m.displayWidth-20))
{
posX=(m.displayWidth-20)-width;
setXVelocity(-getXVelocity());
}
if(posY<=40)
{
posY=40;
setYVelocity(-getYVelocity());
}
else if((posY+height)>m.displayHeight)
{
posY=(m.displayHeight-20)-height;
lockedToPaddle=true;
}
else
{
if(hasCollided(m.player)) // ball hit the paddle
{
setYVelocity(-getYVelocity());
float val = (getX()-m.player.getX()+getWidth())/m.player.getWidth();
if(val<.5f && getXVelocity()<0)
setXVelocity(getXVelocity()-(1f*val));
else if(val<.5f && getXVelocity()>=0)
setXVelocity(-getXVelocity()-(1f*val));
else
setXVelocity(getXVelocity()+(1f*val));
System.out.println(""+val);
}
}
// detect block hits
int pw = m.mapDisplay.cellBlock.cellPixelWidth;
int ph = m.mapDisplay.cellBlock.cellPixelHeight;
int startColumn = (int)((getX()-20)/(float)pw);
int endColumn = (int)((getX()-20+getWidth())/(float)pw);
int startRow = (int)((getY()-40)/(float)ph);
int endRow = (int)((getY()-40+getHeight())/(float)ph);
boolean top = false;
boolean bottom = false;
boolean left = false;
boolean right = false;
for(int row=startRow;row<=endRow;row++)
{
for(int col=startColumn;col<=endColumn;col++)
{
MapCell cell = null;
cell = m.mapDisplay.cellBlock.getCell(col,row,0);
if(cell!=null &&
cell.fringeIcon>=0 &&
!m.mapDisplay.palette.getIcon(cell.fringeIcon).isFlagSet(MapTerrainIcon.FLAG_WALKABLE))
{
cell.fringeIcon = -1;
m.mapDisplay.changed=true;
m.soundHandler.playAudioClip(m.hitSound);
if(getYVelocity()<0 && !top) // moving up
bottom=true;
else if(getYVelocity()>0 && !bottom) // moving down
top=true;
if(getXVelocity()<0 && (!right && !bottom && !top)) // moving left
left=true;
else if(getXVelocity()>0 && (!left && !bottom && !top)) // moving right
right=true;
}
}
}
if(bottom || top) setYVelocity(-getYVelocity());
if(left || right) setXVelocity(-getXVelocity());
}
}
make the ball go up in the air, and then have arrows, make it look like it is flying!!!
Try that allrighty then!!!
I AM REALLY NICE AND FRIENDLY, SO SEND ME A MESSAGE!!!
This reply will be no big help, but … I currently have to model a rolling ball myself. In the end I bought a physics book and looked up all the stuff there (friction, collision etc.). I didn’t understood everything and I didn’t found all in there what I needed but I got me much further than trying to only program by the “looks good, then it is correct” approach.