Object's pushed by touch

Official forum for the Chipmunk2D Physics Library.
Post Reply
tobe_sta
Posts: 6
Joined: Thu Mar 22, 2012 5:55 pm
Contact:

Object's pushed by touch

Post by tobe_sta »

I have an app that consists of ~10 circles, that can all be pushed and dragged around screen with 0 gravity. Before starting to use Chipmunk, I was able to push (on touch, if the touch was within 100px of the circle, it'd push it away from the point of touch, much like a magnet repelling another magnet) my circles (UIViews) around using this method:

Code: Select all

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
  CGPoint checkPoint = [touch locationInView:self.view];
  
  // Allows grabbing and pushing of circles
  for (UIView *circleView in self.view.subviews) 
  { 
    float threshold = 100;
    CGPoint center;
    
    float distx = circleView.center.x - checkPoint.x;
    float disty = circleView.center.y - checkPoint.y;
    float dist = sqrt(distx * distx + disty * disty);
    
    // Can push circle
    else if (dist < threshold)
    {
      center.x = checkPoint.x + threshold * distx / dist;
      center.y = checkPoint.y + threshold * disty / dist;
      circleView.center = center;
    }
  }
}
Now having made the transition to Chipmunk, this method doesn't work as before. Is there a Chipmunk version of pushing objects around, without actually touching the object?

Thanks.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Object's pushed by touch

Post by slembcke »

You have a number of options. You could insert a new circle that moves around with the touch. You'd want to control the circle's body much like the mouse grabbing works in the demo app. You could apply forces based on distance to the touch. This would be pretty simple.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tobe_sta
Posts: 6
Joined: Thu Mar 22, 2012 5:55 pm
Contact:

Re: Object's pushed by touch

Post by tobe_sta »

Thanks for the reply.

I don't think creating a circle at the touch location would work as desired. If the user moves their finger quickly around screen, the circle would lag behind the touch and create a delayed response time.

How would forces based on distance to touch work?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Object's pushed by touch

Post by slembcke »

What would cause lag with that? You do need to smooth out the coarse input some, so that will cause a little lag, but that's unavoidable.

Having a rigid object to collide with is going to be your best option. Using forces will just make things feel mushy and increase the lag further.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tobe_sta
Posts: 6
Joined: Thu Mar 22, 2012 5:55 pm
Contact:

Re: Object's pushed by touch

Post by tobe_sta »

When I talk about lag, I mean the touch screen latency (as shown here http://www.geek.com/articles/gadgets/mi ... n-2012039/). I will try out the circle that follows the touch around, see how it feels.

How do you target the object within the chipmunk space? I can set the view that houses the circle to the touch location, but the space object doesn't move with it (which is weird because it separates the uibutton circle and the chipmunk circle).
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Object's pushed by touch

Post by slembcke »

You have to tell the rigid body to move either by applying forces, adding constraints to it or updating its position and velocity.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tobe_sta
Posts: 6
Joined: Thu Mar 22, 2012 5:55 pm
Contact:

Re: Object's pushed by touch

Post by tobe_sta »

I decided that the best way for this to work would be to initialise the object in touchesBegan, and then remove it in touchesEnded. This is so I don't have an invisible object sitting in my screen interfering with the other objects. I then set the initialisation point to my touch point. How would I tell it not to initialise if I hit an chipmunk object?

touchesBegan:

Code: Select all

touchSpaceObject = [[TouchSpaceObject alloc] init];
  [self.view addSubview:touchSpaceObject.button];
  [space add:touchSpaceObject];
touchesEnded:

Code: Select all

[space remove:touchSpaceObject];
  [touchSpaceObject.button removeFromSuperview];
Rather than starting a new question, is there a way to have this specific object go outside the bounds of the space? I will still controlling it within the space, but can the edges of an object leave the chipmunk space?

EDIT: I added [touchSpaceObject class]; to the spaces' group and that fixed the latter of the questions (I don't understand why but it did).
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Object's pushed by touch

Post by slembcke »

tobe_sta wrote:How would I tell it not to initialise if I hit an chipmunk object?
You mean if it's going to be inserted into the space where an object already exists? You can use a shape query for that.

For excluding collision with the world bounds, you can use the layers or group feature of collision shapes to filter out collisions. It sounds like that's what you did.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
tobe_sta
Posts: 6
Joined: Thu Mar 22, 2012 5:55 pm
Contact:

Re: Object's pushed by touch

Post by tobe_sta »

Here's the code I ended up with:

Code: Select all

ChipmunkShape *shape = [space pointQueryFirst:point layers:GRABABLE_MASK_BIT group:nil];
  if(!shape){
    // Magic goes here
  }
Thanks for all your help!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 9 guests