- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图让一个 b2body 对象以恒定的速度跟随触摸位置,但我在“y”轴上遇到问题, body 在该轴上到达“屏幕的另一半”上的位置就像镜像一样……不过“x”轴很好。(我使用的是cocos2d 1.0.0)
这里是所有的代码:
HelloWorldLayer.h
#import "cocos2d.h"
#import "Box2D.h"
#define PTM_RATIO 32.0
@interface HelloWorldLayer : CCLayer {
b2World *_world;
b2Body *_body;
CCSprite *_ball;
}
+ (id) scene;
@end
HelloWorldLayer.m
#import "HelloWorldLayer.h"
#import "CGPointExtension.h"
static CGPoint location;
@implementation HelloWorldLayer
+ (id)scene {
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild:layer];
return scene;
}
- (id)init {
if ((self=[super init])) {
CGSize winSize = [CCDirector sharedDirector].winSize;
// Create sprite and add it to the layer
_ball = [CCSprite spriteWithFile:@"ball.png" rect:CGRectMake(0, 0, 52, 52)];
_ball.position = ccp(100, 300);
[self addChild:_ball];
// Create a world
b2Vec2 gravity = b2Vec2(0.0f, 0.0f);
_world = new b2World(gravity);
// Create edges around the entire screen
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
b2Body *groundBody = _world->CreateBody(&groundBodyDef);
b2EdgeShape groundEdge;
b2FixtureDef boxShapeDef;
boxShapeDef.shape = &groundEdge;
//wall definitions
groundEdge.Set(b2Vec2(0,0), b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
groundEdge.Set(b2Vec2(0,0), b2Vec2(0,winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundEdge.Set(b2Vec2(0, winSize.height/PTM_RATIO),
b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO));
groundBody->CreateFixture(&boxShapeDef);
groundEdge.Set(b2Vec2(winSize.width/PTM_RATIO, winSize.height/PTM_RATIO),
b2Vec2(winSize.width/PTM_RATIO, 0));
groundBody->CreateFixture(&boxShapeDef);
// Create ball body and shape
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(100/PTM_RATIO, 300/PTM_RATIO);
ballBodyDef.userData = _ball;
_body = _world->CreateBody(&ballBodyDef);
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 1.0f;
ballShapeDef.friction = 0.2f;
ballShapeDef.restitution = 0.8f;
_body->CreateFixture(&ballShapeDef);
[self schedule:@selector(tick:)];
[self setTouchEnabled:YES];
[self setAccelerometerEnabled:YES];
}
return self;
}
- (void)tick:(ccTime) dt {
int32 velocityIterations = 8;
int32 positionIterations = 1;
_world->Step(dt, velocityIterations, positionIterations);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *ballData = (CCSprite *)b->GetUserData();
ballData.position = ccp(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
ballData.rotation = 1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
location = [touch locationInView:touch.view];
NSLog(@"%@", NSStringFromCGPoint(location));
}
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [[event allTouches] anyObject];
location = [touch locationInView:touch.view];
[self schedule:@selector(asd:)];
}
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
// [self unschedule:@selector(asd:)];
// _body->SetLinearVelocity(b2Vec2(0,0));
// _body->SetAngularVelocity(0);
//_body->SetActive(false);
}
-(void)asd:(ccTime) dt {
NSLog(@"%@", NSStringFromCGPoint(location));
//float ca = location.y;
b2Vec2 convertedLocation = b2Vec2(location.x/PTM_RATIO - _body->GetPosition().x, location.y/PTM_RATIO - _body->GetPosition().y);
//b2Vec2 toTouchPoint = convertedLocation - _body->GetPosition();
convertedLocation.Normalize();
b2Vec2 impulse = 5 * convertedLocation;
_body->SetLinearVelocity(impulse);
// _body->ApplyLinearImpulse(_body->GetMass() * impulse, _body->GetWorldCenter());
}
- (void)dealloc {
delete _world;
_body = NULL;
_world = NULL;
[super dealloc];
}
@end
提前致谢!
最佳答案
当您使用 locationInView 通过触摸获取位置时,原点位于左上角。在 cocos2D 中,原点位于左下角。你可以翻转 y 值。或者您可以使用 CCDirector 的 ConvertToGL 函数。我更喜欢后者,主要是因为它可以处理所有纵向和横向模式。
CGPoint loc = [touch locationInView:touch.view]
location = [[CCDirector sharedDirector] convertToGL:loc];
关于c++ - Box2d SetLinearVelocity 奇怪的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18499255/
我一直在摆弄 jbox2d,当物体的 x 速度受到世界引力的影响时,我感到很惊讶。这是我的代码: //create world Vec2 gravity = new Vec2(0, 1
我的 box2d 世界中有一个 body (玩家)。 每当我尝试设置它的 x 速度(每 2 秒或其他)并且它由于重力而下落时,它会上下抖动。 我也曾尝试将其 y 速度设置为等于重力 (-30.0f),
我试图让一个 b2body 对象以恒定的速度跟随触摸位置,但我在“y”轴上遇到问题, body 在该轴上到达“屏幕的另一半”上的位置就像镜像一样……不过“x”轴很好。(我使用的是cocos2d 1.0
我正在尝试使用 OpenGL 和 BulletPhysics 创建一个具有 RigidBody 的 FPS 播放器。唯一的问题是让盒子移动。我尝试使用 setLinearForce、applyForc
首先,这是我在 StackOverflow 上写的第一篇文章,我实际上是编程新手,所以如果我说错了什么,我很抱歉。 我试了又试让这项工作成功 - 我想模拟一个静止在空中的物体,为此我使用 SetLin
我是一名优秀的程序员,十分优秀!