如何使用弹弓在不同角度移动物体?

发布于 2024-10-05 23:32:46 字数 1540 浏览 0 评论 0原文

我是 iPhone 应用程序开发和 Xcode 的初学者,我正在使用 Box2D 开发一个具有弹弓效果的小游戏。当使用弹弓时,我需要帮助以各自的角度移动身体。弹弓是使用 ccDrawLine 绘制的,并在其上放置了一个主体。

在我的项目中,当使用弹弓时,物体会朝不同方向随意移动。有谁知道如何解决这个问题?

我的代码:

绘制弹弓:

-(void)draw
{
    //NSLog(@"in dra");
    //glDisable(GL_TEXTURE_2D);
    //glDisableClientState(GL_COLOR_ARRAY);
    //glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    _world->DrawDebugData();

    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_COLOR_ARRAY);
    //glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glColor4f(0.6, 0.4, 0.2, 1.0);
    glLineWidth(4.0f);

    //glEnable(GL_LINE_SMOOTH);
    ccDrawLine( ccp(80, 75),ccp(pt1,pt2));
    ccDrawLine(ccp(pt1,pt2), ccp(240,75));
    ccDrawLine(ccp(80,75),ccp(80,0));
    ccDrawLine(ccp(240,75),ccp(240,0));    
}

将物体放在吊索上:

-(void)addsprite2
{
    stone=[CCSprite spriteWithFile:@"rock.png"];
    stone.position=ccp(160,80);
    stone.tag=1;
    [self addChild:stone];
}

我正在尝试手动创建和添加角度。

角度创建:

    if (stone.position.y < 80 && stone.position.y >= 70)
    {
        ft = abs(tp2)/PTM_RATIO;
    }
     else if(stone.position.y < 70 && stone.position.y >=60)
    {
        ft = (abs(tp2)+90)/PTM_RATIO;
    }

    else if(stone.position.y < 60 && stone.position.y >= 50)
    {
        ft = (abs(tp2)+110)/PTM_RATIO;
    }

    else if(stone.position.y < 50 && stone.position.y >= 40)
    {
        ft = (abs(tp2)+140)

I'm a beginner in iPhone Application Development and Xcode and I'm developing a small game using Box2D which has a slingshot effect. I need help with moving a body in its respective angles when the sling shot is used. The slingshot is drawn using ccDrawLine and a body is placed on it.

In my project the object moves haphazardly in different directions when the sling shot is used. Does anyone know how to fix this?

My code:

Draw the slingshot:

-(void)draw
{
    //NSLog(@"in dra");
    //glDisable(GL_TEXTURE_2D);
    //glDisableClientState(GL_COLOR_ARRAY);
    //glDisableClientState(GL_TEXTURE_COORD_ARRAY);

    _world->DrawDebugData();

    glEnable(GL_TEXTURE_2D);
    glEnableClientState(GL_COLOR_ARRAY);
    //glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glColor4f(0.6, 0.4, 0.2, 1.0);
    glLineWidth(4.0f);

    //glEnable(GL_LINE_SMOOTH);
    ccDrawLine( ccp(80, 75),ccp(pt1,pt2));
    ccDrawLine(ccp(pt1,pt2), ccp(240,75));
    ccDrawLine(ccp(80,75),ccp(80,0));
    ccDrawLine(ccp(240,75),ccp(240,0));    
}

Place the object on the sling:

-(void)addsprite2
{
    stone=[CCSprite spriteWithFile:@"rock.png"];
    stone.position=ccp(160,80);
    stone.tag=1;
    [self addChild:stone];
}

I'm trying to create and add the angles manually.

Angle creation:

    if (stone.position.y < 80 && stone.position.y >= 70)
    {
        ft = abs(tp2)/PTM_RATIO;
    }
     else if(stone.position.y < 70 && stone.position.y >=60)
    {
        ft = (abs(tp2)+90)/PTM_RATIO;
    }

    else if(stone.position.y < 60 && stone.position.y >= 50)
    {
        ft = (abs(tp2)+110)/PTM_RATIO;
    }

    else if(stone.position.y < 50 && stone.position.y >= 40)
    {
        ft = (abs(tp2)+140)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看透却不说透 2024-10-12 23:32:46

您可以使用贝塞尔曲线在2D空间中实现弹丸曲线效果。
然后根据拉动距离设置第一和第二控制点以及终点。

这是实现弹丸曲线的最简单方法。

另外,要移动对象,您可以使用touchesMoved函数并将对象的位置设置为与touchesMoved点的位置相同。

object.position = pulling.position;

这将允许您在移动手指时移动对象。

You can use a bezier curve to implement the projectile curve effect in a 2D space.
Then set the the first and second control points and the end point depending on the pull distance.

This is the simplest way to implement the projectile curve.

Also to move the object you may use touchesMoved function and set the position of the object as the same as that of the touchesMoved point.

object.position = pulling.position;

This will allow you to move the object as you move your finger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文