-1
\$\begingroup\$

How would I convert this into C#? This is for a pong game and I am having a difficult time understanding. This is detecting the collision between a ball and a paddle. Can you comment please to help me understand this further, thank you. We have a picture to help enter image description here

And here is the C++ code that was used that I would like to see converted to c#

float minimum_distance(vec2 v, vec2 w, vec2 p) {
  // Return minimum distance between line segment vw and point p
  const float l2 = length_squared(v, w);  // i.e. |w-v|^2 -  avoid a sqrt
  if (l2 == 0.0) return distance(p, v);   // v == w case

  // Consider the line extending the segment, parameterized as v + t (w - v).
  // We find projection of point p onto the line. 
  // It falls where t = [(p-v) . (w-v)] / |w-v|^2
  // We clamp t from [0,1] to handle points outside the segment vw.
  const float t = max(0, min(1, dot(p - v, w - v) / l2));
  const vec2 projection = v + t * (w - v);  // Projection falls on the segment
  return distance(p, projection);
}
\$\endgroup\$
1
  • \$\begingroup\$ Not sure why you want us to convert that to c#, this code is very trivial... Did you even try? \$\endgroup\$ Commented Sep 13, 2016 at 22:20

2 Answers 2

0
\$\begingroup\$

The code you provided in your question is syntactically correct C# already.

All that you lack are the vec2 structs and the mathematical functions being used in that code. Fortunately, the XNA library provides all of this for you in the Vector2 structure. ( https://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector2_methods.aspx?f=255&MSPPError=-2147217396 )

I won't convert everything for you as that's not a good way for you to learn, but I will show you how to convert the first two lines of code.

In C++:

float minimum_distance(vec2 v, vec2 w, vec2 p) {

In C#:

private float minimum_distance(Vector2 v, Vector2 w, Vector2 p) {

This conversion is extremely simple, effectively just replacing the references to the C++ vec2 object with the C# Vector2 structure.

Now for the next line, In C++:

const float l2 = length_squared(v, w);

In C#:

float l2 = Vector2.DistanceSquared(v, w);

On this line, I've removed the const modifier, because although correct, it does not really do anything useful in this case from what I can see. I've then replaced the C++ function being used with the equivalent method provided by XNA. The name changes, but the operation should be identical.

This entire function can be converted in the same way as Vector2 also contains methods for Dot, Min, Max, and Distance.

\$\endgroup\$
-2
\$\begingroup\$

Here you go, I just used a convertor so idk if it works but I converted it for you

private float minimum_distance(vec2 v, vec2 w, vec2 p)

// Return minimum distance between line segment vw and point p
const float l2 = length_squared(v, w);
// i.e. |w-v|^2 -  avoid a sqrt
if (l2 == 0.0) {
    return distance(p, v);
}
// v == w case
// Consider the line extending the segment, parameterized as v + t (w - v).
// We find projection of point p onto the line. 
// It falls where t = [(p-v) . (w-v)] / |w-v|^2
// We clamp t from [0,1] to handle points outside the segment vw.
const float t = max(0, min(1, dot(p - v, w - v) / l2));
const vec2 projection = v + t * (w - v);
// Projection falls on the segment
return distance(p, projection);
\$\endgroup\$
1
  • \$\begingroup\$ Whats with the down vote? \$\endgroup\$ Commented Sep 22, 2016 at 0:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.