LibGdx--Box2d中元件与刚体重合

本文介绍了如何在LibGdx和Box2d中处理元件与刚体重合的情况,包括已知图片位置时绘制刚体与图片重合,以及已知刚体位置时绘制图片与刚体重合。文章还提到了单位转换的代码封装,并展示了一个使用Fender.png图片的示例,该图片位于Android工程的assets文件夹下。
摘要由CSDN通过智能技术生成

这里的元件暂且为图片,这里分两种情况:

1.知道图片的position,要绘制一个刚体与图片重合

2.知道刚体的position,要绘制一个图片与刚体重合

将单位换算的代码做一下封装

package com.joye3g.box2d.util;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;

public class Transform {
	/**
	 * @param x_px				图片所在x坐标
	 * @param y_px				图片所在y坐标
	 * @param width_px			图片宽度
	 * @param height_px			图片高度
	 * @param scale				缩放比例
	 * @return					(x,y)直接设置为body的position可使body与图片重合
	 */
	public static Vector2 ptm(float x_px, float y_px, float width_px, float height_px, float scale){
		float screenWidth = Gdx.graphics.getWidth();
		float screenHeight = Gdx.graphics.getHeight();
		Vector2 vector2 = new Vector2();
		vector2.x = -(screenWidth - x_px * 2 - width_px) / scale / 2;
		vector2.y = -(screenHeight - y_px * 2 - height_px) / scale / 2;
		return vector2;
	}

	/**
	 * @param x_m				body所在x坐标
	 * @p
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值