propulsion systems????

Started by
14 comments, last by PyroniaWatches 20 years, 3 months ago
the code was derived from www.GameTutorials.com, one of the tutorials on double buffering. i copied it and have used it as a base ever since. as for your redone code, i tried it and it does the same thing...so i dono what to do now. try it your self, the trythis.h file is as follows:

HBITMAP player_bmp = (HBITMAP)LoadImage(NULL,"player.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);

struct ship
{
public:
int x;
int y;
float y_vel;
float x_vel;
float y_acc;
float x_acc;
float shipAngle;
};


ship player;


void ChangeToFullScreen(int width, int height);
void key();
void game_init();

see what you get. and the bitmap is a red square 2D. i haven''t found an application that''ll let me rotate a bitmap.
Advertisement
It''s not working because of a small bug.

In your code you say:

if(KEYDOWN(VK_UP))
{
float radianAngle;

radianAngle = player.shipAngle * (180/3.14159);

player.x_acc = cos(radianAngle);
player.y_acc = sin(radianAngle);

player.x_vel += player.x_acc;
player.y_vel += player.y_acc;
}

if(player.x_vel > 0) player.x_vel -= 5;
if(player.x_vel < 0) player.x_vel += 5;


This is where the problem lies.

The reason your ship is moving around all crazy like is because your call to sin ( randianAngle) and cos ( radianAngle) will return a number between -1 and 1 (I think). Either way, it will never be 5!

You add this number to your velocity, and then subtract 5. So:

xAccel = 0.2
yAccle = -0.9

xVelocty = 0.2 - 5 = -4,8.

Here is how you fix the problem:

You have to multiply cos(radianAngle) by something, like an acceleration constant. (See my original post).

ie:

player.x_acc = THRUST_CONST * cos ( radianAngle);

In this case THRUST_CONST _must_ be grater than 5 (otherwise your ship will move backwards).

Let me know if this fixed your problem.

Best of luck,
Will






------------------http://www.nentari.com
well...i tried it, but now it moves according to the mouse, i took that out and it screws up...no matter what it wont work. i did the following:

#define THRUST_CONSTANT 6

then when cos and/or sin i did this:

player.x_acc = THRUST_CONSTANT * cos(radianAngle);

.....IT STILL SCREWED up. try it your self and see but im tellin you it just wont work so....its evil
hmm... this might not fix it, but isn''t this wrong?

radianAngle = player.shipAngle * (180/3.14159);

should be

radianAngle = player.shipAngle * (3.14159/180);

also, since C++ uses rounding that should be

radianAngle = player.shipAngle * (3.14159f/180.0f);


for example: player.shipAngle = 180
radianAngle = 180 * (3.14159 / 180) = 3.14159
kVandaele: I think you''re right.

Try doing what kVandaele says, PyroniaWatches.

You should really learn how to use your debugger. Point your ship Right (0 degrees and 0 radians) and see what the thrust is in the x direction. Try it again with the ship facing north, west, south, etc.

At 0 degrees you should get MAX_THUST in the x Direction, and 0 in the Y. If the ships facing west you should get -MAX_THRUST in the x direction, and 0 in the Y.

The code isn''t evil, but I wrote it off the top of my head. I didn''t test it. You''ll figure it out-- just keep at it.

Best of luck,
Will
------------------http://www.nentari.com
actually your code''s quite good... I might even use it (well, modify it... Personally i believe having one velocity with an angle is more intuitive than two velocities... so if you turn 180 degrees and accelerate you''re going faster not slower -- but of course, that''s not how the site did it ). Thanks for the sine and cosine bit, i would''ve cracked my head on that in about a month

This topic is closed to new replies.

Advertisement