A Little Trigonometry

Started by AGP, May 30, 2009, 06:32:34 PM

Previous topic - Next topic

AGP

Thanks, man, and I wish I could take credit for the model, but I bought it. :-) I'm preparing a website with a bunch of things I did over the years (since I didn't finish a lot there's a lot to do), but I'll be posting the links here as I finish them.

paulscode

Ok, I made a test applet.  I found one problem with the formula I wrote - namely that it only produces a positive angle, so you can't simply use rotateZ() or the car will only rotate counterclockwise.  I fixed that problem by defining the rotationAxis as I mentioned in an earlier post:
                SimpleVector rotationAxis = a.calcCross( b );
                car.rotateAxis( rotationAxis, theta );


This greatly improves the movement, but there is still a problem with some of the rotations being not quite right.  I'll work on this some more to see if I can solve the problem.

Here is the test applet (let me know if this is the same behavior you are seeing in your program):

Car Navigation Applet  (Source)

AGP

I applied your change and the result is conservative (it does most of the rotation, maybe 70% of it, but never quite enough). Also, since my streets always turn left, I didn't get to test a right turn, although I supposed your applet does. Here's a quick question: why doesn't the following code produce an even speed?


float translationAmount = 72f/distance; //MOVE % OF distance THAT EQUALS 72
SimpleVector directionMoved;
directionMoved = moveTowardsMinusZ(opponentCar, rails[currentDirectionMarker], translationAmount);
//THE DISTANCE IS UPDATED WHENEVER calculateDistance(opponentCar.getTransformedCenter(), rails[currentDirectionMarker]) < 1f AND currentDirectionMarker IS INCREMENTED
//THEN THE MOVETOWARDS METHOD CALCULATES THE DELTAS:
float deltaX = (direction.x -origin.x)*translationAmount;
float deltaY = (direction.y -origin.y)*translationAmount;

EgonOlsen

Quote from: AGP on June 04, 2009, 03:07:21 AM
Here's a quick question: why doesn't the following code produce an even speed?
Maybe because of an uneven frame rate? Just a wild guess...

AGP

It's not supposed to be uneven, it sleeps 40 milliseconds, and draws. Obviously some parts of the model might take a little longer to render, but it should hardly be perceptible, and it really really is.

Egon, on yet a separate question (not to draw too much attention away from the bigger turning problem), how do I draw an image onto the buffer using the hardware renderer outside of an AWTGLCanvas? I've tried reading the lwjgl docs, but I found nothing there. I even tried looking for my C OpenGL programs of so many years ago, but I can't find them right now. Do you know?

EgonOlsen

You could load the image into a texture and blit it. Or does it change constantly?

Edit: About the turning problem: Wasn't my approach sufficient?

EgonOlsen

Quote from: AGP on June 04, 2009, 08:06:13 AM
It's not supposed to be uneven, it sleeps 40 milliseconds, and draws. Obviously some parts of the model might take a little longer to render, but it should hardly be perceptible, and it really really is.
Have you printed out deltaX and deltaY to see how they actually change over time?

AGP

Thanks for the blitting suggestion, I'm about to try it out. But yes, it's really an animated gif I want drawn (but at this point I'll settle for a static one).

And no, your approach constantly rotates the car in odd angles. The closest one so far has been paulscode's latest, but even that doesn't quite do it.

And I solved the speed issue (I was working from a constantly-updated carCenter). Sorry if I waste your time sometimes, but I really try not to. This particular question took me a couple of days to ask. I think something happens with me after I ask a question (unburden myself, I suppose!) that I think more clearly. So, down to two problems. :-)

EgonOlsen

Well, if all you want to to rotate something to match another vectors direction, maybe getRotationMatrix() in SimpleVector helps to solve this? I usually prefer to work with matrices instead of euler angles...causes less trouble in the end.

AGP

If you mean something like the following, it didn't do it for me either.

opponentCar.setRotationMatrix(rails[currentDirectionMarker].getRotationMatrix());

EgonOlsen

But it should...are you sure that the problem isn't somewhere else? Maybe in the direction markers themselves?

paulscode

A note on constant speed (regardless of framerate).  I usually define a "distancePerMilli" float and a "lastTick" long:

    private float distPerMilli = 0.018f;
    private long lastTick;


When initializing, I give "lastTick" its initial value:
        lastTick = System.currentTimeMillis();

Then when moving the object, I determine how long it's been since the last move, and calculate how far to move the object:
        long currentTime = System.currentTimeMillis();
        long millisPast = currentTime - lastTick;
        lastTick = currentTime;

        float distance = millisPast * distPerMilli;


Then given a direction, I normalize, scalarMul by the distance, and apply the translation:
SimpleVector trans = new SimpleVector( direction ).normalize();
trans.scalarMul( distance );
myObject.translate( trans );


I also use this type of setup for rotations, special effects, and so on.  Higher framerates, of course, have smoother movement, but things will move the same speed for both high and low framerates.

AGP

Egon, what kind of a problem could the direction markers have? I store the center of my car and the opponent car follows the markers perfectly.

And paulscode, thanks for the tip, but on a PC (where performance differences won't be that significant) isn't that a little bit overkill?

EgonOlsen

You should always time your game logic based on ticks or real ms (whatever you prefer, i prefer ticks as they are easier to handle IMHO). Imagine the game running on a slow PC or with AA enabled or swapping causes a slow down or...

Could you provide the marker data in form of a simple ASCII-file or something? I would like to try this myself to see what's actually going on. Getting the rotation matrix out of the direction vector and apply it has to work IMHO... ???

AGP

I e-mailed it to you. It's not an ASCII, but all you need is DataInputReader to read the floats.