Game Development Community

Getting the X and Y coords from an angle?

by David Taylor · in Torque Game Builder · 11/01/2006 (10:28 pm) · 14 replies

I've got a player who is a 2D object, and a 3dshape. I want the player's angle to affect the rotation of the 3dShape. The problem is that getRotation() for the player only returns a single variable, representing the angle as a whole. And setAutoShapeRotation() is split up into X, Y, Z - three variables.

How can I get the X and Y values out of the getRotation() so that I can then use them to rotate the 3dShape?

#1
11/01/2006 (11:07 pm)
If I understand you correctly, you're asking for the x and y components of a 2D angle? If so,
%x = mCos(%player.getRotation());
%y = mSin(%player.getRotation());

But then again, from looking at your post... Perhaps you are just looking to rotate a 3D shape to match a 2D one? In that case, you just rotate the shape along the axis going through the screen (which is probably Z but I haven't played with Shapes yet) and leave the X and Y blank. Have you tried something like that? It wouldn't really make sense to have a rotation based off of a component (that's usually movement...).

Hope that helped at all ;)
#2
11/02/2006 (7:32 am)
Tom, do our trig functions take degrees? I know the inverse functions return radians so I suspect those functions take radians. In which case there should be an mDegtoRad or something in those.
#3
11/02/2006 (9:31 am)
Yeah, I thought about that but couldn't remember and it was late... ;)

I'm not sure either way - and I don't think that David is going to use it anyway.

--Aight, just tested it and mCos and mSin take radians. Silly me ;)
#4
11/02/2006 (5:24 pm)
Thanks guys. So we're looking at the following, then?

%x = mCos(mDegtoRad(%player.getRotation()));
%y = mSin(mDegtoRad(%player.getRotation()));
#5
11/02/2006 (5:36 pm)
I wish there was an easy way to draw on forums :/

Anyway, I don't think this line makes any sense:
Quote:How can I get the X and Y values out of the getRotation() so that I can then use them to rotate the 3dShape?

The X and Y components (gotten with the code above) of the angle are distances. (The length of the legs of the right triangle with the angle you're using) I don't see how that's useful - more likely you just want to use the player's rotation itself to rotate the 3D shape...

But, I don't know what you're doing, so try it out until it works ;)
#6
11/02/2006 (8:38 pm)
Yeah, you're right on both counts - images would help, and my sentence didn't make much sense, lol.

Okay, here's what I want to do. Does anyone remember playing GTA and GTA 2? The game play was 2D, but the graphics for buildings, etc, were 3D. I want the same sort of thing. So if my player is walking down a street towards a building, that building starts to 'lean' towards the player. The closer the player is to the building, the more of a topdown view the player gets of it...from further away, the player can see part of the side of the building. Does that make more sense?

So which angle the building leans towards at any given moment is defined by what angle the player is headed in at that same moment. My building will lean in accordance to the angle of the player, if that makes sense.

So if I can get the x and y coords from my player's rotation, and somehow apply them to the 3d building to get it to lean in the way that makes it look most normal, that would be just dandy. =P
#7
11/02/2006 (8:51 pm)
Perspective? Rotation isn't going to give you that illusion, not by a long shot. You'd be much better off with a 3D engine for that sort of thing (which is what the GTA games used).
#8
11/02/2006 (9:07 pm)
Well, first, the effect in the first two GTA games wasn't done with 3D - it was just an illusion as far as I know...
In TGB, you could just use 3D shapes for buildings or simply fake it with sprites. That might be more complicated than it should be, unfortunately, because TGB uses rectangular sprites, but could probably still be done convincingly.

Either way, though, the angle of the player is irrelevant to the effect. Only position matters for the perspective calculation here.

Hope that helps.
#9
11/02/2006 (10:03 pm)
Oh my gosh...you're right...position and distance between the player and the building. Lol, I can't believe I had that so back-to-front!

I want to keep using the 3D models for buildings. It just makes it a lot easier...for me, anyway.

So what calculation am I looking at now?

Player's position in relation to building...(hang on, is this not still angle related?)... * distance between player and building = amount of lean and direction? I'm not too sure how to implement that properly.
#10
11/02/2006 (10:55 pm)
You shouldn't have to lean them at all...unless the TGB 3D system is crazy orthogonal, which wouldn't make any sense at all (watch as I'm proved wrong ;) - so, you should just put the buildings in there and move them around and the perspective should be automatic...all you'd have to do it adjust the FOV (if you even can in TGB through script) if needed. Have you tried just sticking a box in TGB and moving it around the scene?
#11
11/02/2006 (11:08 pm)
Oh my gosh...you're right...position and distance between the player and the building. Lol, I can't believe I had that so back-to-front!

I want to keep using the 3D models for buildings. It just makes it a lot easier...for me, anyway.

So what calculation am I looking at now?

Player's position in relation to building...(hang on, is this not still angle related?)... * distance between player and building = amount of lean and direction? I'm not too sure how to implement that properly.
#12
11/02/2006 (11:47 pm)
I have no idea why I re-posted my previous post... :/

Yes, 'lean' is a poor choice of words. It's rotating the 3D model with setAutoRotation().

If I use 2D sprites, I can only use the tops of buildings, though, correct? You won't see the sides of the building, such as the front, back, etc. And if you made a 2D sprite with the sides included, it would look odd, especially when the player is right by the building and all they should see is the roof. That's why I want to use a 3D model.

So what I want to work out is the distance between two points, (player and building), and the angle between them. The distance will tell me how much to rotate the 3D model, and the angle will tell me which direction to rotate it in. Unless I can just tell it to rotate away from the player's position somehow?
#13
11/04/2006 (7:44 pm)
I found this link: http://www.joelesko.com/games/gta/screen_main.png

Which is a great example of what I want to be able to do. You see how the player can see both the front and top of the buildings? And then, to have the player be to the left of all the buildings, and being able to see the left side of the building and the top, etc. That's what I want to do with the 3d model.

I hope that helps to clarify what I'm aiming for.
#14
11/06/2006 (10:07 pm)
Thanks, guys.

I kept fiddling around, and was embarrassed by how simple it was!

function SurfaceObject::RotateObjectAroundPlayer(%playerPositionX, %playerPositionY)
{
    for(%i = 0; %i <= $SurfaceObjectQuantity + 1; %i++)
    {
        if($SurfaceObject[%i].getPositionY() == %playerPositionX)
        {
            $SurfaceObject[%i].yRotation = 180;
        }
        else
        {
            $SurfaceObject[%i].yRotation = (%playerPositionX - $SurfaceObject[%i].getPositionX()) + 180;
        }

        if($SurfaceObject[%i].getPositionY() == %playerPositionY)
        {
            $SurfaceObject[%i].xRotation = -90;
        }
        else
        {
            $SurfaceObject[%i].xRotation = (%playerPositionY - $SurfaceObject[%i].getPositionY()) - 90;
        }

        $SurfaceObject[%i].setShapeRotation( $SurfaceObject[%i].xRotation, $SurfaceObject[%i].yRotation, 0 );
    }
}

(The -90 and +180 are because the default 3d model I have been using requires to be turned that way to look topdown, but a more suitably angled 3d model wouldn't require any modifications there.)