Windows Phone 8: Collision in rectangles - c#

So im working on a windows phone app and i need a way to detect weather the user has dragged a rectangle into another. ive got al of the dragging and everything sorted but i cannot for the life of me figure out how to detct a collision, i looked a little into xna and managed to bodge the bellow together if anybody could help out it would be much appriciated.
double BoxLeft = characterMain.Margin.Left;
double BoxTop = characterMain.Margin.Top;
Rect r1 = new Rect(BoxLeft, BoxTop, 20, 20);
txtblck2.Text = r1.X.ToString() + r1.Y.ToString();
double badBoxLeft = BadGuy.Margin.Left;
double badBoxTop = BadGuy.Margin.Top;
Rect r2 = new Rect(badBoxLeft, badBoxTop,12, 100);
txtblck3.Text = r2.X.ToString() + r2.Y.ToString();
r1.Intersect(r2);
if (r1.IsEmpty)
{
MessageBox.Show("You Did it");
}

Collision with rectangles is pretty easy, I don't know if u have searched anything but this will help you out a little:
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.rectangle.intersects.aspx
Also some tutorials you'll probably want to read:
http://jamessadlier.wordpress.com/tutorials/xna-4-0-collision-detection/
http://codeforcake.com/blog/?p=160
The basic collision works like this:
It basically does what it says.
if(r1.Intersects(r2))
{
//Collision happend
//Do something
}
Next time when you have a question be sure to check the internet real good, there are a lot of tutorials and good examples out there.
Good luck with your project!

Related

Load .3ds model with high number of polygons - Helix Toolkit Sharp DX

I used HelixToolkit a couple of f times before, and I managed to load my model (all were small-sized models), and they all worked properly. Now, I have many models with lots of polygons for a project (some have about 10000 polygons). So, when I ran my program, I got an error: "System.ArgumentOutOfRangeException: 'Non-negative number required.'
So, I googled the problem, and somebody mentioned that he uses Helix Sharp DX for his large models. I tried Helix Sharp DX. My XAML code is something like this:
<helix:Viewport3DX x:Name="Viewport3D" ZoomExtentsWhenLoaded="True">
<helix:GroupModel3D x:Name ="LoadedModels" />
</helix:Viewport3DX>
And my C# code is something like this:
Viewport3D.Items.Add(new DirectionalLight3D() { Direction = new System.Windows.Media.Media3D.Vector3D(-1, -1, -1) });
var reader = new HelixToolkit.Wpf.SharpDX.StudioReader();
var model = reader.Read(path);
foreach (var modelPart in model)
{
var sharpModel = new MeshGeometryModel3D()
{
Geometry = modelPart.Geometry,
//Material = modelPart.Material,
Name = modelPart.Name,
//Transform = new MatrixTransform3D(modelPart.Transform.ToMatrix3D())
};
LoadedModels.Children.Add(sharpModel);
}
But, I again got the same error. I wonder if there is any problem with my code; or if we should try to reduce the number of polygons.
Any help/suggestion regarding this issue is appreciated. I prefer to solve it via coding since the polygon count reduction will take most of the time of the project timeline.
Thanks

c# SharpDX ScaleEffect Interpolation question

i've been using this wonderful library for low impact screen recording software however im at a point where i need to use a DirectX Scale Effect to scale the image down with anisotropic filtering.
I can create the effect just fine and set up most of its parameters, however im not able to set the most important one for me scaleEffect.SetValue((int)ScaleProperties.InterpolationMode, (int)SharpDX.Direct2D1.ScaleInterpolationMode.Anisotropic);
As soon as the program runs it crashes at that line with an invalid parameter exception.
I've searched and couldnt find anything wrong with the code, but then again i have almost 0 experience with anything Direct2D.
Here is the full code for that specific effect for reference:
SharpDX.Direct2D1.Effect scaleEffect = new D2D.Effect(textureDc, D2D.Effect.Scale);
scaleEffect.Cached = true;
scaleEffect.SetInput(0, frameBitmap, false);
var centerPoint = new SharpDX.Vector2(0, 0);
var newSize = new SharpDX.Vector2(0.2f, 0.2f);
scaleEffect.SetValue((int)ScaleProperties.CenterPoint, centerPoint);
scaleEffect.SetValue((int)ScaleProperties.Scale, newSize);
//Crashes Here: scaleEffect.SetValue((int)ScaleProperties.InterpolationMode, (int)SharpDX.Direct2D1.ScaleInterpolationMode.Anisotropic);
Error Message: SharpDX.SharpDXException: 'HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: The parameter is incorrect.'
textureDc.BeginDraw();
textureDc.DrawImage(scaleEffect, InterpolationMode.Anisotropic);
textureDc.EndDraw();
Thank you very much in advance for any help!
UPDATE:
As per #Simon Mourier comment, the solution to avoid this bug somewhere in the SharpDX API, is to use the SharpDX.Direct2D1.Effects.Scale instead.
This way, i can confirm the InterpolationMode does no longer crash and works as intended!
Here is the new, working code for anyone running into the same issue.
SharpDX.Direct2D1.Effects.Scale scaleClass = new D2D.Effects.Scale(textureDc);
scaleClass.SetInput(0, frameBitmap,false);
scaleClass.Cached = true;
var centerPoint = new SharpDX.Vector2(0, 0);
var newSize = new SharpDX.Vector2(0.2f, 0.2f);
scaleClass.CenterPoint = centerPoint;
scaleClass.ScaleAmount = newSize;
scaleClass.InterpolationMode = InterpolationMode.Anisotropic;
textureDc.BeginDraw();
textureDc.DrawImage(scaleClass);
textureDc.EndDraw();
PS: For anyone wondering about casting the value to uint instead using the first method, it returns the following error:
(Argument1 cannot convert from uint to int)
So it semms that there might be some underlying bug in this specific scenario.
One solution is to use the SharpDX.Direct2D1.Effects.Scale directly which is a wrapper over the Effect class and comes with an InterpolationMode property.

