将后轮约束到前轮
希望我能清楚地描述我的情况,因为每当我试图在白板或笔和纸上向其他人描述它时,都需要几次尝试:)
我的程序的一部分涉及一辆沿着轨道行驶的小车,并且小车用两个轮子运行。铁轨是一条简单的线,呈曲线起伏,也呈扁平状。为了创建关卡,我使用 CGPoints 的 NSMutableArray 并将它们绘制到屏幕上。
我当前的碰撞检测涉及使用每个车轮的 x 坐标并将其与点 x 值数组进行比较。当它位于两点 x 值之间时,我可以使用梯度来发现它是否低于/低于该点的 y 值。当我使用 X 的确定值时,这工作得很好。
所以问题来了。
首先,我有一个前轮,它可以沿着这条线完美滚动,并具有正确的 x 和 y 位置。我遇到的问题是创建一个跟随前轮的后轮。后轮也完美地遵循该线,但它总是在前轮后面有一个设定的 X 距离,这被证明是不现实的,因为当您上下陡峭的山坡时,由于 Y 的差异,车轮之间的距离太大。
示例:
O.......O
Hill 示例:
O
........O
x 方向的距离相同,Y 方向的距离不同,因此斜边距离更大,给人车轮分开的印象。我需要将后轮限制到前轮,因此真实(斜边)距离始终相同。我遇到的困难是,由于这涉及到改变它的 x 位置,我无法处理它的碰撞检测。
如果我开始上坡,我必须改变它的 y 速度以落在前轮后面,所以我需要改变它的 x 值以保持车轮后面相同的真实距离,现在它的 y 值将是错误的,因为它最终落在地板上。
这几天我的脑子和办公室都伤透了,请问有什么帮助吗?
Hopefully I can describe my situation clearly as whenever I try to describe it to others on the whiteboard or pen and paper it takes a few attempts :)
Part of my program involves a cart that rides along a rail and the cart runs on two wheels. The rails is a simple line which hills up and down in curves and also flattens out. To create the level I use an NSMutableArray of CGPoints and draw them to the screen.
My current collision detection involves using each wheels x-cordinate and comparing it to the array of points x values. When it's between two points x-values I can use the gradient to discover whether or not it's below/under the y-value at that point. This works fine when I use definite values of X.
So here comes the problem.
First, I have a front wheel which rolls along this line perfectly with a correct x and correct y position. The problem I'm having is creating a back wheel that follows this front wheel. The back wheel also follows the line perfectly however it's always a set X distance behind the front wheel, this proves to be unrealistic as when you go up or down steep hills the distance between the wheels is too great due to the difference in Y.
Example:
O.......O
Hill Example:
O
........O
Same distance in x, difference in Y so the hypotenuse distance is greater and gives the impression of the wheels spreading apart. I need to constrain the back wheel to the front wheel so it's true(hypotenuse) distance is always the same. The difficulty I'm having is that since this involves changing it's x-position I can't get a handle on it's collision detection.
If I start to go uphill, I have to change it's y velocity to drop behind the front wheel, so then I need to change it's x-value to stay the same true distance behind the wheel, now it's y-value will be wrong since it ends up in the floor.
Wracked my brains and the offices for a few days now, any help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
保持车轮间距相同的最简单方法可能是将两个车轮限制在轨道上。您可以为 NSMutableArray 轨道定义第三个数字,即距轨道原点的距离。您必须定义自己的类,而不是使用 CGPoints 类。
另一种可能性是让轮子的中心跟随轨道,但偏移量等于轮子的半径。每个轮子的中心是使用轨道 x 和 y 坐标、轮子接触轨道的线到轮子中心的角度以及轮子的半径来计算的,
Probably the easiest way to keep the wheels the same distance apart is to constrain both wheels to the track. You could define a 3rd number for the NSMutableArray track, distance from track origin. You'd have to define your own class instead of using the CGPoints class.
Another possibility is to have the center of the wheels follow the track, but with an offset equal to the radius of the wheels. The center of each wheel is calculated using the track x and y coordinates, the angle of the line from where the wheel touches the track to the center of the wheel, and the radius of the wheel,