View Full Version : Things are moving in hyperspeed!
07-07-2005, 07:09 AM
I am pretty new to game programming, so I'm stumbling accross problems that I've never encountered before. I'm making an Asteroids clone using the PTK engine for practice and I can't get things to move slowly. The code I'm using for movement essentially boils down to this:
accelx = (1)*( (cos(getAngle()) )*1); //get direction
accely = (1)*( (sin(getAngle()) )*1);
vx = getSpeedx() + accelx; //velocity
vy = getSpeedy() + accely;
posx = getPosx() +vx;
posy = getPosy() +vy;
speed is initially set to 0.
I've put a limit on the top speed, but since the acceleration is so fast, the control is really bad. The ones in the accel part of the code were to be modifiers for accelleration, but I've set them all to 1 for the smallest increase since it goes so fast.
At one touch of the keyboard the ship leaps accross the screen. i've limited the Frame rate to 60FPS. I don't know what else to do. I'm sure I'm missing some obvious thing that all game programmers do.
07-07-2005, 07:26 AM
Well I can't see any problem with that code, but its hard to know what is fast and what is slow in your game world without knowing its relative size, and how big a unit is. Instead of multiplying accel by 1, perhaps try 0.1, or 0.01 or some other decimal numbers.
Unless your using very small units, accelerating 60 units per second does seem to be very high. If you are using pixels as your unit then this is almost certainly the reason why.
07-07-2005, 07:38 AM
I have no idea. I'm very new to graphics, and moving things around on the screen. What do you use? A low frame rate, decimal units, some other method?
07-07-2005, 07:44 AM
Don't lower your frame rate, it will make your game look worse. I would just try keep lowering all your speed and accel values until it works correctly, which will most likely be numbers below 1.
07-07-2005, 07:49 AM
yeah lowering the frame rate doesn't change anything, just makes it choppy. I tried lowering the number 1 in
accelx = scalex *1;
to 0.01, but it just made it choppy and buggy and the controls unresponsive. Touching the keyboard wouldn't make it move, then it would move after a second or two.
edit: wait I may have a reason for that...
edit2: Ok when I lower the values they either have no effect, or I lower them alot, like, 0.005 and then the control is just completely buggy. I can't get that slow, slow acceleration like in Asteroids. It's always instant.
07-07-2005, 08:31 AM
Hmm I suspect there is more to it if this is the case. If nothing happens for a few seonds and then its moves jumpy then it would suggest your framrate is incredibly low. But without seeing the entire program its hard to tell exactly the problem.
07-07-2005, 02:44 PM
One immediate thing that jumps out at me is that you are not factoring time into your physics calculations at all. As a result, your physics simulation here is going to be framerate dependent. You will want to incorporate a "dt" where dt=time since last frame.
You should try something like this:
dt = time since last frame (at fps = 60 this would be 1/60 = 0.01667 but better to calculate dynamically in case frame rate drops)
accelrate = 100; // you can tweak this; this is in units per second.
accelx = (cos(getAngle()) * accelRate); //get direction
accely = (sin(getAngle()) * accelRate);
vx = getSpeedx() + (accelx * dt); // v = a(t)
vy = getSpeedy() + (accely * dt);
posx = getPosx() + (vx * dt); // this needs to also be multiplied by dt
posy = getPosy() + (vy * dt); // x = v(t)
07-18-2005, 08:20 AM
Ok I tried out the suggestions and they didn't work. So I started sort of from scratch and slowly built up everything piece by piece using the suggestion of incorporating a "dt." Doing this I was able to get constant velocity movement to work. Then I added the angle movement so that I can do things such as, rotate the ship or pick a random direction for rocks to start moving in, and the whole thing fell apart. It looks like the reason for buggy movement is based on the trig angle stuff.
Right now I'm testing with a linked list of rocks, and I go through the whole list and for each one, I run this code to update it's position and move it. I'm using stl for the trig, and piConvert converts the angle to radians. With the two trig lines commented out, this code does the movement perfectly. But when they are in the code, then it is extremely buggy. Some rocks don't even move at all, and when they do, they stop at the left and top walls (position x=0,position y=0) instead of looping to the other side like they do when the trig code is commented out.
vx = rockiter->getSpeedx();
vy = rockiter->getSpeedy();
//vx = (cos(rockiter->getAngle()*piConvert)) * vx; //get x direction
//vy = -(sin(rockiter->getAngle()*piConvert)) * vy; //get y direction
posx = rockiter->getPosx() + (vx*dt ); // this needs to also be multiplied by dt
posy = rockiter->getPosy() + (vy*dt ); // x = v(t)
Is there maybe a better way to impliment the angles necessary for an Asteroid clone? This doesn't seem to be working and I have absolutely no idea why.
07-18-2005, 02:56 PM
Here, do it like this. Give each asteroid a velocity_x and velocity_y variable, and make them floats. Make the x and y floats, too.
Set velocity_x and velocity_y when the asteroid is initiated. Get the values from sin and cos of the angle. I would expect these values to probably be somewhere <1.
Every game loop, x+=velocity_x and y+=velocity_y. That's all. Nice and easy.
To make the rocks loop around the screen:
if x<screen_left and velocity_x<0 then x=screen_right
if x>screen_right and velocity_x>0 then x=screen_left
if y<screen_up and velocity_y<0 then y=screen_down
if y>screen_down and velocity_y>0 then y=screen_up
07-18-2005, 05:27 PM
I did some more testing and I found out that everything gets buggy if speed*dt <1.
posx = posx + (speed*dt);
since my dt is 0.001, my speed must be 1000, but while that makes everything go fine, it's really fast. Why could this be occuring? How can I get things to go slower?
The angle making things mess up could be related to this, as it might make (speed*dt) <1.
07-18-2005, 08:37 PM
Sounds like you're using ints instead of floats.
07-19-2005, 10:37 AM
I casted the floats to ints later in the program and that fixed the problem. I thought I had already checked this possiblility, but I guess I was wrong. Thank you everyone for your help.
Powered by vBulletin™ Version 4.1.3 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.