» «

Post mod/code requests in the Mod Requests topic.

Post mod releases in the Mod Showroom.

Read the Modding Rules BEFORE posting!

GTAGarage.com
Free mod hosting from GTANet, simply login with your GTAForums account details

GTAModding.com
GTANet's modding wiki

GTA Modding Chatroom

# Help with Portal Vector Math

12 replies to this topic
nixolas1
• ##### nixolas1

Creator/Destroyer

• Members
• Joined: 23 Dec 2008
•

### #1 Posted 18 June 2013 - 09:58 PM

Hey guys, I'm making a portal mod for GTA IV/EFLC, and its quite done and working somewhat nicely. But I'm having some problems with the math behind the rotation of the vehicle speed vectors and vehicle rotations/quaternions.

Are there anyone in here with some good math skills?
What i am doing is rotating the speed vector according to the angle between the two portals, both in the 2D axis and the 3D axis.
Here is the code in question, and ill comment along the way. Ask any questions if you dont understand something.

 CODE Vector3 v;       f32 he, rot, rotz,hx,hy,sr,cr,srz,crz,x2,y2,z2;       GetCarSpeedVector(veh, &v, false);             //teleport car to other portal       SetCarCoordinates(veh,p[u],p[u+1],p[u+2]);       //speed vect rot, p[5] is portal1 x rot, p[11] is portal2 xrot, p3=port1 z rot, p9=p2 zrot. PIX=pi/180.//rot = the difference + 180 degrees between the two portals       rot=(abs(p[5]-p[11])+180)*PIX;       rotz=(abs(p[3]-p[9])+180)*PIX;       sr=sin(rot);       cr=cos(rot);       srz=sin(rotz);       crz=cos(rotz);       //z axis rot 2d, works at times, sometimes gives 90 deg wrong, sometimes 180.       x2=(v.X*cr-v.Y*sr);       y2=(v.X*sr+v.Y*cr);       z2=v.Z;       //x axis rot, veh is supposed to fly up if the car drives horizontally into a portal on a wall, and the other portal is on the floor(facing upwards). //it is also supposed to fly horizontally if car falls into portal on floor to portal on wall, in the direction the portal on wall is heading.//right now, some of that works, ish. if it falls on a portal on floor to another on floor, it correctly flies up again, and if the portal is on a wall it(mostly) flies up (correctly).// but vertical to horizontal does not work. it goes horizontally, but not in the direction the portal is headed.       x3=(x2*abs(crz)-z2*srz);       y3=(y2*abs(crz)-z2*srz);       z3=(sqrt(v.X*v.X+v.Y*v.Y)*-abs(srz)+z2*crz);       //car rotation, which has to be partially rewritten, using quaternion to set the proper rotation of the car after it has been through the portal. (Quaternions are hard!)       GetCarHeading(veh, &he);       hx=cos(he*PIX);       hy=sin(he*PIX);       GetHeadingFromVector2D(hx*cr-hy*sr,hx*sr+hy*cr, &hed);       hed+=90;       SetCarHeading(veh, hed);       Wait(1);       ApplyForceToCar(veh, 3, x3, y3, z3, 0, 0, 0, 0, 0, 0, 1);

What am I doing wrong? Do you have any tips on what I can do?

If you are interested and show that you can contribute, I might let you in on the project, and I'll give you credits anyway if you contribute.

Cheers! A nice pic: http://puu.sh/39Lst

Edited by nixolas1, 18 June 2013 - 10:38 PM.

julionib
• ##### julionib

Coder

• Members
• Joined: 13 Sep 2012
•

### #2 Posted 19 June 2013 - 04:57 AM

math kills me always

let me see if i understood, the actual main problem is an vertical portal on wall teleporting to a horizontal portal on ground right? the car enters with 0 pitch and should get out with 90 degrees pitch? in this case, you know what is the direction of each portal? like for example, portal on wall should always pull against street (direction: from wall to street/pavement), portal on ground should pull up (direction: from ground to ground + vector3.worldUp)

this can be a very nice mod

nixolas1
• ##### nixolas1

Creator/Destroyer

• Members
• Joined: 23 Dec 2008
•

### #3 Posted 19 June 2013 - 09:08 AM

Yes, the portal object rotations are correct, and heading the correct way. It's the math that is wrong somehow.

julionib
• ##### julionib

Coder

