使用代码进行冲突检测



我是编程新手,现在真的需要帮助。在过去的两个月里,我一直在寻找一个答案。我正在使用Xcode和objective-c。我的问题是关于碰撞检测的。有成千上万的例子可以说明当 2 个矩形使用 CGRECT 发生碰撞时该怎么做,例如警报或翻转屏幕或播放声音扩展,但任何地方都没有关于什么都不做,哈哈!我想要的只是我的对象不穿过另一个对象!这就是我想在屏幕上继续拖动它的全部内容。我只是不想让 2 个对象彼此重叠,似乎我是世界上唯一想要这样做的在线用户,因为我找不到任何东西。所以请帮忙,因为我是新来的.请尽可能简单

#import "YellowDot.h"
@interface YellowDot ()
@end
@implementation YellowDot
@synthesize Dot;
@synthesize CollisionImage;
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *Drag = [ [  event allTouches ] anyObject ];
Dot.center = [ Drag locationInView: self.view ];
[self checkCollison];
}
-(void) checkCollison
{
if (CGRectIntersectsRect(Dot.frame, CollisionImage.frame))
{
AudioServicesPlaySystemSound(playSoundId);
}

}
- (void)viewDidLoad
{
NSURL *SoundURL = [ NSURL fileURLWithPath:[[NSBundle mainBundle]           pathForResource:@"beep"ofType:@"wav"]];
AudioServicesCreateSystemSoundID((__bridge CFURLRef)SoundURL, & playSoundId);

[super viewDidLoad];
// Do any additional setup after loading the view.

}

and here is the .h file :
#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>
@interface YellowDot : UIViewController
{
IBOutlet UIImageView *Dot;
IBOutlet UIImageView *CollisionImage;
SystemSoundID playSoundId;
}
@property (nonatomic, retain) UIImageView *Dot;
@property (nonatomic, retain) UIImageView *CollisionImage;

@end

那么那里能有什么呢? 正如你所看到的,它在碰撞时已经播放了声音,但仅此而已。点是我在屏幕上拖动的图像,碰撞图像是我希望点与之碰撞但作为墙停止的图像。希望它足够清楚(我是法国人,很抱歉写得不好):S 谢谢。

鉴于您已成功检测到碰撞,答案似乎是如果移动导致碰撞,则不要将对象更新到新位置。 只是不要更新 Dot.center。顺序是:获取移动的触摸事件,预先计算对象将要出现的位置,如果没有碰撞,则移动它;如果发生冲突,请不要更新其位置。

请注意,OpenGL可能更适合这种类型的事情,因为您将要做很多事情。

最新更新