Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-04-2011, 10:42 AM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 4
sankey is on a distinguished road
Default Detect collision between images

Have some troubles with detecting collisions.

Exists two images for example the wall and square. main task is make imposible crossing square via wall during touchesMoving. and we can't know from which direction we moving square.

Code:
if (CGRectIntersectsRect(square.frame, wall.frame) {
{
   // what position we need set for stop moving of square??
}
on practice if set coordinates of collision, it works just during conditions of intersection inside the wall frame, when finger over cross wall center , this condition
Code:
if (CGRectIntersectsRect(square.frame, wall.frame)
not working and square moving later cross wall

maybe needs one more conditions, or another restrictions for square position inside collision's condition

thanks
sankey is offline   Reply With Quote
Old 02-05-2011, 06:19 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

It's hard to understand what you're asking, sorry, but I'll try to help.

In general with this kind of collision you need to test if the new position is valid, and if it is not then you do NOT move into the new position. Once you allow the objects to move into a collision you are in trouble.

You want to do something like this:

Code:
//calculate new frame
CGRect newFrame = square.frame;
newFrame.origin = CGPointMake(square.origin.x+xspeed, square.origin.y+yspeed);

//only move if the new frame is OK
if(CGRectIntersectsRect(newFrame, wall.frame)==NO){
     square.frame = newFrame;
}
Your second problem is that touchesMoved is not guaranteed to fire every frame or ever few pixels; if the system is very busy then you might get 100 pixels or more between calls to touchesMoved, which would make you jump right through a wall. You probably need touchesMoved to update some "target" position, and then use an update method every frame to move the square toward the target at constant speed and check for collisions.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-06-2011, 01:34 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 226
mlfarrell is on a distinguished road
Default

You guys and your CGPoints and CGRects

Code:
//old school C way

static BOOL collide(int x1, int y1, int wt1, int ht1, int x2, int y2, int wt2, int ht2)
{
  return (!(((x1 > (x2+wt2)) || (x2 > (x1+wt1)) || (y1 > (y2+ht2)) || (y2 > (y1+ht1)))));
}
__________________


http://vertostudio.com/
mlfarrell is offline   Reply With Quote
Old 02-06-2011, 01:53 PM   #4 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 4
sankey is on a distinguished road
Default

Smasher, many thanks for your help. I will try to implement your solution.

about:
Quote:
Originally Posted by smasher View Post
Your second problem is that touchesMoved is not guaranteed to fire every frame or ever few pixels; if the system is very busy then you might get 100 pixels or more between calls to touchesMoved, which would make you jump right through a wall. You probably need touchesMoved to update some "target" position, and then use an update method every frame to move the square toward the target at constant speed and check for collisions.
i did understand your logic, but still don't know method to implement it. could you tell advise or example for this realisation. if it's not hard for you.

Thanks.
sankey is offline   Reply With Quote
Old 02-06-2011, 04:15 PM   #5 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by mlfarrell View Post
You guys and your CGPoints and CGRects
Yeah, readability is such a fad
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-08-2011, 08:34 AM   #6 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 4
sankey is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
You want to do something like this:

Code:
//calculate new frame
CGRect newFrame = square.frame;
newFrame.origin = CGPointMake(square.origin.x+xspeed, square.origin.y+yspeed);

//only move if the new frame is OK
if(CGRectIntersectsRect(newFrame, wall.frame)==NO){
     square.frame = newFrame;
}
What xspeed, yspeed are mean in this code ? is't predefined variables ? may be in case with tochesMoved will be somthing like that

Code:
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
//calculate new frame
CGRect newFrame = square.frame;
newFrame.origin = CGPointMake( touchLocation.x,  touchLocation.y);

//only move if the new frame is OK
if(CGRectIntersectsRect(newFrame, wall.frame)==NO){
     square.frame = newFrame;
}
i am intresting only in moving during touching , but not with defined speed like during gameloop.

and in this code the same problem as in first post.

as soon as finger crossing wall newFrame not intersect with wall image and as result square move cross wall too
sankey is offline   Reply With Quote
Old 02-12-2011, 10:39 AM   #7 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

>i am intresting only in moving during touching , but not with defined speed like during gameloop.

Okay... but if someone drags their finger very quickly, or the system is busy, they can drag right through a wall. The defined speed fixes that.

>as soon as finger crossing wall newFrame not intersect with wall image and as result square move cross wall too

I think I understand - if someone drags over the wall, the square stays on the old side of the wall while they drag and then "pops" to the new side.

The problem is that your code says this:
(1) is the new position clear?
(2) if so, pop to the new position.

It does not check the positions in between.

The fix is not to "pop" - do no move directly to the new location. You must check every location in between, either by using a loop or my only moving at a specific speed every frame. I prefer the specific speed, otherwise if you tap on one side of the screen the square will still jump there if all of the spaces in-between are clear.

To move at a specific speed, you'd do something like this:
Code:
//in your .h file, two new instance variables
float speedx;
float speedy;

//your touch code, in touchesmoved:
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint targetLocation = [touch locationInView:self.view];

	float dx = targetLocation.x-square.center.x; 
	float dy = targetLocation.y-square.center.y;
	float distanceToTarget = sqrt (dx*dx +dy*dy);

	float squareSpeed = 10 ; //10 pixels per frame; it'd be nicer to have this in tiles per second.
	float steps = distanceToTarget / squareSpeed;
	speedx = dx /steps;
	speedy = dy / steps;

//trigger this method with an NStimer 10 or 20 times a second
-(void)update{
	//calculate new frame
	CGRect newFrame = square.frame;
	newFrame.origin = CGPointMake( square.origin.x+speedx,  square.origin.y+speedy);

	//only move if the new frame is OK
	BOOL isClear = CGRectIntersectsRect(newFrame, wall.frame)==NO; 
	if(isClear){
	     square.frame = newFrame;
	}
}
__________________

Free Games!

Last edited by smasher; 02-12-2011 at 05:11 PM.
smasher is offline   Reply With Quote
Old 02-12-2011, 05:34 PM   #8 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I realized a bug in that code; it chooses a direction when you drag and but never stops moving the square.

You need to check if you're already at the target location and stop updating.

Code:
//in your .h file, two new instance variables
float speedx;
float speedy;
float distance;

//also
#define kSquareSpeed = 10

//your touch code, in touchesmoved:
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint targetLocation = [touch locationInView:self.view];

	float dx = targetLocation.x-square.center.x; 
	float dy = targetLocation.y-square.center.y;
	float distanceToTarget = sqrt (dx*dx +dy*dy);

	float steps = distanceToTarget / kSquareSpeed;
	speedx = dx /steps;
	speedy = dy / steps;

//trigger this method with an NStimer 10 or 20 times a second
-(void)update{
	if(distance<=0)
		return;

	//calculate new frame
	CGRect newFrame = square.frame;
	newFrame.origin = CGPointMake( square.origin.x+speedx,  square.origin.y+speedy);

	//only move if the new frame is OK
	BOOL isBlocked = CGRectIntersectsRect(newFrame, wall.frame); 
	if(isblocked){
		distance=0;
	} else {
		square.frame = newFrame;
		distance -= kSquareSpeed;
	}
}
EXTRA CREDIT: this code may go almost one step PAST the targetLocation; on the last step you should make sure you land directly on the targetLocation.
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Tags
collision detection, intersection

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 420
10 members and 410 guests
7twenty7, chemistry, ChrisYates, gmarro, hussain1982, Retouchable, skrew88, SLIC, walex, xzoonxoom
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,921
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 08:10 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0