Math for Game Developers - Backstabbing Part 2

  Рет қаралды 27,793

Jorge Rodriguez

2 жыл бұрын

Question? Leave a comment below, or ask me on Twitter: VinoBS
The game I used to implement this was Double Action. You can check it out here: doubleactiongame.com

Пікірлер: 61
@varuntyagi6116
@varuntyagi6116 11 жыл бұрын
"Now I'm gonna go behind and see if our dot product is working for..oh..okay I think its working" hahahahaha
@daywalkertpify
@daywalkertpify 4 жыл бұрын
Seven years since creation of this video tutorial but still very helpful and adoptable to the game development today. Thank you very much for this great content!
@charlie12486
@charlie12486 2 жыл бұрын
I watched all of your videos and now i see this a signal of life from a legend
@CoffeeOverdosed
@CoffeeOverdosed 5 жыл бұрын
Omg that visual representation was so on point with the lesson hahahaha!! "I think it worked" was the best reaction
@To-mos
@To-mos 6 жыл бұрын
This is an insanely simple solution to the problem. I did this through localizing the player offsets from one another to their perspective by subtracting their world transformation and using atan2 to figure out if they were looking at one another or away. This also allowed for solving who was looking at who. Now I'm gonna replace it all using this :D
@JorgeVinoRodriguez
@JorgeVinoRodriguez 6 жыл бұрын
Glad I could help! :)
@JorgeVinoRodriguez
@JorgeVinoRodriguez 11 жыл бұрын
Thanks! Glad I could help! Most of the videos have example code, the best way to learn is to try it out.
@maschill92
@maschill92 11 жыл бұрын
"I think it worked." HAHAHA!!!
@omg_look_behind_you
@omg_look_behind_you 2 жыл бұрын
wow bro, I've missed your videos so much. I always considered you the Khan academy of math for game development. Hope we will be seeing more of you and all is right on your end of the world.
@mreazl6227
@mreazl6227 2 жыл бұрын
this isn't a new video, check other comments
@unity6926
@unity6926 2 жыл бұрын
Holy crap you're back!!!! Awesome
@walker-snow
@walker-snow 2 жыл бұрын
Some comments are really old. I doubt it's a new video.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 2 жыл бұрын
I appreciate the excitement but no, I just made a previously private video public :)
@tjhaskelh96
@tjhaskelh96 11 жыл бұрын
ah ok. Great videos! Just finished my linear algebra class and intro to comp sci class (scheme). I really want to get in to game development so it's really cool to watch these and at least get some perspective on how the math is translated to code even though I don't know too much syntax yet.
@joysaha3927
@joysaha3927 2 жыл бұрын
Please make more videos like this man! :)
@mohammedgoder
@mohammedgoder 2 жыл бұрын
Damn, its been a while since you've posted.
@RussellTeapot
@RussellTeapot 6 жыл бұрын
This series is amazing, explanations are crystal clear and essentially language agnostic (I'm following this in JavaScript with no problems), excellent job! I have a question: since for the dot product (d.p.) we use normalized vectors, is it wise to implement d.p. already with normalized vectors? What I mean is: instead of passing normalized vectors, I could pass the vector and "grab" the normalized version within the d.p. function, since I already implemented a getter for the normalized version of the vector. Or it's best to leave the d.p. as it is, since it can be used in other situations, in which "regular" (not normalized) vectors are used?
@JorgeVinoRodriguez
@JorgeVinoRodriguez 6 жыл бұрын
Thank you for the kind words :) I think your question is perceptive and shows that you've thought about the issue and you care about good code, so nice work there. There are many applications for dot product on non-normalized vectors (which I will cover in later parts of this series) so I would recommend you keep your dot product function to just do dot product. As a general rule, it can be a drawback if you create a function that does too much at once. You want to be helpful so you normalize vectors in your function, but the side effect is that if anyone wants only the dot product and not the normalizing they're up.a creek. On the upside, if you provide a dot product that only does dot product, then you can write something like: float AngleBetweenTwoVectors(vec3 a, vec3 b) { return dot(normalize(a), normalize(b)); } and use that method if you like.
@julianaskuratovsky8701
@julianaskuratovsky8701 5 ай бұрын
super ultra amazing!!!
@CatzHoek
@CatzHoek 2 жыл бұрын
I was ready to complain about the background noise but then saw saw this is a 8 year old video you just made available again. So maybe that´s okay.
@wasiimo
@wasiimo 8 жыл бұрын
After watching the vector collinearity with scalars videos on Khan Academy this makes a lot of sense. Making these normalized makes so is a great idea. Since the only time the red player's direction would be facing the same way as the blue player's direction is when the red player's length is 1 or -1. At first the dot product equation seemed rather bizarre to me but now it makes a lot of sense. By the way did you ever cover atan2?
@JorgeVinoRodriguez
@JorgeVinoRodriguez 8 жыл бұрын
+fun Tertain I think I may have talked about atan2 in the mouse control video.
@wasiimo
@wasiimo 8 жыл бұрын
Jorge Rodriguez You never did
@JorgeVinoRodriguez
@JorgeVinoRodriguez 8 жыл бұрын
+fun Tertain Oops sorry. Try www.cplusplus.com/reference/cmath/atan2/ and the wikipedia article.
@sweatyeti
@sweatyeti 9 жыл бұрын
Hey, thanks so much for this video. This is a neat technique! Since I've read it's nice to avoid using square root operations when programming, is there a way to avoid this when normalizing the "blue-red" vector?
@JorgeVinoRodriguez
@JorgeVinoRodriguez 9 жыл бұрын
+sweatyeti If you only care about whether something is completely behind something else then you don't have to do any normalization. If the dot product is negative the vectors point in opposite directions (less than 90 degrees), and positive in the same direction. But remember, in this particular case the code is only called once in a while (if you think about it, compared to everything else games have to do, this particular dot product doesn't occur very often, once every few frames at best) so the impact on performance is minimal. Check out the Amdahl's Law video and the rest of the optimization series for a deeper explanation of that. But you use dot product a lot for other stuff as well and in those cases it's usually straightforward to avoid sqrt() by e.g. precomputing.
@sweatyeti
@sweatyeti 9 жыл бұрын
Jorge Rodriguez Thanks so much for the quick and thorough response. For better or for worse (probably for worse), I'm always keen to optimization opportunities, but you make a good point about how situational this event is. I'll check out Amdahl's Law and learn more about better optimization habits.
@theleopeople5771
@theleopeople5771 Ай бұрын
I don't understand a thing... don't player and enemy in the final example face in the same direction? So it would mean that dot product should be 1, not -1 What is it I'm missing here?!
@wasiimo
@wasiimo 9 жыл бұрын
This clicks so easily.
@abandoned7501
@abandoned7501 5 жыл бұрын
very nice, why unlisted tho?
@ClimFreeFeelRain
@ClimFreeFeelRain Жыл бұрын
3:57 when the code you think is not working actually works (too well)
@harrynewton4786
@harrynewton4786 9 жыл бұрын
Ok so just to clarify some things, so the normalize function is the calculation of the R-B/absolute(R-B)? and from there on the dot product equation takes those values and does the magic stuff.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 9 жыл бұрын
That's right :)
@Xgamesvidoes
@Xgamesvidoes 4 жыл бұрын
Hahaha! Great videos so far! :D
@NonsensGaming
@NonsensGaming 4 жыл бұрын
if i do this i have the problem that if i stand infornt of the enemy i get 0 and if i stand beheind the enemy i get 0 as well
@johnwheel2591
@johnwheel2591 2 жыл бұрын
Face stab is so true
@ezetreezy
@ezetreezy 8 жыл бұрын
This may a bit off topic, but how would using the cross product to determine(what direction left or right) a main player needs to turn to see an enemy player. (assuming you have the position and facing vectors)(positive z in the up vector). Your videos rock btw.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 8 жыл бұрын
If you have the direction vector that the player is facing and a direction vector to an enemy, the cross product of these will be perpendicular to both of them, so it will be up or down. Not sure that's what you want. If you want to figure out how to turn to see an enemy player, maybe you should use plain old simple angles. In other words, find an angle that describes the current rotation of the player in world coordinates, then another angle that describes the direction to the enemy in world coordinates, then subtract. Then you have a rotation angle that you can apply to your rotation.
@ezetreezy
@ezetreezy 8 жыл бұрын
Jorge Rodriguez Yeah thats what i thought. I was asked this by a professor and I did not know how to answer it using the Cross Product.
@ezetreezy
@ezetreezy 8 жыл бұрын
i believe this is the answer. //get cross between main player facing and up //vector Vector3 cross = mainPlayer.Facing.Cross(new Vector3(0,0,1)); //get the vector from enemy to mainPlayer Vector 3 inBetween = new Vector3((enemy.Position.X - mainPlayer.Position.X, enemy.Position.Y - mainPlayer.Position.Y, enemy.Position.Z - mainPlayer.Position.Z); //compute the angle float dot = cross.dot(inBetween); if (dot
@JorgeVinoRodriguez
@JorgeVinoRodriguez 8 жыл бұрын
There should be some kind of vector subtraction procedure you can call instead of doing it manually like that. Also, there may be some kind of global constant for the up vector. Also, you may want to make a check or guard for when the player is facing directly up or down - this code will fail in those situations. Other than that, looks good. :)
@ezetreezy
@ezetreezy 8 жыл бұрын
Jorge Rodriguez Awesome. Thanks for the help.
@JorgeVinoRodriguez
@JorgeVinoRodriguez 11 жыл бұрын
C++ :)
@Ark0Hak
@Ark0Hak 5 жыл бұрын
Hi in 2019 ). in the previous video you promised to show in the next video mario's source code.
@pascalampere6098
@pascalampere6098 8 жыл бұрын
So with 3D unit vectors if the dot product is negative that means the normalized vector br points to a point on the surface of the back half sphere of the view vector v?
@JorgeVinoRodriguez
@JorgeVinoRodriguez 8 жыл бұрын
+Pascal Ampere Yes.
@sweatyeti
@sweatyeti 9 жыл бұрын
I created a quick demo using this concept on KhanAcademy for those who are interested: www.khanacademy.org/computer-programming/demo-collision-facing-behind/5554809164398592
@murrayKorir
@murrayKorir 8 ай бұрын
Really good stuff.
@PETRUSAGOSTINHO
@PETRUSAGOSTINHO Жыл бұрын
Where is the link of first part?
@smackerlacker8708
@smackerlacker8708 7 жыл бұрын
So.... what I've learned so far is.... Dot product good?
@JorgeVinoRodriguez
@JorgeVinoRodriguez 7 жыл бұрын
Very good. I can help if you have specific questions.
@harrynewton4786
@harrynewton4786 9 жыл бұрын
did you normalize both objects? the Vector v and br?
@JorgeVinoRodriguez
@JorgeVinoRodriguez 9 жыл бұрын
The equation for dot product is A * B = |A| |B| cos
@harrynewton4786
@harrynewton4786 9 жыл бұрын
Jorge Rodriguez ok i understand now,thanks
@dogukanozdemir1914
@dogukanozdemir1914 8 жыл бұрын
Can someone please help me the output seems to give me 1 i dont understand why: Point player(3.0f, 0.0f); Vector playerDirection(2.0f, 0.0f); Vector normalizedPDirection = playerDirection.normalize(); Point enemy(3.0f, 3.0f); Vector br = (enemy - player); Vector normalizebr = br.normalize(); float Angle = getDotProductOf(normalizedPDirection, normalizebr); std::cout
@JorgeVinoRodriguez
@JorgeVinoRodriguez 8 жыл бұрын
NormalizedPDirection is playerDirection.normalize() = (2.0, 0.0).normalized() = (1.0, 0.0) normalizebr is br.normalize() = (enemy - player).normalize() = ((3.0, 3.0) - (3.0, 0.0)).normalize() = (0.0, 3.0).normalize() = (0.0, 1.0). The dot product of (1, 0) and (0, 1) should be 0. If you're getting 1 then your dot product must be different than mine.
@dogukanozdemir1914
@dogukanozdemir1914 8 жыл бұрын
Jorge Rodriguez this is how my dot product looks like but I'm still getting 1 float getDotProductOf(const Vector & a, const Vector & b) { return (a.x * b.x + a.y + b.y); } just in case here is the whole code: pastebin.com/Zm22r9SZ
@dogukanozdemir1914
@dogukanozdemir1914 8 жыл бұрын
Just as I post my reply i saw the problem the "a.y + b.y" should be * not + lol thanks:D
@JorgeVinoRodriguez
@JorgeVinoRodriguez 8 жыл бұрын
:)
@tjhaskelh96
@tjhaskelh96 11 жыл бұрын
Is this in java?
@To-mos
@To-mos 5 жыл бұрын
C++ from the looks of it
1 сквиш тебе или 2 другому? 😌 #шортс #виола
00:36
Epic Reflex Game vs MrBeast Crew 🙈😱
00:32
Celine Dept
Рет қаралды 10 МЛН
إخفاء الطعام سرًا تحت الطاولة للتناول لاحقًا 😏🍽️
00:28
حرف إبداعية للمنزل في 5 دقائق
Рет қаралды 56 МЛН
1 сквиш тебе или 2 другому? 😌 #шортс #виола
00:36