gamecreator Posted September 19, 2013 Share Posted September 19, 2013 I was trying to find these in the documentation and then Math.h but didn't see them. Do we have a function that gets the distance between two 2D points or at least a square root function? Quote Link to comment Share on other sites More sharing options...
franck22000 Posted September 19, 2013 Share Posted September 19, 2013 Hello, here you are (My version for LE2 so you have to juste change the TVec2...): float getDistance(TVec2 fVector1, TVec2 fVector2) { float _X = fVector2.X - fVector1.X; float _Y = fVector2.Y - fVector1.Y; return std::sqrt(_X * _X + _Y * _Y); } 1 Quote You guys are going to be the death of me. Josh Link to comment Share on other sites More sharing options...
YouGroove Posted September 19, 2013 Share Posted September 19, 2013 Or you just can you two Vector3, with each Z value = 0 and just give X and Y for these two Vector3 1 Quote Stop toying and make games Link to comment Share on other sites More sharing options...
franck22000 Posted September 19, 2013 Share Posted September 19, 2013 ??? I dont understand what you said YouGroove. Quote You guys are going to be the death of me. Josh Link to comment Share on other sites More sharing options...
Mumbles Posted September 19, 2013 Share Posted September 19, 2013 Or you just can you two Vector3, with each Z value = 0 and just give X and Y for these two Vector3 There's no point using a 3 component vector for 2D calculations because the Z will never be used. It's the same reason we don't usually use 4 component vectors for points in 3D space. 1 Quote LE Version: 2.50 (Eventually) Link to comment Share on other sites More sharing options...
shadmar Posted September 19, 2013 Share Posted September 19, 2013 Sqrt in lua can be done like this : math.sqrt(some number) or (some number)^0.5 Quote HP Omen - 16GB - i7 - Nvidia GTX 1060 6GB Link to comment Share on other sites More sharing options...
gamecreator Posted September 19, 2013 Author Share Posted September 19, 2013 Thank you franck but it looks like using sqrt breaks Android apps. That's why I asked specifically if there is a Leadwerks version of it. Quote Link to comment Share on other sites More sharing options...
franck22000 Posted September 19, 2013 Share Posted September 19, 2013 Oh sorry ! Well it is strange that std::sqrt does not work since it is from the c++ standard library. You can use this square root function then it should work if you dont need a very accurate precision float sqrt(float x) { union { int i; float x; } u; u.x = x; u.i = (1<<29) + (u.i >> 1) - (1<<22); u.x = u.x + x/u.x; u.x = 0.25f*u.x + x/u.x; return u.x; } 1 Quote You guys are going to be the death of me. Josh Link to comment Share on other sites More sharing options...
gamecreator Posted September 19, 2013 Author Share Posted September 19, 2013 No prob at all. I was sure it would work too but I figured there's a reason that Josh provides math functions that probably already exist in C++ and I guess compatibility is it. I'll give your latest function a shot. Thanks very much franck! Quote Link to comment Share on other sites More sharing options...
Josh Posted September 19, 2013 Share Posted September 19, 2013 Math::Sqrt() is also there. And this: http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/vec3/vec3distancetopoint-r403 Quote My job is to make tools you love, with the features you want, and performance you can't live without. Link to comment Share on other sites More sharing options...
gamecreator Posted September 19, 2013 Author Share Posted September 19, 2013 Math::Sqrt() is also there. I'm 99% sure it wasn't in the header but I'll look again when I get home. It's definitely not here though. And this: http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/vec3/vec3distancetopoint-r403 Thanks! Will try this. And thanks, YouGroove, for originally suggesting it. Quote Link to comment Share on other sites More sharing options...
Mumbles Posted September 19, 2013 Share Posted September 19, 2013 Math::Sqrt() is also there. And this: http://www.leadwerks.com/werkspace/page/documentation/_/command-reference/vec3/vec3distancetopoint-r403 Thanks! Will try this. And thanks, YouGroove, for originally suggesting it. Oh, I see what YouGroove was saying now. I thought he was saying to use Franck's code, but with three component vectors as the parameters. I feel a bit silly now... Quote LE Version: 2.50 (Eventually) Link to comment Share on other sites More sharing options...
gamecreator Posted September 20, 2013 Author Share Posted September 20, 2013 float DistanceBetween2DPoints(Vec2 p1, Vec2 p2) { return Vec3(p1.x,p1.y,0).DistanceToPoint(Vec3(p2.x,p2.y,0)); } Quote Link to comment Share on other sites More sharing options...
Admin Posted September 24, 2013 Share Posted September 24, 2013 Math functions are just provided for consistency. sqrt should work, since Leadwerks calls it internally. Maybe you need to type std::sqrt() or include "Math.h" or <Math.h>? Quote Link to comment Share on other sites More sharing options...
gamecreator Posted September 24, 2013 Author Share Posted September 24, 2013 Thanks. As you said, I tested this again in my other thread and it works. Sorry guys. My Android app definitely crashed before but I have no idea why now. I was sure at the time it was just because of adding sqrt (I didn't need to add any headers and it compiled fine on Express). Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.