• Members
• Joined: 13 Sep 2012
•

### #4 Posted 19 June 2013 - 08:18 PM

right, so if you have the direction vector of the portal you can just push car in that direction after teleport, what you need is just change pitch to something like 80 degrees

how are you detecting the wall?

nixolas1
• ##### nixolas1

Creator/Destroyer

• Members
• Joined: 23 Dec 2008
•

### #5 Posted 19 June 2013 - 09:24 PM

the portals are rotated so they align with the walls/floor, just like the sticky bombs. So i have the portal rotation & position. But I can't just set the vehicle speed vector and veh rotation in the direction of the portal, I have to take into account the speed vector and rot the car had before it entered the portal. So what youre saying about just rotating it 80 degrees does not apply here, because the rotation needs to be based on what it was before.

The wall detection and wall angle detection is using GetGroundZAtCoordinates, loops and math. You'll see it when i release the mod, as the source will be available, but its basicly trigonometry and stuff.

What I need help with is the math here, I think im doing the part of getting the rotations right.

julionib
• ##### julionib

Coder

• Members
• Joined: 13 Sep 2012
•

### #6 Posted 20 June 2013 - 05:41 AM

right, i will try some code to test what i sid because its theory hehe

julionib
• ##### julionib

Coder

• Members
• Joined: 13 Sep 2012
•

### #7 Posted 20 June 2013 - 07:15 AM

send me your email and i send to you the code that shows what i was trying to explain

nixolas1
• ##### nixolas1

Creator/Destroyer

• Members
• Joined: 23 Dec 2008
•

### #8 Posted 20 June 2013 - 03:26 PM

I figured out how to do it
By attaching the car to the object so it achieves the same rotation as it, but with the offsets it had when it entered the first portal!

 CODE Vector3 v;       f32 vx,vy,vz,fx,fy,fz,sp=95.0f;       f32 spe;       GetCarSpeed(veh, &spe);       GetCarSpeedVector(veh, &v, true);       GetCarPitch(veh, &vx);       GetCarRoll(veh, &vz);       GetCarHeading(veh, &vy);       //set correct rotation and speed dir of car       fx=(-p[c+3]+vx)*PIX; //pitch       fy=(vy-p[c+5]+180)*PIX; //heading       fz=0; //roll       AttachCarToObject(veh, po[a], 0,0,0,0, fx, fy, fz);       Wait(0);       DetachCar(veh);       SetCarCoordinates(veh,p[u],p[u+1],p[u+2]);       ApplyForceToCar(veh, 3, vx/sp, vy/sp, vz/sp, 0, 0, 0, 0, 1, 1, 1);

julionib
• ##### julionib

Coder

• Members
• Joined: 13 Sep 2012
•

### #9 Posted 20 June 2013 - 06:40 PM

what i did was put car in next portal and use the portal direction to set as new direction, ignoring the entering angle in the first portal

nixolas1
• ##### nixolas1

Creator/Destroyer

• Members
• Joined: 23 Dec 2008
•

### #10 Posted 20 June 2013 - 09:04 PM

Yeah, thats the simple way of doing it, which works, but if you want real portal-physics, you need to take the rot offset between the car and first portal into account.

julionib
• ##### julionib

Coder

• Members
• Joined: 13 Sep 2012
•

### #11 Posted 21 June 2013 - 09:19 PM

right, like entering in 45 degree must exit in 45 degree or something like that?

nixolas1
• ##### nixolas1

Creator/Destroyer

• Members
• Joined: 23 Dec 2008
•

### #12 Posted 25 June 2013 - 01:40 PM

Well, basicly yes. But some modifications/simplifications have to be made because GTA has cars and Portal does not. For example, if you drive a car through a horizontal portal (on the ground), to a portal which is on a wall, you will have too much speed to be able to "fall" DOWN into that portal, and you would (realisticly) drive straight over it. But thats no fun! So I'll have to make some special cases with special rules, so that, in this example, the car will go horizontally into the portal on the ground, and horizontally out of the portal on the wall, even though its not how it is supposed to be.

Quite interesting stuff to be making!
And its coming along quite nicely.

Frank.s
• ##### Frank.s

• Members
• Joined: 15 Apr 2008
•

### #13 Posted 25 June 2013 - 01:50 PM

Just posting to say i'm looking forward to seeing this mod.

#### 1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users