4
votes

I am currently building a game about infrastructure building and management in XNA(C#). What I am currently trying to achieve is to make the game draw 'bends' between train tracks. I want to do this by drawing only a certain part of a circle texture.
In other words, I only want to draw a "pizza slice" of this texture. The slice of the circle that needs to be drawn is based on three points:

  • The center of the sprite;
  • A variable position 'a';
  • A variable position 'b';

These three points together determine how much of my circle is drawn on the screen e.g. how big the pizza slice is.

To put it simply: If I have a circle and cut it from the centre to a point 'a' and then again from the centre to a point 'b', how can I only draw the part I've just cut out?

This slice has to be altered in real-time, so the slice becomes bigger and smaller based on those two positions 'a' and 'b'.

What is the best way to achieve this effect?

1

1 Answers

2
votes

This can be done entirely in the shader with relatively simple trig. In your pixel shader (effect) try something like:

float dX = b.x - a.x;
float dY = b.y - a.y;
float theta = Math.Atan2(dY, dX);

Note that theta will be in radians. Then just check if theta is within your limits. Say, between 0 and pi/2. If it is, then sample the texture, if not then return a float4 with no alpha (transparent). That should give you the top right quarter of your texture with the rest masked out. If you want to convert theta to degrees you can do that too, but I recommend staying with radians as it makes your math so much easier.

You'll have to set your limits with

effect.Parameters['thetamin']=/*minTheta*/;
effect.Parameters['thetamax']=/*maxTheta*/;

Before you call EffectPass.Apply().