JS实现一个物体的自由落体

代码如下

<!DOCTYPE html>  
< html  lang= "zh-cn">  
< head>  
    < meta  charset= "UTF-8">  
    < title>HTML5+CSS3练习</ title>  
   
</ head>  
< body>  
   < title></ title>  
< p>横向系数:< input  id= "hp"  type= "text"  value= "0.1" /> (0-1)</ p>  
< p>纵向系数:< input  id= "vp"  type= "text"  value= "0.6" /> (0-1)</ p>  
< p>速度:< input  id= "sp"  type= "text"  value= "30" />(20-1000)  
< input  type= "button"  value= "demo"  onclick= "demo(document.getElementById('hp').value, document.getElementById('vp').value, document.getElementById('sp').value)" /></ p>  
< div  style= "margin-left:600px;">  
    < div  id= "fall"  style= "width:30px; height:30px; background:#f90; position:relative; right:0px; bottom:40px"></ div>  
</ div>  
< script  type= "text/javascript">  
var i  =  null;  
function  demo( hp, vp, sp) {  
     var f =document. getElementById( 'fall');  
        f.style.right = '0px';  
        f.style.bottom = '40px';  
        i && clearInterval(i);  
     var h = 1,v = 1,hp =(hp > 0 &&hp < 1) ?hp : 0.2,vp =(vp > 0 &&vp < 1) ?vp : 0.5,sp =(sp > 20  || sp < 1000) ?sp : 30;  
        i = setInterval( function(){  
             if(f){  
                 var r = parseInt(f.style.right) +h,b = parseInt(f.style.bottom) -v;  
                f.style.right =r + 'px';  
                f.style.bottom =b + 'px';  
                 if(r > 1000) clearInterval(i);  
                 if(b >- 210){  
                    v += 2  
                }  else {  
                    h =(v > 0) ?v *hp : 0;  
                    v *=(v > 0) ?- 1 *vp : 0  
                }  
            }  
        },sp);  
    }  
</ script>  
      
</ body>  
</ html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据引用内容,可以使用Qt和Box2D结合QGraphicsItem来实现物体自由落体效果。下面是一个示例代码: ```cpp #include <QApplication> #include <QGraphicsScene> #include <QGraphicsView> #include <QTimer> #include <Box2D/Box2D.h> class MyContactListener : public b2ContactListener { void BeginContact(b2Contact* contact) { // 处理碰撞开始事件 } void EndContact(b2Contact* contact) { // 处理碰撞结束事件 } }; int main(int argc, char *argv[]) { QApplication a(argc, argv); // 创建场景和视图 QGraphicsScene scene; QGraphicsView view(&scene); view.setRenderHint(QPainter::Antialiasing); view.setSceneRect(-200, -200, 400, 400); view.show(); // 创建物理世界 b2Vec2 gravity(0.0f, 9.8f); b2World world(gravity); MyContactListener contactListener; world.SetContactListener(&contactListener); // 创建地面 b2BodyDef groundBodyDef; groundBodyDef.position.Set(0.0f, 200.0f); b2Body* groundBody = world.CreateBody(&groundBodyDef); b2PolygonShape groundBox; groundBox.SetAsBox(200.0f, 10.0f); groundBody->CreateFixture(&groundBox, 0.0f); // 创建物体 b2BodyDef bodyDef; bodyDef.type = b2_dynamicBody; bodyDef.position.Set(0.0f, -100.0f); b2Body* body = world.CreateBody(&bodyDef); b2CircleShape dynamicCircle; dynamicCircle.m_radius = 10.0f; b2FixtureDef fixtureDef; fixtureDef.shape = &dynamicCircle; fixtureDef.density = 1.0f; fixtureDef.friction = 0.3f; body->CreateFixture(&fixtureDef); // 创建定时器,模拟物理世界 QTimer timer; QObject::connect(&timer, &QTimer::timeout, [&]() { world.Step(1.0f / 60.0f, 6, 2); b2Vec2 position = body->GetPosition(); qreal x = position.x * 10.0; qreal y = -position.y * 10.0; body->SetTransform(b2Vec2(x, y), 0.0f); body->SetLinearVelocity(b2Vec2(0.0f, 0.0f)); body->SetAngularVelocity(0.0f); scene.clear(); scene.addEllipse(x - 10, y - 10, 20, 20); }); timer.start(16); return a.exec(); } ``` 这段代码使用了Qt的QGraphicsScene和QGraphicsView来显示物体自由落体效果。通过Box2D物理引擎模拟物体的运动,并在每次模拟后更新物体在场景中的位置。在这个例子中,物体一个圆形,它会从初始位置自由落体,并在与地面碰撞后反弹。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值