C# Control Microphone volume form application

want to control microphone volume from inside of my application, i tried to search it and tried many different solutions but haven't got any success :( all solutions are bit confusing and incomplete.
already spend lots of time so i need your help guys, if anybody help me to do this. i want to control Microphone level using c# from my application.
get MicrophoneLevel
set MicrophoneLevel
I hope you just need to adjust the volume level for your own application only. You can do that with NAudio perhaps.
UnsignedMixerControl volumeControl;
int waveInDeviceNumber = 0;
var mixerLine = new MixerLine((IntPtr)waveInDeviceNumber,
0, MixerFlags.WaveIn);
foreach (var control in mixerLine.Controls)
{
if (control.ControlType == MixerControlType.Volume)
{
volumeControl = control as UnsignedMixerControl;
break;
}
}
volumeControl.Percent = 30; // you are setting volume here, as a percentage.
For more information, refer to the article .NET Voice Recorder.

XNA 3.1 DrawUserPrimitives with Anti Aliasing - MultiSampling doesn't work?

So this is a continuation of a question I asked earlier today. I've built myself some nice looking ribbon trails using XNA 3.1's DrawUserPrimitives method, essentially by expanding a polygon as motion occurs. It all looks super sleek and nice, except for one thing - anti-aliasing. I cannot for the life of me work out how to apply it.
I've set this in the game constructor:
graphics.PreferMultiSampling = true;
And I've also added this to test for the hardware:
graphics.PreparingDeviceSettings += new EventHandler<PreparingDeviceSettingsEventArgs>((sender, e) =>
{
PresentationParameters parameters = e.GraphicsDeviceInformation.PresentationParameters;
parameters.MultiSampleQuality = 0;
#if XBOX
pp.MultiSampleType = MultiSampleType.FourSamples;
return;
#else
int quality;
GraphicsAdapter adapter = e.GraphicsDeviceInformation.Adapter;
SurfaceFormat format = adapter.CurrentDisplayMode.Format;
if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format, false, MultiSampleType.FourSamples, out quality))
{
parameters.MultiSampleType = MultiSampleType.FourSamples;
}
else if (adapter.CheckDeviceMultiSampleType(DeviceType.Hardware, format, false, MultiSampleType.TwoSamples, out quality))
{
parameters.MultiSampleType = MultiSampleType.TwoSamples;
}
#endif
});
By adding some print lines, I know my hardware can support 4 sample AA, but this all seems to make no difference. I just can't seem to get this to work.
Here's a screenshot of one of my trails with all of that code applied:
I'd really appreciate some help. I looked at this a while ago for a solution to a different problem, and couldn't get it to work then, either.
Well, cheers.
Fixed this one, too!
The issue was that, while the back buffer was getting the right anti-alias settings, the render target wasn't. This meant that drawing to the render target was done without AA, but the texture that was then drawn to the back buffer was done with it. I've fixed it now.

CA overlay on UISegmentedControl is overwritten in iOS5

I have the following code, which in iOS 4 produces the look of a "disabled" UISegmentedControl. As the attentive reader will notice, I am developing in MonoTouch, thus the c# syntax.
private void MakeOverlayOnLastRatingSegment()
{
RemoveOverlayOnLastRatingSegment();
lastRatingDisabledLayer = new CALayer();
lastRatingDisabledLayer.Frame = new RectangleF(new PointF(0, 0), segmLastRating.Frame.Size);
lastRatingDisabledLayer.CornerRadius = 10.0f;
lastRatingDisabledLayer.BackgroundColor = UIColor.FromRGBA(0.5f, 0.5f, 0.5f, 0.2f).CGColor;
segmLastRating.Layer.AddSublayer(lastRatingDisabledLayer);
}
And the result is:
But on iOS5 the result is a bit different. The segment before the selected cell is written on top of my gray layer. It takes a bit close look - more evident on a real device instead of simulator. For some reason the screendump from the simulator is very light.
Has anyone else experienced this, and do somebody have a workaround?
Best regards
/Anders
Could you add your overlay layer to the layer of the segmented control’s superview? Or perhaps the segmented control’s layer’s superlayer?

Categories

Resources