What is the best method in XNA to have the player paint smoothly on the screen?
Asked Answered
H

1

6

I'm developing a wp7 game where the player draws lines in the program and a ball bounces off of them. I'm using XNA and farseer physics. What is the best method for a user to draw a line, and then for the program to take it and turn it in to a physics object, or at least a list of vector2s? I've tried creating a list of TouchLocations, but it ends up spotty if the user does not draw very slow, like the picture I've attached. Any suggestions? Thanks http://img829.imageshack.us/img829/3985/capturehbn.png

Here's some code: I'm using the gamestatemanagement sample, and this is in the HandleInput method

foreach (TouchLocation t in input.TouchState) {
  pathManager.Update(gameTime, t.Position);
}

The pathManager class manages a collection of path classes, which are drawable physics objects. Here is pathManager.Update

public void Update(GameTime gameTime, Vector2 touchPosition) {
  paths.Add(new Path(world,texture, new Vector2(5,5) ,0.1f));
  paths[paths.Count-1].Position = touchPosition;
}

This is just what I'm doing now and I'm willing to throw it out for anything. You'd think that having a 5x5 rectangle for each touch location would kill the performance, but using farseer I didn't see any drops, even with a mostly full screen. However, this system doesn't create a smooth line at all if the line is drawn fast.

I doubt this helps any, but here is the Path constructor.

public Path(World world, Texture2D texture, Vector2 size, float mass) {
  this.Size = size;
  this.texture = texture;
  body = BodyFactory.CreateRectangle(world, size.X * pixelToUnit, size.Y * pixelToUnit, 1);
  body.BodyType = BodyType.Static;
  body.Restitution = 1f;
  body.Friction = 0;
  body.Friction = 10;
}
Handfast answered 24/12, 2012 at 16:23 Comment(6)
Could you paste your relevant code in?Caernarvonshire
do you have to have more than 2 points of a line?Souse
I supoose not, but preferably yes. I can see just a straight line though. How would I go about doing that?Handfast
Im wondering if theres some sort of algorithim to calculate all of the positions inbetween the places it skipped?Handfast
When you say all positions in between, what do you actually mean?Stickseed
I mean when it skips over from one point to the next, like in the image link I posted, if there's a way to find all of the points along the slope of the two points it did record?Handfast
P
1

How do I draw lines using XNA?

the best way to draw primitives is to use the basiceffect shader. this will be accelerated by the hardware. you can also add a texture to it if you'd like.

i'm not sure if its the same on WP7 but this works for Windows7 at least.

Pyrogen answered 26/12, 2012 at 13:37 Comment(3)
Thanks for the answer, I like this method for drawing it, but I'd need to have a list of all the points along the line for collision detection.Handfast
Well any of the Body's created by the BodyFactory should work. Or do you mean when the lines collide with eachother? In farseer theres a thing called a Contact. Whenever a collision occurs a Contact is created that contains some information about where exactly the collision occured.Pyrogen
That makes sense. I don't know why I was stuck on a list of vector2s. I ended up using BodyFactory.CreateEdge using the first and last points of a free drag gesture and using your drawing method. Thanks!Handfast

© 2022 - 2024 — McMap. All rights reserved.