平台游戏 - 让对象根据鼠标位置调整角度 [英] Platform Game - Getting the Object to adjust angle according to mouse position

查看:113
本文介绍了平台游戏 - 让对象根据鼠标位置调整角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上要做的是一个平台游戏,你可以通过越过敌人等进入阶段。当你前进并捡起东西时,你会得到不同的武器。我想要你拿起这些不同的武器,以便能够指向鼠标当前的方向。

What I basically am going for is a platform game where you can advance through stages by getting past enemies and such. You will get different weapons as you advance and pick things up. I would like for these different weapons you pick up to be able to be pointed in the direction that the mouse is currently at.

因为它有点难以举例说明我发现了一个视频,几乎可以显示我想要的内容,但使用了不同的精灵。
http://www.youtube.com/watch?v=8W7WSkQq5SU

Since it's a little hard to example it right I found a video that pretty much shows exactly what I want but with a different sprites used. http://www.youtube.com/watch?v=8W7WSkQq5SU

所以我想说这是我用于其中一种武器的图像:

So lets say this is the image I would use for one of the weapons:

如何调整它(伪代码)以使镐的正确位置指向鼠标的方向相同?

How would I go about adjusting it (pseudo code) to make the right point of the pickaxe be pointed in the same direction of the mouse?

我只是在寻找一些伪代码,告诉你如何以最有效的方式和最顺畅的方式来做这件事。

I just looking for some pseudo code of how you may go about doing this the most efficient way and the smoothest way.

推荐答案

如果您使用的是Java Graphics2D ,则必须使用 AfflineTransform 。要做到这一点,首先要转换到镐轴的点。然后你可以按照鼠标的方向旋转,可以使用 atan2 找到。然后你绘制轴,使偏移量与轴点匹配。

If you are using Java Graphics2D then you have to use AfflineTransform. To do this first translate to the point of the axis of your pickaxe. Then you would rotate by the direction to the mouse which can be found using atan2. then you draw the axe so the offset matches the axis point.

伪代码:

#function drawAxe:
    translate(player.x, player.y);
    translate(pickaxis.x, pickaxis.y);
    rotation=toDegrees(atan2(mouse.y-player.y,mouse.x-player.x));
    rotate(roation);
    translate(-axisPointOnPickaxe.x, -axisPointOnPickaxe.y);
    drawImage(pickaxe.texture);
    IdentityMatrix();//[[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]] or no transformation. i think you have to set a blank transform to do this in java efficiently
#endFunction drawAxe

这篇关于平台游戏 - 让对象根据鼠标位置调整角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