1. Senior Member Join Date
Apr 2012
Posts
115
Rep Power
0

## Game programming : please help me understand this example from book

Ok so I bought a book that takes me through creating the astroids game. They gave me code in chapter 3 but I dont quite understand how velocity works... perhaps someone could help explain this to me. Ok so we create the astriods

Java Code:
```for (int n = 0; n<ASTEROIDS; n++) {
ast[n] = new Asteroid();
ast[n].setRotationVelocity(rand.nextInt(3)+1);
ast[n].setX((double)rand.nextInt(600)+20);
ast[n].setY((double)rand.nextInt(440)+20);
ast[n].setMoveAngle(rand.nextInt(360));
double ang = ast[n].getMoveAngle() - 90;
ast[n].setVelX(calcAngleMoveX(ang));
ast[n].setVelY(calcAngleMoveY(ang));
}```
we set the move angle (lets say its heading north, 0)
now we calculate "ang" which is move angle - 90 (west, 270)
we then use ang to calculate the angleMove

public double calcAngleMoveX(double angle)
{
return (double) (Math.cos(angle * Math.PI / 180));
}

what is it that this method is returning? (the book just uses terms like update x & y value), is this method returning the updated angle of movement? how do you work that out from current angle - 90?

now we use this value to calculate velocity (setVel above) - what is velocity, the speed of the object? how do we work that out from the new move angle?

also, because setvelx and setvely use the same base "calcAngleMoveX(ang)" wont x and y be the same?

I dont understand any of this stuff... could one you smart guys explain it all in simple terms so I can visualise how the objects are able to move around. points for whoever explains it the clearest  Reply With Quote

2. ## Re: Game programming : please help me understand this example from book

From what I remember about Asteroids on Atari, velocity might be a fixed value, maybe a static field. From this incomplete code, it looks like each Asteroid has an angle field (ang), and methods like calcAngleMoveX(...) use basic trigonometry to convert the polar vector (angle and velocity) into X and Y rates. The setVelX(...) method probably takes the result of that calculation and stores it somewhere.

Those are kind of awkward method names, but there's one good thing you should notice about them: a method whose name begins with "calc" should, as its name suggests, simply calculate something without changing any class fields, and calcAngleMoveX(...) follows that convention. But when a method name begins with "set", you can assume it changes the value of a field. Preferably a field whose name matches the method name, so a method called setVelX(...) should set a field called velX. You can't really count on everyone naming things that way, but it's a good habit to get into.
Last edited by kjkrum; 07-14-2012 at 12:00 PM.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•