如何在libgdx中进行旋转对象的碰撞检测(How to get collision detection of a rotating object in libgdx)

编程入门 行业动态 更新时间:2024-10-28 20:18:34
如何在libgdx中进行旋转对象的碰撞检测(How to get collision detection of a rotating object in libgdx)

我有一个将要旋转的游戏对象。

import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; public class Object extends AbstractGameObject{// AbstractGameObject is a class to apply physics on the object private TextureRegion object; float rot; public Object(){ init(); } private void init() { dimension.set(2f, 2f); object = Assets.instance.object.object;//it just retrieves the texture region from assets class // Set bounding box for collision detection bounds.set(0,0, dimension.x, dimension.y); } @Override public void render(SpriteBatch batch) { TextureRegion reg = null; final float degressPerSecond = 120.0f; rot = (rot + Gdx.graphics.getDeltaTime() * degressPerSecond) % 360; reg = object; batch.draw(reg.getTexture(), position.x, position.y, origin.x, origin.y, dimension.x, dimension.y, scale.x, scale.y, rot, reg.getRegionX(), reg.getRegionY(), reg.getRegionWidth(), reg.getRegionHeight(), false, false); } }

Rectangle bounds = new Rectangle(); //表示wt我的意思是边界

通过浮动腐烂物体将旋转。 by,因为边界设置为固定点,并且边界没有旋转。 我的问题是,当我的charac接触它时,这个对象开始旋转。 应该发生的事情。 谢谢你。

I have a game object which is going to rotate.

import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.graphics.g2d.TextureRegion; public class Object extends AbstractGameObject{// AbstractGameObject is a class to apply physics on the object private TextureRegion object; float rot; public Object(){ init(); } private void init() { dimension.set(2f, 2f); object = Assets.instance.object.object;//it just retrieves the texture region from assets class // Set bounding box for collision detection bounds.set(0,0, dimension.x, dimension.y); } @Override public void render(SpriteBatch batch) { TextureRegion reg = null; final float degressPerSecond = 120.0f; rot = (rot + Gdx.graphics.getDeltaTime() * degressPerSecond) % 360; reg = object; batch.draw(reg.getTexture(), position.x, position.y, origin.x, origin.y, dimension.x, dimension.y, scale.x, scale.y, rot, reg.getRegionX(), reg.getRegionY(), reg.getRegionWidth(), reg.getRegionHeight(), false, false); } }

Rectangle bounds = new Rectangle();//to say wt i mean by bounds

by the float rot the object is going to rotate. by as the bounds is set to fixed point and as bounds doesn't have a rotation. my problem is wen this object starts rotating when my charac touches it. something should happens. thank u.

最满意答案

您可以从矩形创建多边形。 然后,将旋转应用于多边形。 要检查点是否在内部,只需使用polygon.contains(x,y)并检查它是否与其他多边形重叠,请使用Intersector类。 http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html

You can create a polygon from your rectangle. Then, apply the rotation to the polygon. To check if a point is inside just use polygon.contains(x,y) and to check if it is overlapping with other polygon, use the Intersector class. http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/math/Intersector.html

更多推荐

本文发布于:2023-07-05 02:40:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1032095.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   如何在   libgdx   collision   rotating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!