Jump to content
A 2021 backup has been restored. Forums are closed and work in progress. Join our Discord server for more updates! ×
SoaH City Message Board

Vague Trig Function question topic time! [General C Programming]


Recommended Posts

Okay, two major questions I have here. Both of which are very vague and theoretical sounding, but please bear with me.

1.

I know the location of an object, I know it's rotation along a vertical axis, I know it's standing on a triangle, and I know the three points of that triangle in a three dimensional existence. I know from looking at most existing 3D fangames that this information can be used to determine the object's rotation along a horizontal axis, but I don't know how. If at all possible, I'd like reference to some math.h functions, a formula, etc. But any help here is appreciated.

2.

Once I have this information, I'll admit that screwing around I've still yet to figure out just how to adapt the old 2D vector turning around algorithm thingy to work with two axis.

To put it in a more understandable way, I know the player's current speed, and I know his rotation along two axis, how do I use this to determine his new location?

Any help in either question is appreciated, thanks in advance. :3

Link to comment
Share on other sites

What do you mean vertical and horizontal axises in a 3D space? Do you mean you have a vertex on the XY plane (assuming you're oriented such that Z is up/down) indicating its speed on a flat ground and you want to convert it to a vertex on the angled plane of the triangle?

If that's the case, then you can use any number of formulas to fit the z position of the vertex to the triangle. One relatively-easy way to do this is to find the normal of the triangle and set the points projection on the normal to 0.

// Determine the normal of a triangle where (x#, y#, z#) are the points
  i = (((y2 - y1)*(z3 - z1)) - ((y3 - y1)*(z2 - z1)));
  j = (((z2 - z1)*(x3 - x1)) - ((z3 - z1)*(x2 - x1)));
  k = (((x2 - x1)*(y3 - y1)) - ((x3 - x1)*(y2 - y1)));

// Determine the z value of an x, y value such that it's 
// flattened to the plane determined from the triangle
  zp = ((x1 - xp)*i + (y1 - yp)*j)/k + z1;

(note: when k = 0, that means you have a plane that's standing straight up like a wall. There's no real way to flatten the z value in this case, but you still want to do something to avoid dividing by 0)

As for what you should use as xp, yp, you should use it as the vertex of the speed (easy to determine from magnitude and direction) plus the position of the player <xp, yp> = <sx + x, sy + y>. Then after you find zp, you should subtract the position of the player from all three components to get a speed vector <sx, sy, sz> = <xp - x, yp - y, zp - z> which you simply add to the player to get its new position.

If you want to determine his rotation from this vertex (so that you can rotate the player model appropriately), it's just arctan2( sz, sqrt(sx*sx + sy*sy))

Edit: This will give you a form of slope detection which does not parallel 360 2D Sonic games (in that you will not be able to run up walls and it will get kinda weird at extremely steep slopes). To do that, you will probably need an orientation vector for the player which is determined from the normal of the triangle and then you could rotate all those calculations to that orientation.

Link to comment
Share on other sites

  • 5 weeks later...

Going to bump this topic to say that unfortunately the previous post hasn't helped me at all due to being too complicated for my insignificant brain, however I've gotten a better idea of what I'm doing, and am prepared to reword/update the details of my question.

Here's the deal, I have the player's location, and angle on a vertical axis (Your standard turning left and right rotation like virtually every 3D game has, not anything complicated) I know he is standing on a triangle, and I know the normal of that triangle. How can I use this information to find his other rotation values? Any help is appreciated, but if at all possible, maybe avoid using too much complicated terminology?

Edit: Rereading the previous post, I don't think it even covers what I was asking, hence my confusion.

Link to comment
Share on other sites

If you don't know the math to figure out the alignment based on standard alignment and normal vector alone, you aren't ready to try and make a 3D game engine.

I'd suggest Linear Algebra. The only other class where you'll really learn how to do this is Calc 3. Without the concepts in either of those, your understanding of whats going on here is going to be limited at best. Yeah, you could try to ignore those issues, but it will constantly hold you back.

Actually, if you had a really strong understanding of the basics of trigonometry, that would probably be enough...

Probably the two best people to ask for help on this subject are Dami and Mark.

Link to comment
Share on other sites

I'd honestly assumed that by posting this question in a public forum, I was asking everyone and it didn't matter who was best suited to the problem.

If you're saying I should give up though, I'd rather not, although it does feel like I'm constantly asking someone for help.

Link to comment
Share on other sites

The thing is there's a lot of ways to express a "rotation value." In fact, you already have all the information (the normal vector and the "twist" rotation of that normal vector). You're kinda asking for the things which you already have. And so it makes it a matter of converting them to another form, which makes me ask "what do you need this for" so that I know what form you need to convert them to. Are you trying to figure out the rotation for purposes of setting the object transforms (the matrices which rotate a 3D model) so that it draws correctly (that was a bit of an after thought in my first post, since it's just a couple of trig functions)? Or are you trying to figure it out so that Sonic stays on the triangle as he walks (that's what the majority of my first post covers)? Or are you doing it for some other reason entirely?

Link to comment
Share on other sites

I'd honestly assumed that by posting this question in a public forum, I was asking everyone and it didn't matter who was best suited to the problem.

If you're saying I should give up though, I'd rather not, although it does feel like I'm constantly asking someone for help.

I'm not saying you should give up. I'm saying you should shelf the idea of working on a project beyond your current comprehension until you have that level of comprehension. Oooootherwise, you'll find yourself stuck more than you are actually getting work done. Believe it or not, I've walked that line before.

Link to comment
Share on other sites

I suppose I should clarify what I'm actually trying to do here then.

Basically, I've been trying to reverse engineer Irrlicht's built-in collision functions into a workable "vector based gameplay fad" Sonic fangame. So far it's proven horribly hacked, but mostly acceptable. (Contradiction? You be the judge.)

I actually kinda like it as-is, and might just hack in proper acceleration and call it quits, but still it seems like a good idea to at least try. Basically as-is the player is freely able to move around with WASD, but is responsible for aligning himself with oncoming slopes himself. I was wanting to make it so that he doesn't have to, and the game simply "forces" him into the angle needed. I can already use the built in functions to find the vector of the normal of the triangle the player's standing on, I'm just not sure how to translate that into degrees of rotation. I suppose since I've got an engine doing about 80% of the work for me, I don't really understand it myself. Hence most help anyone gives me is probably going to be in vain.

But again, it seems to be too much math for a little sophomore like me who hasn't even had a chance to take trig, and I'm actually started to like it as-is. So yeah.

Link to comment
Share on other sites

ShootingStar, the whole alignment madness need to be done depending based upon how the actual rotations is done. Irrlicht is purely matrix based, or uses quaternions?

If it's matrix based, you could create a matrix with the up vector set to the triangle normal and apply that to a clean transformation matrix (an identity matrix with scale and the rotation over the y axis). On the other hand, if it uses quaternions... duh, you'll better find help on the Irrlicht forums :3

________________________________

Implementation of aligning to up vector: link

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...